This commit is contained in:
sepia 2024-12-11 11:29:50 -06:00
parent 17b62f7e79
commit 78f7f2db49
2 changed files with 50 additions and 1 deletions

47
src/day11.rs Normal file
View File

@ -0,0 +1,47 @@
use std::collections::HashMap;
const BLINKS: usize = 75;
// (value, blinks_remaining)
type Stone = (u64, usize);
pub fn day11() {
let mut lut: HashMap<Stone, usize> = HashMap::new();
let count: usize = crate::input(11)
.split_whitespace()
.map(|s| s.parse().unwrap())
.map(|v| blink((v, BLINKS), &mut lut))
.sum();
println!("Total Stones: {}", count);
}
fn blink(stone: Stone, lut: &mut HashMap<Stone, usize>) -> usize {
if let Some(&count) = lut.get(&stone) {
return count;
}
let blinked = blink_single(stone);
let count = match stone.1 {
1 => blinked.len(),
_ => blinked.iter().map(|&s| blink(s, lut)).sum(),
};
lut.insert(stone, count);
count
}
fn blink_single((v, b): Stone) -> Vec<Stone> {
match v.to_string().as_str() {
"0" => vec![(1, b - 1)],
s if s.len() % 2 == 0 => {
let mid = s.len() / 2;
vec![
(s[..mid].parse().unwrap(), b - 1),
(s[mid..].parse().unwrap(), b - 1),
]
}
_ => vec![(v * 2024, b - 1)],
}
}

View File

@ -16,10 +16,12 @@ mod day7;
mod day8;
#[allow(dead_code)]
mod day9;
#[allow(dead_code)]
mod day10;
mod day11;
fn main() {
day10::day10();
day11::day11();
}
pub fn input(day: u8) -> String {