solution: day 11

mostly stolen..........................
This commit is contained in:
YK 2024-12-12 04:30:39 +03:00
parent 6d308ac598
commit 9e25265af5
2 changed files with 85 additions and 1 deletions

View File

@ -8,7 +8,7 @@ pub mod d7;
pub mod d8; pub mod d8;
pub mod d9; pub mod d9;
pub mod d10; pub mod d10;
// pub mod d11; pub mod d11;
// pub mod d12; // pub mod d12;
// pub mod d13; // pub mod d13;
// pub mod d14; // pub mod d14;

84
src/days/d11.rs Normal file
View File

@ -0,0 +1,84 @@
use cached::proc_macro::cached;
use crate::prelude::*;
pub type I = Vec<u128>;
pub type O = u128;
fn _parse (data: &str) -> I {
data.trim().split_ascii_whitespace().map(|e| p!(e)).collect()
}
fn _solve (data: I, iterations: u32) -> O {
data.into_iter().map(|stone| count(stone, iterations)).sum()
}
fn count_digits (i: u128) -> u32 {
i.checked_ilog10().unwrap_or(0) + 1
}
#[cached]
fn count (stone: u128, blinks: u32) -> u128 {
match blinks {
0 => 1,
_ => next(stone).into_iter().flatten().map(|stone| count(stone, blinks - 1)).sum()
}
}
#[cached]
fn next (i: u128) -> [Option<u128>; 2] {
match i {
0 => [Some(1), None],
i if let a = count_digits(i) && a % 2 == 0 => {
let div = 10u128.pow(a / 2);
let left = i / div;
let right = i % div;
[Some(left), Some(right)]
},
i => [Some(i * 2024), None]
}
}
fn _silver (data: I) -> O {
_solve(data, 25)
}
fn _gold (data: I) -> O {
_solve(data, 75)
}
#[cfg(test)]
mod test {
use super::*;
fn read () -> I {
let data = inc!(11);
_parse(data)
}
#[test]
fn sample () {
let data = "125 17";
let data = _parse(data);
assert_eq!(_silver(data), 55312)
}
#[test]
fn silver () {
let data = read();
let ans = _silver(data);
assert_eq!(ans, 224529)
}
#[test]
fn gold () {
let data = read();
let ans = _gold(data);
assert_eq!(ans, 266820198587914)
}
}