day(13): transpose instead of a copy-paste
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
b3e7037f33
commit
0a4cc4c031
1 changed files with 8 additions and 12 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue