From 52e7bb9af7bf2e6e9188cfc74232f4b9756df4a0 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Sat, 21 Dec 2024 21:15:59 -0800 Subject: [PATCH] day22: part 1 solution --- src/day22.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/day22.rs diff --git a/src/day22.rs b/src/day22.rs new file mode 100644 index 0000000..b6ff2e8 --- /dev/null +++ b/src/day22.rs @@ -0,0 +1,64 @@ +use aoc_runner_derive::{aoc, aoc_generator}; + +fn evolve_secret(mut n: i64) -> i64 { + n = ((n * 64) ^ n) % 16777216; + n = ((n / 32) ^ n) % 16777216; + n = ((n * 2048) ^ n) % 16777216; + n +} + +fn rounds(mut secret: i64, n: i64) -> i64 { + for _ in 0..n { + secret = evolve_secret(secret) + } + secret +} + +fn parse(input: &str) -> Vec { + input.lines().map(|l| l.parse().unwrap()).collect() +} + +#[aoc(day22, part1)] +fn part1(input: &str) -> i64 { + let secrets = parse(input); + + secrets.iter().map(|s| rounds(*s, 2000)).sum::() +} + +#[aoc(day22, part2)] +fn part2(input: &str) -> i64 { + todo!() +} + + +#[cfg(test)] +mod tests { + use super::*; + const EXAMPLE: &str = "1 +10 +100 +2024"; + + #[test] + fn evolution() { + assert_eq!(evolve_secret(123), 15887950); + assert_eq!(evolve_secret(15887950), 16495136); + assert_eq!(evolve_secret(16495136), 527345); + } + + #[test] + fn test_rounds() { + assert_eq!(rounds(1, 2000), 8685429); + assert_eq!(rounds(10, 2000), 4700978); + } + + #[test] + fn part1_example() { + assert_eq!(part1(EXAMPLE),37327623); + } + + #[test] + fn part2_example() { + assert_eq!(part2(EXAMPLE), 0); + } +} \ No newline at end of file