1
0
Fork 0

day(13): transpose instead of a copy-paste

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-13 21:47:47 +01:00
parent b3e7037f33
commit 0a4cc4c031
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -19,17 +19,13 @@ impl From<&[String]> for Pattern {
}
impl Pattern {
fn check_vertically(&self, allowed_error: usize) -> Option<i32> {
(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<i32> {
@ -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