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 the 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 input.lines().skip(1).take(2).map(str::unsigned::<u32>).product()
22}
23
24pub fn part1(input: &u32) -> u32 {
25 let offset = *input;
26 let mut result = 0;
27
28 // Find the next number with binary pattern `101010..` greater than the input.
29 while result < offset {
30 result = (result << 2) | 2;
31 }
32
33 result - offset
34}
35
36pub fn part2(_input: &u32) -> &'static str {
37 "n/a"
38}