mirror of
https://github.com/Rhelvetican/aoc2024.git
synced 2024-12-22 22:51:38 +00:00
day 11
This commit is contained in:
parent
68e14d9ef6
commit
925a735a8b
3 changed files with 59 additions and 7 deletions
|
@ -32,6 +32,7 @@ fn main() -> Result<()> {
|
|||
8 => AocDayEightSolution,
|
||||
9 => AocDayNineSolution,
|
||||
10 => AocDayTenSolution,
|
||||
11 => AocDayElevenSolution,
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,8 +1,58 @@
|
|||
use std::{fs::read_to_string, path::Path};
|
||||
use std::{collections::HashMap, fs::read_to_string, path::Path};
|
||||
|
||||
use super::AocSolution;
|
||||
use crate::utils::Result;
|
||||
|
||||
struct Stones {
|
||||
stones: Vec<u64>,
|
||||
}
|
||||
|
||||
impl Stones {
|
||||
fn new(src: &str) -> Self {
|
||||
Self {
|
||||
stones: src
|
||||
.split_whitespace()
|
||||
.filter_map(|num| num.parse().ok())
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn blinks(&self, cnt: u8) -> u64 {
|
||||
fn _inner(cache: &mut HashMap<(u64, u8), u64>, stone: u64, cnt: u8) -> u64 {
|
||||
if cnt == 0 {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if let Some(&val) = cache.get(&(stone, cnt)) {
|
||||
return val;
|
||||
}
|
||||
|
||||
let next = match stone {
|
||||
0 => _inner(cache, 1, cnt - 1),
|
||||
n => {
|
||||
let digits = n.ilog10() + 1;
|
||||
if digits % 2 == 0 {
|
||||
let pow = 10u64.pow(digits / 2);
|
||||
_inner(cache, stone / pow, cnt - 1) + _inner(cache, stone % pow, cnt - 1)
|
||||
} else {
|
||||
_inner(cache, stone * 2024, cnt - 1)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
cache.insert((stone, cnt), next);
|
||||
next
|
||||
}
|
||||
|
||||
let mut cache = HashMap::new();
|
||||
self.stones
|
||||
.iter()
|
||||
.map(|&x| _inner(&mut cache, x, cnt))
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct AocDayElevenSolution;
|
||||
|
||||
impl AocSolution for AocDayElevenSolution {
|
||||
|
@ -11,15 +61,17 @@ impl AocSolution for AocDayElevenSolution {
|
|||
fn get_input(&self, path: Option<&Path>) -> Result<String> {
|
||||
Ok(match path {
|
||||
Some(p) => read_to_string(p)?,
|
||||
None => read_to_string("./input/day_day_11.txt")?,
|
||||
None => read_to_string("./input/day_11.txt")?,
|
||||
})
|
||||
}
|
||||
|
||||
fn part_one(&self, input: &str) -> Result<Self::Output> {
|
||||
todo!()
|
||||
let stones = Stones::new(input);
|
||||
Ok(stones.blinks(25))
|
||||
}
|
||||
|
||||
fn part_two(&self, input: &str) -> Result<Self::Output> {
|
||||
todo!()
|
||||
let stones = Stones::new(input);
|
||||
Ok(stones.blinks(75))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,9 +9,8 @@ macro_rules! define_solution {
|
|||
}
|
||||
|
||||
define_solution!(
|
||||
day_1, day_2, day_3, day_4, day_5, day_6, day_7, day_8, day_9,
|
||||
day_10,
|
||||
// day_11,
|
||||
day_1, day_2, day_3, day_4, day_5, day_6, day_7, day_8, day_9, day_10,
|
||||
day_11,
|
||||
// day_12,
|
||||
// day_13,
|
||||
// day_14,
|
||||
|
|
Loading…
Reference in a new issue