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`,
21//!   for example `length` is 8 then the 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`
26//!      => 5 - x => half - x
27//!    * Ones in `abc` => y
28//!    * Ones in `CBA` => the number of zeroes in `abc`
29//!      => 3 - y => 11 - 8 - y => full - length - y => next - y
30//!    * The total number of ones in `abcde0ED` is
31//!      x + (half - x) - (next - y) => half - next + y
32//!
33//! Now for the really neat part. We can recursively find the number of ones in `y` by repeating
34//! the same process by setting the new `length` to `next`. We keep recursing until the length
35//! is less than the size of the initial input and we can lookup the final count from the prefix sum.
36//!
37//! Note that it is also possible to compute the parity of any prefix of the Dragon Curve in
38//! O(1) time; the formula is available on [OEIS A255070](https://oeis.org/A255070), and there
39//! are a [couple](https://www.reddit.com/r/adventofcode/comments/5ititq/2016_day_16_c_how_to_tame_your_dragon_in_under_a/)
40//! of [posts](https://www.reddit.com/r/adventofcode/comments/1r642oc/2016_day_16_in_review_dragon_checksum/)
41//! showing how to utilize that approach. However, the logarithmic solution shown here is
42//! fast enough to not need to worry about askalski's comment "I have no idea why it works,
43//! only that it does work."
44
45/// Build a prefix sum of the number of ones at each length in the pattern
46/// including zero at the start.
47pub fn parse(input: &str) -> Vec<usize> {
48    let mut sum = 0;
49    let mut ones = vec![0];
50
51    for b in input.trim().bytes() {
52        sum += (b & 1) as usize;
53        ones.push(sum);
54    }
55
56    ones
57}
58
59/// 272 is 17 × 2⁴
60pub fn part1(input: &[usize]) -> String {
61    checksum(input, 272)
62}
63
64/// 35651584 is 17 × 2²¹
65pub fn part2(input: &[usize]) -> String {
66    checksum(input, 35651584)
67}
68
69/// Collect the ones count at each `step_size` then subtract in pairs to calculate the number of
70/// ones in each interval to give the checksum.
71pub fn checksum(input: &[usize], disk_size: usize) -> String {
72    // Determine how many blocks and how big each one is, by lowest 1-bit in disk_size.
73    let step_size = 1 << disk_size.trailing_zeros();
74    let blocks = disk_size / step_size;
75
76    let counts: Vec<_> = (0..blocks + 1).map(|i| count(input, i * step_size)).collect();
77    counts.array_windows().map(|&[a, b]| if (b - a) % 2 == 0 { '1' } else { '0' }).collect()
78}
79
80/// Counts the number of ones from the start to the index (inclusive).
81fn count(ones: &[usize], mut length: usize) -> usize {
82    let mut half = ones.len() - 1;
83    let mut full = 2 * half + 1;
84
85    // Find the smallest pattern size such that the index is on the right hand side
86    // (greater than or to) the middle `0` character.
87    while full < length {
88        half = full;
89        full = 2 * half + 1;
90    }
91
92    let mut result = 0;
93
94    while length >= ones.len() {
95        // Shrink the pattern size until the index is on the right side once more.
96        while length <= half {
97            half /= 2;
98            full /= 2;
99        }
100
101        // "Reflect" the index then add the extra number of ones to the running total.
102        let next = full - length;
103        result += half - next;
104        length = next;
105    }
106
107    result + ones[length]
108}