Skip to content

Commit b60ebdc

Browse files
committed
Day 13, part 1: Actually verify before making assumptions
1 parent 74544a2 commit b60ebdc

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

src/day13.rs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Pattern {
1616
fn find_mirror_column(&self, print: bool) -> impl Iterator<Item = usize> {
1717
let mut result = vec![];
1818
let mut last = None;
19-
for (i, c) in self.columns.iter().enumerate() {
19+
'outer: for (i, c) in self.columns.iter().enumerate() {
2020
if print {
2121
println!("find_mirror_column: i = {}, c = {}, last = {:?}", i, c, last);
2222
}
@@ -25,6 +25,17 @@ impl Pattern {
2525
if print {
2626
println!("find_mirror_column: found potential mirror line at i = {}", i);
2727
}
28+
29+
// Verify: The patterns must be the same now between this column and the previous column.
30+
for row in 0..self.pattern.len() {
31+
if self.pattern[row][i] != self.pattern[row][i - 1] {
32+
if print {
33+
println!("find_mirror_column: not matching pattern, i = {}, row = {}", i, row);
34+
}
35+
continue 'outer;
36+
}
37+
}
38+
2839
// Found a potential mirror line
2940
// Iterate outwards from here
3041
let mut found = true;
@@ -73,15 +84,23 @@ impl Pattern {
7384
fn find_mirror_row(&self, print: bool) -> impl Iterator<Item = usize> {
7485
let mut result = vec![];
7586
let mut last = None;
76-
for (i, c) in self.rows.iter().enumerate() {
87+
'outer: for (i, c) in self.rows.iter().enumerate() {
7788
if print {
78-
println!("find_mirror_row: i = {}, c = {}, last = {:?}", i, c, last);
89+
println!("find_mirror_row: row = \"{}\", i = {}, c = {}, last = {:?}", self.pattern[i].iter().collect::<String>(), i, c, last);
7990
}
8091
if let Some(l) = last {
8192
if l == *c {
8293
if print {
8394
println!("find_mirror_row: found potential mirror line at i = {}", i);
8495
}
96+
// Verify: The patterns must be the same now between this row and the previous row.
97+
if self.pattern[i] != self.pattern[i - 1] {
98+
if print {
99+
println!("find_mirror_row: not matching pattern, i = {}", i);
100+
}
101+
continue 'outer;
102+
}
103+
85104
// Found a potential mirror line
86105
// Iterate outwards from here
87106
let mut found = true;
@@ -131,9 +150,12 @@ impl Pattern {
131150
// Search until we find the same column twice: If it is a mirror line, then we can extend from there
132151
// and compare the columns. If both comparison directions hit the border, the line is a mirror, otherwise
133152
// proceed.
134-
self.find_mirror_column(print).map(|column| (Some(column), None)).into_iter().chain(
135-
self.find_mirror_row(print).map(|row| (None, Some(row))).into_iter()
153+
self.find_mirror_row(print).map(|row| (None, Some(row))).chain(
154+
self.find_mirror_column(print).map(|column| (Some(column), None))
136155
)
156+
// self.find_mirror_column(print).map(|column| (Some(column), None)).chain(
157+
// self.find_mirror_row(print).map(|row| (None, Some(row)))
158+
// )
137159
}
138160
}
139161

@@ -315,4 +337,40 @@ mod tests {
315337
assert_eq!(pattern.find_mirror(true).collect::<Vec<_>>(), vec![(None, Some(4))]);
316338
print_pattern_with_row_indicator(&pattern, 4)
317339
}
340+
341+
#[test]
342+
fn example1_test1() {
343+
const INPUT: &str = "##...##..
344+
.##.#..#.
345+
.#.......
346+
...#.##.#
347+
##..#..#.
348+
.##..##..
349+
.#...##..
350+
#.#######
351+
....####.
352+
..##.##.#
353+
..##.##.#
354+
....####.
355+
#.#######
356+
.#...##..
357+
.##..##..
358+
#...#..#.
359+
...#.##.#";
360+
let pattern = INPUT.parse::<Pattern>().unwrap();
361+
assert_eq!(pattern.find_mirror(true).collect::<Vec<_>>(), vec![(Some(6), None)]);
362+
}
363+
364+
#[test]
365+
fn example1_test2() {
366+
const INPUT: &str = "....#....##....
367+
.##..##.####.##
368+
#..#.#...##...#
369+
.##.####....###
370+
#..#.##.####.##
371+
#..###.#.##.#.#
372+
.##.##.##..####";
373+
let pattern = INPUT.parse::<Pattern>().unwrap();
374+
assert_eq!(pattern.find_mirror(true).collect::<Vec<_>>(), vec![(Some(2), None)]);
375+
}
318376
}

0 commit comments

Comments
 (0)