|
| 1 | +//! # Chronal Calibration |
| 2 | +//! |
| 3 | +//! The simplest approach to part two is to store previously seen numbers in a `HashSet` then |
| 4 | +//! stop once a duplicate is found. However this approach requires scanning the input of ~1,000 |
| 5 | +//! numbers multiple times, around 150 times for my input. |
| 6 | +//! |
| 7 | +//! A much faster `O(nlogn)` approach relies on the fact that each frequency increases by the same |
| 8 | +//! amount (the sum of all deltas) each time the list of numbers is processed. For example: |
| 9 | +//! |
| 10 | +//! ```none |
| 11 | +//! Deltas: +1, -2, +3, +1 => |
| 12 | +//! 0 1 -1 2 |
| 13 | +//! 3 4 2 5 |
| 14 | +//! ``` |
| 15 | +//! |
| 16 | +//! Two frequencies that are a multiple of the sum will eventually repeat. First we group each |
| 17 | +//! frequencies by its remainder modulo the sum, using `rem_euclid` to handle negative frequencies |
| 18 | +//! correctly, Then we sort, first by the remainder to group frequencies that can repeat together, |
| 19 | +//! then by the frequency increasing in order to help find the smallest gap between similar |
| 20 | +//! frequencies, then lastly by index as this is needed in the next step. |
| 21 | +//! |
| 22 | +//! For the example this produces `[(0, 0, 0), (1, 1, 1), (2, -1, 2), (2, 2, 3)]`. Then we use |
| 23 | +//! a sliding windows of size two to compare each pair of adjacent canditates, considering only |
| 24 | +//! candidates with the same remainder. For each valid pair we then produce a tuple of |
| 25 | +//! `(frequency gap, index, frequency)`. |
| 26 | +//! |
| 27 | +//! Finally we sort the tuples in ascending order, first by smallest frequency gap, breaking any |
| 28 | +//! ties using the index to find frequencies that appear earlier in the list. The first tuple |
| 29 | +//! in the list gives the result, in the example this is `[(3, 2, 2)]`. |
| 30 | +use crate::util::parse::*; |
| 31 | + |
| 32 | +pub fn parse(input: &str) -> Vec<i32> { |
| 33 | + input.iter_signed().collect() |
| 34 | +} |
| 35 | + |
| 36 | +pub fn part1(input: &[i32]) -> i32 { |
| 37 | + input.iter().sum() |
| 38 | +} |
| 39 | + |
| 40 | +pub fn part2(input: &[i32]) -> i32 { |
| 41 | + // The frequencies increase by this amount each pass through the list of deltas. |
| 42 | + let total: i32 = input.iter().sum(); |
| 43 | + |
| 44 | + // Calculate tuples of `(frequency gap, index, frequency)` then sort to group frequencies that |
| 45 | + // can collide together. |
| 46 | + let mut frequency: i32 = 0; |
| 47 | + let mut seen = Vec::with_capacity(input.len()); |
| 48 | + |
| 49 | + for n in input { |
| 50 | + seen.push((frequency.rem_euclid(total), frequency, seen.len())); |
| 51 | + frequency += n; |
| 52 | + } |
| 53 | + |
| 54 | + seen.sort_unstable(); |
| 55 | + |
| 56 | + // Compare each adjacent pair of tuples to find candidates, then sort by smallest gap first, |
| 57 | + // tie breaking with index if needed. |
| 58 | + let mut pairs = Vec::new(); |
| 59 | + |
| 60 | + for window in seen.windows(2) { |
| 61 | + let (remainder0, freq0, index0) = window[0]; |
| 62 | + let (remainder1, freq1, _) = window[1]; |
| 63 | + |
| 64 | + if remainder0 == remainder1 { |
| 65 | + pairs.push((freq1 - freq0, index0, freq1)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pairs.sort_unstable(); |
| 70 | + |
| 71 | + // Result is the frequency of the first tuple. |
| 72 | + let (_, _, freq) = pairs[0]; |
| 73 | + freq |
| 74 | +} |
0 commit comments