Solved day 11
This commit is contained in:
1
input/day11.txt
Normal file
1
input/day11.txt
Normal file
@@ -0,0 +1 @@
|
||||
6563348 67 395 0 6 4425 89567 739318
|
||||
78
src/day11.rs
Normal file
78
src/day11.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use std::collections::HashMap;
|
||||
use num::Integer;
|
||||
use crate::day_solver::DaySolver;
|
||||
#[cfg(test)]
|
||||
use crate::util::read_file;
|
||||
|
||||
pub struct Day11 {
|
||||
pebbles: Vec<u64>
|
||||
}
|
||||
|
||||
impl Day11 {
|
||||
|
||||
pub fn create(input: String) -> Self {
|
||||
Day11 {
|
||||
pebbles: input.split_whitespace().filter_map(|n| n.parse::<u64>().ok()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn split_digits(n: u64) -> (u64, u64) {
|
||||
let digits = n.ilog10() + 1;
|
||||
assert!(digits.is_even());
|
||||
let factor = 10u64.pow(digits / 2);
|
||||
(n / factor, n % factor)
|
||||
}
|
||||
|
||||
fn count_pebbles_after_blinks(pebble: u64, blinks: usize, cache: &mut HashMap<(u64, usize), u64>) -> u64 {
|
||||
if blinks == 0 { return 1 };
|
||||
let cache_key = (pebble, blinks);
|
||||
if let Some(cached) = cache.get(&cache_key) {
|
||||
return cached.to_owned();
|
||||
}
|
||||
|
||||
let res = if pebble == 0 {
|
||||
Self::count_pebbles_after_blinks(1, blinks - 1, cache)
|
||||
} else if pebble.ilog10().is_odd() {
|
||||
let (left, right) = Self::split_digits(pebble.to_owned());
|
||||
Self::count_pebbles_after_blinks(left, blinks - 1, cache) + Self::count_pebbles_after_blinks(right, blinks - 1, cache)
|
||||
} else {
|
||||
Self::count_pebbles_after_blinks(pebble * 2024, blinks - 1, cache)
|
||||
};
|
||||
|
||||
cache.insert(cache_key, res);
|
||||
res
|
||||
}
|
||||
|
||||
fn count_total_pebbles_after_blinks(pebbles: &[u64], blinks: usize) -> u64 {
|
||||
pebbles.iter().map(|p| Self::count_pebbles_after_blinks(p.to_owned(), blinks, &mut HashMap::new())).sum::<u64>()
|
||||
}
|
||||
}
|
||||
|
||||
impl DaySolver for Day11 {
|
||||
|
||||
|
||||
fn solve_part1(&mut self) -> String {
|
||||
Self::count_total_pebbles_after_blinks(&self.pebbles.clone(), 25).to_string()
|
||||
}
|
||||
|
||||
fn solve_part2(&mut self) -> String {
|
||||
Self::count_total_pebbles_after_blinks(&self.pebbles.clone(), 75).to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1_basic() {
|
||||
assert_eq!(7, Day11::count_total_pebbles_after_blinks(&vec![0, 1, 10, 99, 999], 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
let mut day = Day11::create("125 17".into());
|
||||
assert_eq!("55312", day.solve_part1());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
let mut day = Day11::create(read_file("input/day11_example.txt"));
|
||||
assert_eq!("EXAMPLE_ANSWER", day.solve_part2());
|
||||
}
|
||||
@@ -2,6 +2,7 @@ extern crate core;
|
||||
|
||||
use std::time::Instant;
|
||||
use crate::day10::Day10;
|
||||
use crate::day11::Day11;
|
||||
use crate::day1::Day1;
|
||||
use crate::day2::Day2;
|
||||
use crate::day3::Day3;
|
||||
@@ -26,6 +27,7 @@ mod day7;
|
||||
mod day8;
|
||||
mod day9;
|
||||
mod day10;
|
||||
mod day11;
|
||||
|
||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||
|
||||
@@ -107,7 +109,7 @@ fn build_day_solver(day: u8, input: String) -> Option<Box<dyn DaySolver>> {
|
||||
8 => Some(Box::new(Day8::create(input))),
|
||||
9 => Some(Box::new(Day9::create(input))),
|
||||
10 => Some(Box::new(Day10::create(input))),
|
||||
// 11 => Some(Box::new(Day11::create(input))),
|
||||
11 => Some(Box::new(Day11::create(input))),
|
||||
// 12 => Some(Box::new(Day12::create(input))),
|
||||
_ => None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user