From dab87eeb222de17879506c9729686980e7828a4a Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 12 Dec 2023 15:36:19 +0100 Subject: [PATCH] =?UTF-8?q?day(12):=20switch=20back=20to=20the=20=E2=80=B9?= =?UTF-8?q?Vec=E2=80=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stack is enough, we were using just ‹.front()› and ‹.pop_front()› on the ‹VecDeque›. Signed-off-by: Matej Focko --- src/bin/day12.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/bin/day12.rs b/src/bin/day12.rs index 37ea802..bed927c 100644 --- a/src/bin/day12.rs +++ b/src/bin/day12.rs @@ -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, - damaged: VecDeque, + map: Vec, + damaged: Vec, } impl FromStr for Row { @@ -29,7 +29,7 @@ impl FromStr for Row { } #[memoize] -fn arrangements(map: VecDeque, damaged: VecDeque, in_damaged: bool) -> usize { +fn arrangements(map: Vec, damaged: Vec, 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, damaged: VecDeque, 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, damaged: VecDeque, in_damaged: bool) impl Row { fn new(map: Vec, damaged: Vec) -> Row { - Row { - map: VecDeque::from(map), - damaged: VecDeque::from(damaged), - } + Row { map, damaged } } fn arrangements(&mut self) -> usize {