-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.rs
98 lines (90 loc) · 2.77 KB
/
day04.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use aoc_lib::{
answer::Answer,
directions::{Advance, Direction, ExtendedCardinal},
matrix::Matrix,
solution::Solution,
vec2::Vec2,
};
pub struct Day4;
impl Solution for Day4 {
fn part_a(&self, input: &[String]) -> Answer {
let matrix = Matrix::from_chars(input);
let mut count = 0;
for y in 0..matrix.rows {
for x in 0..matrix.cols {
let start = Vec2::new(x, y);
if matrix[start] != 'X' {
continue;
}
'dir: for dir in ExtendedCardinal::all_clockwise() {
let mut pos = Vec2::<isize>::from(start);
for expected in ['M', 'A', 'S'] {
let next = dir.advance(pos);
if Some(&expected) != matrix.get(&next) {
continue 'dir;
}
pos = next;
}
count += 1;
}
}
}
count.into()
}
fn part_b(&self, input: &[String]) -> Answer {
let matrix = Matrix::from_chars(input);
let mut count = 0;
for y in 0..matrix.rows {
for x in 0..matrix.cols {
let start = Vec2::new(x, y);
if matrix[start] != 'A' {
continue;
}
if is_valid_xmas_pattern(&matrix, &start) {
count += 1;
}
}
}
count.into()
}
}
fn is_valid_xmas_pattern(matrix: &Matrix<char>, pos: &Vec2<usize>) -> bool {
let pos = Vec2::<isize>::from(pos);
let dirs: [[ExtendedCardinal; 2]; 2] = [
[ExtendedCardinal::NorthWest, ExtendedCardinal::SouthEast],
[ExtendedCardinal::SouthWest, ExtendedCardinal::NorthEast],
];
for diag in dirs {
let (mut m, mut s) = (false, false);
for dir in diag {
let Some(&char) = matrix.get(&dir.advance(pos)) else {
return false;
};
m ^= char == 'M';
s ^= char == 'S';
}
if !(m && s) {
return false;
}
}
true
}
#[cfg(test)]
mod test {
use aoc_lib::{answer::Answer, input, solution::Solution};
use super::Day4;
#[test]
fn test_a() {
let input =
input::read_file(&format!("{}day_04_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day4.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(18), answer);
}
#[test]
fn test_b() {
let input =
input::read_file(&format!("{}day_04_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day4.part_b(&input);
assert_eq!(<i32 as Into<Answer>>::into(9), answer);
}
}