Compare commits

..

1 Commits

Author SHA1 Message Date
9490e3a4de
2023 - Day 12 2023-12-12 14:35:19 +01:00

View File

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