forgot to add day-12 formated

This commit is contained in:
Dylan Thies
2023-12-13 22:46:56 -05:00
parent 89e7315950
commit 5c07ce4825
2 changed files with 51 additions and 39 deletions

View File

@@ -46,8 +46,7 @@ impl Row {
.all(|(a, b)| (a == b || *a == SpringStatus::Unknown)) .all(|(a, b)| (a == b || *a == SpringStatus::Unknown))
}) })
.filter(|x| { .filter(|x| {
let (mut array, last, current_run) =x.iter() let (mut array, last, current_run) = x.iter().fold(
.fold(
(Vec::new(), SpringStatus::Working, 0_u32), (Vec::new(), SpringStatus::Working, 0_u32),
|(mut array, _last, mut current_run), x| { |(mut array, _last, mut current_run), x| {
if *x == SpringStatus::Failing { if *x == SpringStatus::Failing {
@@ -152,4 +151,3 @@ mod test {
assert_eq!(result, "21".to_string()); assert_eq!(result, "21".to_string());
} }
} }

View File

@@ -1,15 +1,15 @@
#![warn(clippy::all, clippy::pedantic)] #![warn(clippy::all, clippy::pedantic)]
use std::{iter::repeat, collections::HashMap}; use std::{collections::HashMap, iter::repeat};
use itertools::Itertools; use itertools::Itertools;
use nom::{ use nom::{
bytes::complete::{tag, is_a}, bytes::complete::{is_a, tag},
character::complete, character::complete,
multi::separated_list1, multi::separated_list1,
sequence::separated_pair, sequence::separated_pair,
IResult}; IResult,
};
struct Row { struct Row {
springs: String, springs: String,
@@ -22,7 +22,11 @@ impl Row {
self.dynamic_search(&mut cache, (0, 0, 0)) self.dynamic_search(&mut cache, (0, 0, 0))
} }
fn dynamic_search(&self, cache: &mut HashMap<(usize,usize,u32),usize>, search: (usize, usize, u32)) -> usize { fn dynamic_search(
&self,
cache: &mut HashMap<(usize, usize, u32), usize>,
search: (usize, usize, u32),
) -> usize {
let (data_index, group_index, group_size) = search; let (data_index, group_index, group_size) = search;
//are we at the end of the input //are we at the end of the input
if data_index >= self.springs.len() { if data_index >= self.springs.len() {
@@ -32,7 +36,9 @@ impl Row {
} }
//we haven't satisfied groups but the end is failing //we haven't satisfied groups but the end is failing
if group_index == self.broken_spans.len() - 1 && self.broken_spans[group_index] == group_size { if group_index == self.broken_spans.len() - 1
&& self.broken_spans[group_index] == group_size
{
return 1; return 1;
} }
return 0; return 0;
@@ -45,22 +51,26 @@ impl Row {
} }
//we failed to match the group //we failed to match the group
if group_index >= self.broken_spans.len() || self.broken_spans[group_index] != group_size{ if group_index >= self.broken_spans.len()
|| self.broken_spans[group_index] != group_size
{
return 0; return 0;
} }
//completed a group keep going //completed a group keep going
self.dynamic_search(cache, (data_index + 1, group_index + 1, 0)) self.dynamic_search(cache, (data_index + 1, group_index + 1, 0))
}, }
b'#' => { b'#' => {
//too many for our group //too many for our group
if group_index >= self.broken_spans.len() || group_size + 1 > self.broken_spans[group_index] { if group_index >= self.broken_spans.len()
|| group_size + 1 > self.broken_spans[group_index]
{
return 0; return 0;
} }
//haven't completed group yet keep looking //haven't completed group yet keep looking
self.dynamic_search(cache, (data_index + 1, group_index, group_size + 1)) self.dynamic_search(cache, (data_index + 1, group_index, group_size + 1))
}, }
b'?' => { b'?' => {
if let Some(res) = cache.get(&(data_index, group_index, group_size)).copied() { if let Some(res) = cache.get(&(data_index, group_index, group_size)).copied() {
return res; return res;
@@ -74,18 +84,23 @@ impl Row {
} }
//pretend to be damaged //pretend to be damaged
if group_index < self.broken_spans.len() && group_size < self.broken_spans[group_index] { if group_index < self.broken_spans.len()
perms += self.dynamic_search(cache, (data_index + 1, group_index, group_size +1)); && group_size < self.broken_spans[group_index]
{
perms +=
self.dynamic_search(cache, (data_index + 1, group_index, group_size + 1));
} }
//pretend to be undamage, thus ending a damaged group //pretend to be undamage, thus ending a damaged group
if group_index < self.broken_spans.len() && group_size == self.broken_spans[group_index] { if group_index < self.broken_spans.len()
&& group_size == self.broken_spans[group_index]
{
perms += self.dynamic_search(cache, (data_index + 1, group_index + 1, 0)); perms += self.dynamic_search(cache, (data_index + 1, group_index + 1, 0));
} }
cache.insert((data_index, group_index, group_size), perms); cache.insert((data_index, group_index, group_size), perms);
perms perms
}, }
_ => unreachable!(), _ => unreachable!(),
} }
} }
@@ -168,4 +183,3 @@ mod test {
assert_eq!(result, "525152".to_string()); assert_eq!(result, "525152".to_string());
} }
} }