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:
parent
65be0553a6
commit
dab87eeb22
1 changed files with 20 additions and 20 deletions
|
@ -1,4 +1,4 @@
|
|||
use std::{collections::VecDeque, str::FromStr};
|
||||
use std::str::FromStr;
|
||||
|
||||
use indicatif::ParallelProgressIterator;
|
||||
use memoize::memoize;
|
||||
|
@ -11,8 +11,8 @@ type Output2 = Output1;
|
|||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct Row {
|
||||
map: VecDeque<char>,
|
||||
damaged: VecDeque<usize>,
|
||||
map: Vec<char>,
|
||||
damaged: Vec<usize>,
|
||||
}
|
||||
|
||||
impl FromStr for Row {
|
||||
|
@ -29,7 +29,7 @@ impl FromStr for Row {
|
|||
}
|
||||
|
||||
#[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
|
||||
if damaged.is_empty() {
|
||||
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;
|
||||
|
||||
// skip the functional
|
||||
while let Some('.') = map.front() {
|
||||
while let Some('.') = map.last() {
|
||||
if in_damaged {
|
||||
return 0;
|
||||
}
|
||||
|
||||
in_damaged = false;
|
||||
map.pop_front();
|
||||
map.pop();
|
||||
}
|
||||
|
||||
if map.is_empty() || damaged.is_empty() {
|
||||
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 {
|
||||
arrangements(map.clone(), damaged.clone(), false)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
map[0] = '#';
|
||||
map[last_idx] = '#';
|
||||
let as_non_functional = arrangements(map, damaged, true);
|
||||
|
||||
as_functional + as_non_functional
|
||||
}
|
||||
'#' if damaged.is_empty() => 0,
|
||||
'#' => {
|
||||
damaged[0] -= 1;
|
||||
*damaged.last_mut().unwrap() -= 1;
|
||||
|
||||
map.pop_front();
|
||||
map.pop();
|
||||
|
||||
if damaged[0] == 0 {
|
||||
damaged.pop_front();
|
||||
if damaged[last_d_idx] == 0 {
|
||||
damaged.pop();
|
||||
|
||||
if let Some(&'#') = map.front() {
|
||||
if let Some(&'#') = map.last() {
|
||||
0
|
||||
} else if !map.is_empty() {
|
||||
map[0] = '.';
|
||||
map[last_idx - 1] = '.';
|
||||
arrangements(map, damaged, false)
|
||||
} else {
|
||||
map.pop_front();
|
||||
map.pop();
|
||||
arrangements(map, damaged, false)
|
||||
}
|
||||
} else {
|
||||
|
@ -99,10 +102,7 @@ fn arrangements(map: VecDeque<char>, damaged: VecDeque<usize>, in_damaged: bool)
|
|||
|
||||
impl Row {
|
||||
fn new(map: Vec<char>, damaged: Vec<usize>) -> Row {
|
||||
Row {
|
||||
map: VecDeque::from(map),
|
||||
damaged: VecDeque::from(damaged),
|
||||
}
|
||||
Row { map, damaged }
|
||||
}
|
||||
|
||||
fn arrangements(&mut self) -> usize {
|
||||
|
|
Loading…
Reference in a new issue