generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.rs
114 lines (104 loc) · 3.52 KB
/
04.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
advent_of_code::solution!(4);
pub fn part_one(input: &str) -> Option<u32> {
let l: Vec<Vec<char>> = input.lines().map(|l| l.chars().collect()).collect();
let row_lim = l.len();
let col_lim = l[0].len();
let mut c = 0;
// Find - matches
for row in 0..row_lim {
for col in 0..col_lim {
let look_right = col < col_lim - 3;
let look_down = row < row_lim - 3;
// Check -
if look_right
&& ((l[row][col] == 'X'
&& l[row][col + 1] == 'M'
&& l[row][col + 2] == 'A'
&& l[row][col + 3] == 'S')
|| (l[row][col] == 'S'
&& l[row][col + 1] == 'A'
&& l[row][col + 2] == 'M'
&& l[row][col + 3] == 'X'))
{
c += 1;
}
// Check |
if look_down
&& ((l[row][col] == 'X'
&& l[row + 1][col] == 'M'
&& l[row + 2][col] == 'A'
&& l[row + 3][col] == 'S')
|| (l[row][col] == 'S'
&& l[row + 1][col] == 'A'
&& l[row + 2][col] == 'M'
&& l[row + 3][col] == 'X'))
{
c += 1;
}
// Check \
if look_down
&& look_right
&& ((l[row][col] == 'X'
&& l[row + 1][col + 1] == 'M'
&& l[row + 2][col + 2] == 'A'
&& l[row + 3][col + 3] == 'S')
|| (l[row][col] == 'S'
&& l[row + 1][col + 1] == 'A'
&& l[row + 2][col + 2] == 'M'
&& l[row + 3][col + 3] == 'X'))
{
c += 1;
}
// Check /
if look_down
&& col >= 3 // Can look left
&& ((l[row][col] == 'X'
&& l[row + 1][col - 1] == 'M'
&& l[row + 2][col - 2] == 'A'
&& l[row + 3][col - 3] == 'S')
|| (l[row][col] == 'S'
&& l[row + 1][col - 1] == 'A'
&& l[row + 2][col - 2] == 'M'
&& l[row + 3][col - 3] == 'X'))
{
c += 1;
}
}
}
Some(c)
}
pub fn part_two(input: &str) -> Option<u32> {
let l: Vec<Vec<char>> = input.lines().map(|l| l.chars().collect()).collect();
let mut c = 0;
for row in 1..l.len() - 1 {
for col in 1..l[0].len() - 1 {
if l[row][col] != 'A' {
continue;
}
// Check \
let ltor = (l[row - 1][col - 1] == 'M' && l[row + 1][col + 1] == 'S')
|| (l[row - 1][col - 1] == 'S' && l[row + 1][col + 1] == 'M');
// Check /
let rtol = (l[row + 1][col - 1] == 'M' && l[row - 1][col + 1] == 'S')
|| (l[row + 1][col - 1] == 'S' && l[row - 1][col + 1] == 'M');
if ltor && rtol {
c += 1;
}
}
}
Some(c)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(18));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(9));
}
}