1
0
Fork 0

day(12): refactor ‹unfold› and test it

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-12 15:28:07 +01:00
parent 63a41764ec
commit dc2df6d7fb
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -9,7 +9,7 @@ use aoc_2023::*;
type Output1 = usize; type Output1 = usize;
type Output2 = Output1; type Output2 = Output1;
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
struct Row { struct Row {
map: VecDeque<char>, map: VecDeque<char>,
damaged: VecDeque<usize>, damaged: VecDeque<usize>,
@ -118,9 +118,12 @@ impl Row {
fn unfold(&self) -> Row { fn unfold(&self) -> Row {
Row::new( Row::new(
vec![self.map.iter().collect::<String>(); 5] self.map
.join("?") .iter()
.chars() .cloned()
.chain(['?'])
.cycle()
.take(5 * (self.map.len() + 1) - 1)
.collect_vec(), .collect_vec(),
self.damaged self.damaged
.iter() .iter()
@ -166,6 +169,29 @@ test_sample!(day_12, Day12, 21, 525152);
mod day_12_extended { mod day_12_extended {
use super::*; use super::*;
#[test]
fn unfold_1() {
let row = Row::new(".#".chars().collect_vec(), vec![1]);
assert_eq!(
row.unfold(),
Row::new(".#?.#?.#?.#?.#".chars().collect_vec(), vec![1, 1, 1, 1, 1])
);
}
#[test]
fn unfold_2() {
let row = Row::new("???.###".chars().collect_vec(), vec![1, 1, 3]);
assert_eq!(
row.unfold(),
Row::new(
"???.###????.###????.###????.###????.###"
.chars()
.collect_vec(),
vec![1, 1, 3, 1, 1, 3, 1, 1, 3, 1, 1, 3, 1, 1, 3]
)
);
}
#[test] #[test]
fn arrangements_1() { fn arrangements_1() {
let mut row = Row::new("???.###".chars().collect_vec(), vec![1, 1, 3]); let mut row = Row::new("???.###".chars().collect_vec(), vec![1, 1, 3]);