use std::collections::HashMap; use std::collections::HashSet; #[path = "../lib.rs"] mod lib; fn step(x: u32) -> u32 { let mut y = x; y = y ^ ((y << 6) % (1 << 24)); y = y ^ (y >> 5); y = y ^ ((y << 11) % (1 << 24)); return y; } fn main() { let mut sum1 = 0; let mut sums2 = HashMap::new(); for line in lib::iter_input() { let mut x = line.parse::().unwrap(); let mut seen = HashSet::new(); let mut pattern = 0; let mut prev = 0; for i in 0..2000 { x = step(x); let value = x % 10; let d = 10 + value - prev; // +10 to avoid negative numbers pattern = ((pattern << 5) | d) % (1 << 20); if i >= 3 && seen.insert(pattern) { sums2 .entry(pattern) .and_modify(|sum| *sum += value) .or_insert(value); } prev = value; } sum1 += x as u64; } println!("part1: {}", sum1); println!("part2: {}", sums2.values().max().unwrap()); }