mirror of
https://github.com/Noratrieb/advent-of-code.git
synced 2026-01-14 17:45:02 +01:00
part 2
This commit is contained in:
parent
3322b4258c
commit
57c0a55b30
1 changed files with 45 additions and 4 deletions
|
|
@ -13,7 +13,7 @@ helper::define_variants! {
|
||||||
basic => crate::part1;
|
basic => crate::part1;
|
||||||
}
|
}
|
||||||
part2 {
|
part2 {
|
||||||
basic => crate::part2;
|
basic => crate::part2,sample_count=10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,8 +72,49 @@ fn part1(input: &str) -> u64 {
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(_input: &str) -> u64 {
|
fn part2(input: &str) -> u64 {
|
||||||
0
|
let mut cache = FxHashMap::default();
|
||||||
|
|
||||||
|
fn expanded_count(cache: &mut FxHashMap<(u64, u64), u64>, stone: u64, blinks: u64) -> u64 {
|
||||||
|
let result_count = if blinks == 0 {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
if let Some(result) = cache.get(&(stone, blinks)) {
|
||||||
|
return *result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if stone == 0 {
|
||||||
|
expanded_count(cache, 1, blinks - 1)
|
||||||
|
} else if (stone.ilog10() + 1) % 2 == 0 {
|
||||||
|
// 3456 -> ilog=3, split = 100 (10^2)
|
||||||
|
// 123456 -> ilog=5, split = 1000 (10^3)
|
||||||
|
let split = 10_u64.pow((stone.ilog10() / 2) + 1);
|
||||||
|
let lhs = stone / split;
|
||||||
|
let rhs = stone % split;
|
||||||
|
|
||||||
|
let lhs_count = expanded_count(cache, lhs, blinks - 1);
|
||||||
|
let rhs_count = expanded_count(cache, rhs, blinks - 1);
|
||||||
|
lhs_count + rhs_count
|
||||||
|
} else {
|
||||||
|
expanded_count(cache, stone * 2024, blinks - 1)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
cache.insert((stone, blinks), result_count);
|
||||||
|
|
||||||
|
result_count
|
||||||
|
}
|
||||||
|
|
||||||
|
let is_example = input.len() < 10;
|
||||||
|
|
||||||
|
let total_blinks = if is_example { 6 } else { 75 };
|
||||||
|
|
||||||
|
input
|
||||||
|
.trim()
|
||||||
|
.split(" ")
|
||||||
|
.map(parse_unwrap)
|
||||||
|
.map(|x| expanded_count(&mut cache, x, total_blinks))
|
||||||
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
helper::tests! {
|
helper::tests! {
|
||||||
|
|
@ -83,7 +124,7 @@ helper::tests! {
|
||||||
default => 204022;
|
default => 204022;
|
||||||
}
|
}
|
||||||
part2 {
|
part2 {
|
||||||
small => 0;
|
small => 22;
|
||||||
default => 0;
|
default => 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue