1
0
Fork 0

day(12): switch back to the ‹Vec›

Stack is enough, we were using just ‹.front()› and ‹.pop_front()› on the
‹VecDeque›.

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-12 15:36:19 +01:00
parent 65be0553a6
commit dab87eeb22
No account linked to committer's email address

View file

@ -1,4 +1,4 @@
use std::{collections::VecDeque, str::FromStr}; use std::str::FromStr;
use indicatif::ParallelProgressIterator; use indicatif::ParallelProgressIterator;
use memoize::memoize; use memoize::memoize;
@ -11,8 +11,8 @@ type Output2 = Output1;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
struct Row { struct Row {
map: VecDeque<char>, map: Vec<char>,
damaged: VecDeque<usize>, damaged: Vec<usize>,
} }
impl FromStr for Row { impl FromStr for Row {
@ -29,7 +29,7 @@ impl FromStr for Row {
} }
#[memoize] #[memoize]
fn arrangements(map: VecDeque<char>, damaged: VecDeque<usize>, in_damaged: bool) -> usize { fn arrangements(map: Vec<char>, damaged: Vec<usize>, in_damaged: bool) -> usize {
// all damaged have been placed // all damaged have been placed
if damaged.is_empty() { if damaged.is_empty() {
return if map.iter().all(|&c| c == '.' || c == '?') { return if map.iter().all(|&c| c == '.' || c == '?') {
@ -44,49 +44,52 @@ fn arrangements(map: VecDeque<char>, damaged: VecDeque<usize>, in_damaged: bool)
let mut in_damaged = in_damaged; let mut in_damaged = in_damaged;
// skip the functional // skip the functional
while let Some('.') = map.front() { while let Some('.') = map.last() {
if in_damaged { if in_damaged {
return 0; return 0;
} }
in_damaged = false; in_damaged = false;
map.pop_front(); map.pop();
} }
if map.is_empty() || damaged.is_empty() { if map.is_empty() || damaged.is_empty() {
return 0; return 0;
} }
match map[0] { let last_idx = map.len() - 1;
let last_d_idx = damaged.len() - 1;
match &map.last().unwrap() {
'?' => { '?' => {
map[0] = '.'; map[last_idx] = '.';
let as_functional = if !in_damaged { let as_functional = if !in_damaged {
arrangements(map.clone(), damaged.clone(), false) arrangements(map.clone(), damaged.clone(), false)
} else { } else {
0 0
}; };
map[0] = '#'; map[last_idx] = '#';
let as_non_functional = arrangements(map, damaged, true); let as_non_functional = arrangements(map, damaged, true);
as_functional + as_non_functional as_functional + as_non_functional
} }
'#' if damaged.is_empty() => 0, '#' if damaged.is_empty() => 0,
'#' => { '#' => {
damaged[0] -= 1; *damaged.last_mut().unwrap() -= 1;
map.pop_front(); map.pop();
if damaged[0] == 0 { if damaged[last_d_idx] == 0 {
damaged.pop_front(); damaged.pop();
if let Some(&'#') = map.front() { if let Some(&'#') = map.last() {
0 0
} else if !map.is_empty() { } else if !map.is_empty() {
map[0] = '.'; map[last_idx - 1] = '.';
arrangements(map, damaged, false) arrangements(map, damaged, false)
} else { } else {
map.pop_front(); map.pop();
arrangements(map, damaged, false) arrangements(map, damaged, false)
} }
} else { } else {
@ -99,10 +102,7 @@ fn arrangements(map: VecDeque<char>, damaged: VecDeque<usize>, in_damaged: bool)
impl Row { impl Row {
fn new(map: Vec<char>, damaged: Vec<usize>) -> Row { fn new(map: Vec<char>, damaged: Vec<usize>) -> Row {
Row { Row { map, damaged }
map: VecDeque::from(map),
damaged: VecDeque::from(damaged),
}
} }
fn arrangements(&mut self) -> usize { fn arrangements(&mut self) -> usize {