From 26815a6480e3fed9b1b8d27f514afa69a6f57c2e Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 11 Dec 2024 14:46:44 +0200 Subject: [PATCH] feat: day11 --- input/day11/example | 1 + src/day11/mod.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 8 +++++ 3 files changed, 97 insertions(+) create mode 100644 input/day11/example create mode 100644 src/day11/mod.rs diff --git a/input/day11/example b/input/day11/example new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/input/day11/example @@ -0,0 +1 @@ +125 17 diff --git a/src/day11/mod.rs b/src/day11/mod.rs new file mode 100644 index 0000000..1489b6f --- /dev/null +++ b/src/day11/mod.rs @@ -0,0 +1,88 @@ +use std::{ + collections::HashMap, + fs::File, + io::{BufRead, BufReader}, + path::Path, +}; + +pub fn solve(input: &Path) -> anyhow::Result<()> { + println!("part one: {}", part_one(input)?); + println!("part two: {}", part_two(input)?); + + Ok(()) +} + +fn part_one(input: &Path) -> anyhow::Result { + let mut reader = BufReader::new(File::open(input)?); + + let mut input_str = String::new(); + let _ = reader.read_line(&mut input_str)?; + let mut stones: Vec> = input_str + .split_whitespace() + .map(|c| -> Vec { vec![c.to_string().parse().unwrap()] }) + .collect(); + + for _ in 0..25 { + for stone in &mut stones { + if stone[0] == 0 { + stone[0] = 1; + continue; + } + let stone_str = stone[0].to_string(); + if stone_str.len() % 2 == 0 { + let (right, left) = stone_str.split_at(stone_str.len() / 2); + *stone = vec![right.parse()?, left.parse()?]; + } else { + stone[0] *= 2024; + } + } + stones = stones + .iter() + .flatten() + .map(|stone| -> Vec { vec![*stone] }) + .collect(); + } + + Ok(stones.iter().flatten().count()) +} + +fn part_two(input: &Path) -> anyhow::Result { + let mut reader = BufReader::new(File::open(input)?); + + let mut input_str = String::new(); + let _ = reader.read_line(&mut input_str)?; + let input_stones: Vec = input_str + .split_whitespace() + .map(|c| -> usize { c.to_string().parse().unwrap() }) + .collect(); + let mut stones: HashMap = HashMap::new(); + for stone in input_stones { + if let Some(amount) = stones.get_mut(&stone) { + *amount += 1; + } else { + stones.insert(stone, 1); + } + } + for _ in 0..75 { + for (stone, amount) in &stones.clone() { + *stones.entry(*stone).or_insert(0) -= amount; + if *stone == 0 { + *stones.entry(1).or_insert(0) += amount; + continue; + } + let stone_str = stone.to_string(); + if stone_str.len() % 2 == 0 { + let (right, left) = stone_str.split_at(stone_str.len() / 2); + let right = right.parse()?; + let left = left.parse()?; + *stones.entry(right).or_insert(0) += amount; + *stones.entry(left).or_insert(0) += amount; + } else { + let new = stone * 2024; + *stones.entry(new).or_insert(0) += amount; + } + } + } + + Ok(stones.values().sum()) +} diff --git a/src/main.rs b/src/main.rs index 1f480b6..b944cf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use clap::{Parser, Subcommand}; mod day1; mod day10; +mod day11; mod day2; mod day3; mod day4; @@ -61,6 +62,10 @@ enum DayArgs { #[arg(short)] input: PathBuf, }, + Day11 { + #[arg(short)] + input: PathBuf, + }, } fn main() -> anyhow::Result<()> { @@ -97,6 +102,9 @@ fn main() -> anyhow::Result<()> { DayArgs::Day10 { input } => { day10::solve(&input)?; } + DayArgs::Day11 { input } => { + day11::solve(&input)?; + } } Ok(()) -- 2.44.1