feat(day11): init
This commit is contained in:
parent
55833d081c
commit
bee665ddb4
2 changed files with 71 additions and 0 deletions
69
src/solutions/day_11.rs
Normal file
69
src/solutions/day_11.rs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use crate::common::{Answer, Solution};
|
||||||
|
|
||||||
|
pub struct Day11;
|
||||||
|
|
||||||
|
impl Solution for Day11 {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"Plutonian Pebbles"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_a(&self, input: &str) -> Answer {
|
||||||
|
compute_stones(input, 25).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_b(&self, input: &str) -> Answer {
|
||||||
|
compute_stones(input, 75).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute_stones(input: &str, iterations: u64) -> usize {
|
||||||
|
let mut stones = input
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|n| n.parse::<u64>().unwrap())
|
||||||
|
.counts();
|
||||||
|
|
||||||
|
for _ in 0..iterations {
|
||||||
|
stones = stones
|
||||||
|
.into_iter()
|
||||||
|
.flat_map(|(n, count)| {
|
||||||
|
if n == 0 {
|
||||||
|
vec![(1, count)]
|
||||||
|
} else {
|
||||||
|
let s = n.to_string();
|
||||||
|
if s.len() % 2 == 0 {
|
||||||
|
let (one, two) = s.split_at(s.len() / 2);
|
||||||
|
vec![
|
||||||
|
(one.parse::<u64>().unwrap(), count),
|
||||||
|
(two.parse::<u64>().unwrap(), count),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
vec![(n * 2024, count)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into_grouping_map()
|
||||||
|
.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
stones.values().sum::<usize>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::Day11;
|
||||||
|
use crate::common::Solution;
|
||||||
|
|
||||||
|
const INPUT: &str = "125 17";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part_a() {
|
||||||
|
assert_eq!(Day11.part_a(INPUT), 55312.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part_b() {
|
||||||
|
assert_eq!(Day11.part_b(INPUT), 65601038650482u64.into());
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ mod day_07;
|
||||||
mod day_08;
|
mod day_08;
|
||||||
mod day_09;
|
mod day_09;
|
||||||
mod day_10;
|
mod day_10;
|
||||||
|
mod day_11;
|
||||||
|
|
||||||
pub const SOLUTIONS: &[&dyn Solution] = &[
|
pub const SOLUTIONS: &[&dyn Solution] = &[
|
||||||
&day_01::Day01,
|
&day_01::Day01,
|
||||||
|
@ -22,4 +23,5 @@ pub const SOLUTIONS: &[&dyn Solution] = &[
|
||||||
&day_08::Day08,
|
&day_08::Day08,
|
||||||
&day_09::Day09,
|
&day_09::Day09,
|
||||||
&day_10::Day10,
|
&day_10::Day10,
|
||||||
|
&day_11::Day11,
|
||||||
];
|
];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue