Compare commits
2 Commits
9490e3a4de
...
86202a10e6
Author | SHA1 | Date | |
---|---|---|---|
86202a10e6 | |||
250417760b |
|
@ -1,5 +1,8 @@
|
|||
#![feature(test)]
|
||||
use std::{collections::{HashMap, hash_map::DefaultHasher}, hash::{Hash, Hasher}};
|
||||
use std::{
|
||||
collections::{hash_map::DefaultHasher, HashMap},
|
||||
hash::{Hash, Hasher},
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use aoc::Solver;
|
||||
|
@ -76,7 +79,12 @@ impl std::fmt::Display for Spring {
|
|||
}
|
||||
|
||||
// The cache is optional as is slows down part1 by a lot
|
||||
fn count_valid(springs: &mut [Spring], list: &[usize], damaged_chain: usize, cache: &mut Option<&mut Cache>) -> usize {
|
||||
fn count_valid(
|
||||
springs: &mut [Spring],
|
||||
list: &[usize],
|
||||
damaged_chain: usize,
|
||||
cache: &mut Option<&mut Cache>,
|
||||
) -> usize {
|
||||
let hash = if let Some(cache) = cache {
|
||||
// Calculate the hash manually, otherwise we end up with weird borrows or we have to clone
|
||||
// (which is slow)
|
||||
|
@ -97,7 +105,12 @@ fn count_valid(springs: &mut [Spring], list: &[usize], damaged_chain: usize, cac
|
|||
|
||||
// We reached the end of the list, no further processing is possible
|
||||
let count = if list.is_empty() {
|
||||
if springs.iter().filter(|&spring| spring == &Spring::Damaged).count() > 0 {
|
||||
if springs
|
||||
.iter()
|
||||
.filter(|&spring| spring == &Spring::Damaged)
|
||||
.count()
|
||||
> 0
|
||||
{
|
||||
// There are still damaged springs remaining
|
||||
0
|
||||
} else {
|
||||
|
@ -139,10 +152,15 @@ fn count_valid(springs: &mut [Spring], list: &[usize], damaged_chain: usize, cac
|
|||
|
||||
// Return the sum of both branches
|
||||
a + b
|
||||
},
|
||||
}
|
||||
Spring::Damaged => {
|
||||
// Add to the damaged chain
|
||||
count_valid(&mut springs[1..], list, damaged_chain+1, cache)
|
||||
if damaged_chain + 1 > list[0] {
|
||||
// The chain is to long
|
||||
0
|
||||
} else {
|
||||
// Add to the damaged chain
|
||||
count_valid(&mut springs[1..], list, damaged_chain + 1, cache)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -194,10 +212,23 @@ impl aoc::Solver for Day {
|
|||
.collect::<Vec<_>>(),
|
||||
)
|
||||
})
|
||||
.map(|(springs, list)| (
|
||||
[springs.clone(), vec![Spring::Unknown], springs.clone(), vec![Spring::Unknown], springs.clone(), vec![Spring::Unknown], springs.clone(), vec![Spring::Unknown], springs].concat(),
|
||||
[list.clone(), list.clone(), list.clone(), list.clone(), list].concat()
|
||||
))
|
||||
.map(|(springs, list)| {
|
||||
(
|
||||
[
|
||||
springs.clone(),
|
||||
vec![Spring::Unknown],
|
||||
springs.clone(),
|
||||
vec![Spring::Unknown],
|
||||
springs.clone(),
|
||||
vec![Spring::Unknown],
|
||||
springs.clone(),
|
||||
vec![Spring::Unknown],
|
||||
springs,
|
||||
]
|
||||
.concat(),
|
||||
[list.clone(), list.clone(), list.clone(), list.clone(), list].concat(),
|
||||
)
|
||||
})
|
||||
.map(|(mut springs, list)| {
|
||||
let mut cache = HashMap::new();
|
||||
count_valid(&mut springs, &list, 0, &mut Some(&mut cache))
|
||||
|
|
Loading…
Reference in New Issue
Block a user