aoc/year2016/
day25.rs

1//! # Clock Signal
2//!
3//! Like [`Day 12`] and [`Day 23`] this problem is all about *reading* code not writing code.
4//!
5//! Reverse engineering the code shows that it takes the initial value of `a` then adds
6//! two constants multiplied by each other to create a seed.
7//!
8//! This seed is then repeatedly bit shifted right by dividing by 2 using an inefficient linear
9//! time loop. The remainder (the bit that drops off) is the output. This means that output
10//! sequence is simply the binary digits of `a + offset` in reverse repeated over and over.
11//!
12//! To obtain the desired pattern we need the next highest binary number that has the
13//! pattern `101010..`.
14//!
15//! [`Day 12`]: crate::year2016::day12
16//! [`Day 23`]: crate::year2016::day23
17use crate::util::parse::*;
18
19/// Extract the constant offset from the assembunny code.
20pub fn parse(input: &str) -> u32 {
21    let lines: Vec<_> = input.lines().collect();
22    let first: u32 = lines[1].unsigned();
23    let second: u32 = lines[2].unsigned();
24    first * second
25}
26
27pub fn part1(input: &u32) -> u32 {
28    let offset = *input;
29    let mut result = 0;
30
31    // Find the next number with binary pattern `101010..` greater than the input.
32    while result < offset {
33        result = (result << 2) | 2;
34    }
35
36    result - offset
37}
38
39pub fn part2(_input: &u32) -> &'static str {
40    "n/a"
41}