Skip to main content

aoc/year2016/
day16.rs

1//! # Dragon Checksum
2//!
3//! We solve efficiently with a key insight that the checksum is simply the
4//! [odd parity bit](https://en.wikipedia.org/wiki/Parity_bit) for each block. If the total number
5//! of ones is even then the result is one, if the total is odd then the result is zero.
6//!
7//! This means that only the *total number of ones is important* not the pattern itself. Each
8//! checksum bit is computed over the largest power of two divisible into the output size. For part
9//! one this is 2⁴ or 16 and for part two this is 2²¹ or 2097152. If we can calculate the number of
10//! ones for any arbitrary length then we can find the number at the start and end of each block,
11//! subtract from each other to get the total in the range then find the checksum bit.
12//!
13//! We find the number of ones for a pattern of length `n` in `log(n)` complexity as follows:
14//! * Start with a known pattern `abcde` and let the reversed bit inverse of this pattern be
15//!   `EDCBA`.
16//! * Calculate the [prefix sum](https://en.wikipedia.org/wiki/Prefix_sum) of the known sequence.
17//! * If the requested length is within the known sequence (in this example from 0 to 5 inclusive)
18//!   then we're done, return the number of ones directly.
19//! * Else after one repetition this becomes `abcde0EDCBA`.
20//! * If the length is at or to the right of the middle `0`, for example `length` is 8 then the
21//!   number of ones is:
22//!    * Let `half` = 5 the length of the left hand known sequence.
23//!    * Let `full` = 11 the length of the entire sequence.
24//!    * Ones in `abcde` => x
25//!    * Ones in `EDCBA` => the number of zeroes in `abcde` => 5 - x => half - x
26//!    * Ones in `abc` => y
27//!    * Ones in `CBA` => the number of zeroes in `abc` => 3 - y => 11 - 8 - y => full - length - y
28//!      => next - y
29//!    * The total number of ones in `abcde0ED` is x + (half - x) - (next - y) => half - next + y
30//!
31//! Now for the really neat part. We can recursively find the number of ones in `y` by repeating
32//! the same process by setting the new `length` to `next`. We keep recursing until the length
33//! is less than the size of the initial input and we can lookup the final count from the prefix
34//! sum.
35//!
36//! Note that it is also possible to compute the parity of any prefix of the Dragon Curve in
37//! O(1) time. The formula is available on [OEIS A255070](https://oeis.org/A255070), and there
38//! are a [couple](https://www.reddit.com/r/adventofcode/comments/5ititq/2016_day_16_c_how_to_tame_your_dragon_in_under_a/)
39//! of [posts](https://www.reddit.com/r/adventofcode/comments/1r642oc/2016_day_16_in_review_dragon_checksum/)
40//! showing how to utilize that approach. However, the logarithmic solution shown here is
41//! fast enough to not need to worry about askalski's comment "I have no idea why it works,
42//! only that it does work."
43
44/// Build a prefix sum of the number of ones at each length in the pattern
45/// including zero at the start.
46pub fn parse(input: &str) -> Vec<usize> {
47    let mut sum = 0;
48    let mut ones = vec![0];
49
50    for b in input.trim().bytes() {
51        sum += (b & 1) as usize;
52        ones.push(sum);
53    }
54
55    ones
56}
57
58/// 272 is 17 × 2⁴.
59pub fn part1(input: &[usize]) -> String {
60    checksum(input, 272)
61}
62
63/// 35651584 is 17 × 2²¹.
64pub fn part2(input: &[usize]) -> String {
65    checksum(input, 35651584)
66}
67
68/// Collect the ones count at each `step_size` then subtract in pairs to calculate the number of
69/// ones in each interval to give the checksum.
70pub fn checksum(input: &[usize], disk_size: usize) -> String {
71    // Determine how many blocks and how big each one is, by lowest 1-bit in disk_size.
72    let step_size = disk_size.isolate_lowest_one();
73    let blocks = disk_size / step_size;
74
75    let counts: Vec<_> = (0..blocks + 1).map(|i| count(input, i * step_size)).collect();
76    counts.array_windows().map(|&[a, b]| if (b - a) % 2 == 0 { '1' } else { '0' }).collect()
77}
78
79/// Counts the number of ones from the start to the index (inclusive).
80fn count(ones: &[usize], mut length: usize) -> usize {
81    let mut half = ones.len() - 1;
82    let mut full = 2 * half + 1;
83
84    // Find the smallest pattern size such that the index is on the right hand side
85    // (greater than or equal to) the middle `0` character.
86    while full < length {
87        half = full;
88        full = 2 * half + 1;
89    }
90
91    let mut result = 0;
92
93    while length >= ones.len() {
94        // Shrink the pattern size until the index is on the right side once more.
95        while length <= half {
96            half /= 2;
97            full /= 2;
98        }
99
100        // "Reflect" the index then add the extra number of ones to the running total.
101        let next = full - length;
102        result += half - next;
103        length = next;
104    }
105
106    result + ones[length]
107}