day11: part 2 solution
This commit is contained in:
parent
462918b382
commit
83898ad6b4
@ -37,7 +37,7 @@ fn main() {
|
||||
println!("Total duration: {}", duration_format(duration1 + duration2));
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Hash)]
|
||||
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
|
||||
struct Stone {
|
||||
value: u64,
|
||||
}
|
||||
@ -82,9 +82,33 @@ fn problem1<T: BufRead>(mut input: Lines<T>) -> u64 {
|
||||
stones.iter().flat_map(|stone| stone.blink(25)).count() as u64
|
||||
}
|
||||
|
||||
fn count_blinks(stone: Stone, blinks: u64, cache: &mut HashMap<(u64, Stone), u64>) -> u64 {
|
||||
if cache.contains_key(&(blinks, stone)) {
|
||||
return cache[&(blinks, stone)];
|
||||
}
|
||||
let stones = stone.blink_once();
|
||||
let result = if blinks == 1 {
|
||||
stones.len() as u64
|
||||
} else {
|
||||
stones.iter().map(|s| count_blinks(*s, blinks - 1, cache)).sum()
|
||||
};
|
||||
cache.insert((blinks, stone), result);
|
||||
result
|
||||
}
|
||||
|
||||
// PROBLEM 2 solution
|
||||
fn problem2<T: BufRead>(mut input: Lines<T>) -> u64 {
|
||||
0
|
||||
let stones = input
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.split_ascii_whitespace()
|
||||
.map(|v| Stone {
|
||||
value: v.parse().unwrap(),
|
||||
})
|
||||
.collect_vec();
|
||||
let mut cache = HashMap::new();
|
||||
stones.iter().map(|s| count_blinks(*s, 75, &mut cache)).sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -103,6 +127,6 @@ mod tests {
|
||||
#[test]
|
||||
fn problem2_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
assert_eq!(problem2(c.lines()), 0);
|
||||
assert_eq!(problem2(c.lines()), 55312);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user