Compare commits

..

2 Commits

Author SHA1 Message Date
Matej Janezic 0000015022
solution: day 11 2024-12-11 06:37:27 +01:00
Matej Janezic 000001408c
solution: day 10 2024-12-11 06:37:19 +01:00
3 changed files with 81 additions and 2 deletions

1
data/examples/11.txt Normal file
View File

@ -0,0 +1 @@
125 17

View File

@ -74,10 +74,16 @@ mod tests {
use super::*;
#[test]
fn test_part_one() {
assert_eq!(part_one(&aoc::template::read_file("examples", 10)), Some(36));
assert_eq!(
part_one(&aoc::template::read_file("examples", 10)),
Some(36)
);
}
#[test]
fn test_part_two() {
assert_eq!(part_two(&aoc::template::read_file("examples", 10)), Some(81));
assert_eq!(
part_two(&aoc::template::read_file("examples", 10)),
Some(81)
);
}
}

72
src/bin/11.rs Normal file
View File

@ -0,0 +1,72 @@
use std::mem::swap;
use hashbrown::HashMap;
use itertools::Itertools;
fn blink(map: &HashMap<usize, usize>, nmap: &mut HashMap<usize, usize>) {
for (&k, &v) in map.iter() {
match k {
0 => *nmap.entry(1).or_default() += v,
x if (x.ilog10() + 1) % 2 == 0 => {
let xlen = x.ilog10() + 1;
let xhalf = 10_usize.pow(xlen / 2);
*nmap.entry(x / xhalf).or_default() += v;
*nmap.entry(x % xhalf).or_default() += v;
}
x => *nmap.entry(x * 2024).or_default() += v,
}
}
}
fn solve(input: &str, blinks: usize) -> usize {
let v = input
.split(" ")
.filter_map(|x| x.parse().ok())
.collect_vec();
let mut map = HashMap::<usize, usize>::new();
for vv in v {
*map.entry(vv).or_default() += 1;
}
let mut nmap = HashMap::<usize, usize>::new();
for _ in 0..blinks {
blink(&map, &mut nmap);
map.clear();
swap(&mut map, &mut nmap);
}
map.values().sum()
}
pub fn part_one(input: &str) -> Option<usize> {
solve(input, 25).into()
}
pub fn part_two(input: &str) -> Option<usize> {
solve(input, 75).into()
}
aoc::solution!(11);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
assert_eq!(
part_one(&aoc::template::read_file("examples", 11)),
Some(55312)
);
}
#[test]
fn test_part_two() {
assert_eq!(
part_two(&aoc::template::read_file("examples", 11)),
Some(65601038650482)
);
}
}