diff --git a/src/bin/day13.rs b/src/bin/day13.rs index 3bba81c..8cae9b3 100644 --- a/src/bin/day13.rs +++ b/src/bin/day13.rs @@ -19,17 +19,13 @@ impl From<&[String]> for Pattern { } impl Pattern { - fn check_vertically(&self, allowed_error: usize) -> Option { - (0..self.map[0].len() - 1) - .find(|&x| { - let d = min(x + 1, self.map[0].len() - x - 1); - - iproduct!(0..self.map.len(), (1..=d)) - .filter(|&(y, dx)| self.map[y][x + 1 - dx] != self.map[y][x + dx]) - .count() - == allowed_error - }) - .map(|x| (x + 1) as i32) + fn transpose(&self) -> Pattern { + let (m, n) = (self.map.len(), self.map[0].len()); + Pattern { + map: (0..n) + .map(|x| (0..m).map(|y| self.map[y][x]).collect_vec()) + .collect_vec(), + } } fn check_horizontally(&self, allowed_error: usize) -> Option { @@ -46,7 +42,7 @@ impl Pattern { } fn score(&self, allowed_error: usize) -> i32 { - if let Some(s) = self.check_vertically(allowed_error) { + if let Some(s) = self.transpose().check_horizontally(allowed_error) { s } else if let Some(s) = self.check_horizontally(allowed_error) { 100 * s