From: Jack Kinsey Date: Wed, 11 Dec 2024 06:29:41 +0000 (-0500) Subject: Complete day 11 part 1 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=ba81fef9798b74a2b892d986b02c1ded851cb920;p=adventofcode2024.git Complete day 11 part 1 --- diff --git a/src/day11.rs b/src/day11.rs new file mode 100644 index 0000000..d63dcf1 --- /dev/null +++ b/src/day11.rs @@ -0,0 +1,82 @@ +fn input() -> &'static str { + include_str!("../input/day11.txt") +} + +fn parse(input: &str) -> Vec { + input + .split_whitespace() + .map(|s| s.parse().unwrap()) + .collect() +} + +fn iterate(stones: &[u64]) -> Vec { + stones + .iter() + .flat_map(|n| { + if *n == 0 { + vec![1] + } else { + let s = format!("{}", n); + let l = s.len(); + if l % 2 == 0 { + vec![s[0..l / 2].parse().unwrap(), s[l / 2..l].parse().unwrap()] + } else { + vec![n * 2024] + } + } + }) + .collect() +} + +pub fn part1() { + let n = std::iter::successors(Some(parse(input())), |v| Some(iterate(v))) + .take(26) + .last() + .unwrap() + .len(); + println!("Day 11 Part 1: {}", n); +} + +pub fn part2() { + // lol no + let n = std::iter::successors(Some(parse(input())), |v| Some(iterate(v))) + .take(76) + .last() + .unwrap() + .len(); + println!("Day 11 Part 2: {}", n); +} + +#[cfg(test)] +mod test { + use super::*; + + const INPUT_STR: &str = concat!("0 1 10 99 999\n"); + + #[test] + fn test_parse() { + assert_eq!(parse(INPUT_STR), [0, 1, 10, 99, 999]) + } + + #[test] + fn test_iterate() { + assert_eq!(iterate(&parse(INPUT_STR)), [1, 2024, 1, 0, 9, 9, 2021976]); + assert_eq!( + std::iter::successors(Some(parse("125 17")), |v| Some(iterate(v))) + .take(7) + .collect::>(), + [ + vec![125, 17], + vec![253000, 1, 7], + vec![253, 0, 2024, 14168], + vec![512072, 1, 20, 24, 28676032], + vec![512, 72, 2024, 2, 0, 2, 4, 2867, 6032], + vec![1036288, 7, 2, 20, 24, 4048, 1, 4048, 8096, 28, 67, 60, 32], + vec![ + 2097446912, 14168, 4048, 2, 0, 2, 4, 40, 48, 2024, 40, 48, 80, 96, 2, 8, 6, 7, + 6, 0, 3, 2 + ] + ] + ) + } +} diff --git a/src/main.rs b/src/main.rs index f435375..1e16e0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ pub mod day07; pub mod day08; pub mod day09; pub mod day10; -// pub mod day11; +pub mod day11; // pub mod day12; // pub mod day13; // pub mod day14; @@ -37,7 +37,7 @@ const DAYS: &[(Part, Part)] = &[ (day08::part1 as fn(), day08::part2 as fn()), (day09::part1 as fn(), day09::part2 as fn()), (day10::part1 as fn(), day10::part2 as fn()), - // (day11::part1 as fn(), day11::part2 as fn()), + (day11::part1 as fn(), day11::part2 as fn()), // (day12::part1 as fn(), day12::part2 as fn()), // (day13::part1 as fn(), day13::part2 as fn()), // (day14::part1 as fn(), day14::part2 as fn()),