-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
167 lines (145 loc) · 4.12 KB
/
mod.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const TARGET: &[u8] = "XMAS".as_bytes();
enum Direction {
Up,
Down,
Left,
Right,
UpLeft,
DownLeft,
UpRight,
DownRight,
}
fn rek(
matrix: &Vec<Vec<u8>>,
dir: Direction,
pos: (i32, i32),
bounds: (i32, i32),
off: usize,
) -> u64 {
if off == 4 {
return 1;
}
let next_pos = match dir {
Direction::Up => (pos.0, pos.1 - 1),
Direction::Down => (pos.0, pos.1 + 1),
Direction::Left => (pos.0 - 1, pos.1),
Direction::Right => (pos.0 + 1, pos.1),
Direction::UpLeft => (pos.0 - 1, pos.1 - 1),
Direction::UpRight => (pos.0 + 1, pos.1 - 1),
Direction::DownLeft => (pos.0 - 1, pos.1 + 1),
Direction::DownRight => (pos.0 + 1, pos.1 + 1),
};
if next_pos.0 < 0 || next_pos.1 < 0 {
// we went OOB
return 0;
}
if next_pos.0 > bounds.0 || next_pos.1 > bounds.1 {
// we went OOB
return 0;
}
if matrix[next_pos.1 as usize][next_pos.0 as usize] == TARGET[off] {
return rek(matrix, dir, next_pos, bounds, off + 1);
}
0
}
fn check_pos(matrix: &[Vec<u8>], pos: (i32, i32), bounds: (i32, i32), char: u8) -> bool {
if pos.0 < 0 || pos.1 < 0 {
// we went OOB
return false;
}
if pos.0 > bounds.0 || pos.1 > bounds.1 {
// we went OOB
return false;
}
matrix[pos.1 as usize][pos.0 as usize] == char
}
const X: u8 = 0x58;
const M: u8 = 0x4d;
const A: u8 = 0x41;
const S: u8 = 0x53;
fn part_two(matrix: &[Vec<u8>], pos: (i32, i32), bounds: (i32, i32)) -> u64 {
// S S
// A
// M M
if check_pos(matrix, (pos.0 - 1, pos.1 - 1), bounds, S)
&& check_pos(matrix, (pos.0 + 1, pos.1 - 1), bounds, S)
&& check_pos(matrix, (pos.0 - 1, pos.1 + 1), bounds, M)
&& check_pos(matrix, (pos.0 + 1, pos.1 + 1), bounds, M)
{
return 1;
}
// M S
// A
// M S
if check_pos(matrix, (pos.0 - 1, pos.1 - 1), bounds, M)
&& check_pos(matrix, (pos.0 + 1, pos.1 - 1), bounds, S)
&& check_pos(matrix, (pos.0 - 1, pos.1 + 1), bounds, M)
&& check_pos(matrix, (pos.0 + 1, pos.1 + 1), bounds, S)
{
return 1;
}
// M M
// A
// S S
if check_pos(matrix, (pos.0 - 1, pos.1 - 1), bounds, M)
&& check_pos(matrix, (pos.0 + 1, pos.1 - 1), bounds, M)
&& check_pos(matrix, (pos.0 - 1, pos.1 + 1), bounds, S)
&& check_pos(matrix, (pos.0 + 1, pos.1 + 1), bounds, S)
{
return 1;
}
// S M
// A
// S M
if check_pos(matrix, (pos.0 - 1, pos.1 - 1), bounds, S)
&& check_pos(matrix, (pos.0 + 1, pos.1 - 1), bounds, M)
&& check_pos(matrix, (pos.0 - 1, pos.1 + 1), bounds, S)
&& check_pos(matrix, (pos.0 + 1, pos.1 + 1), bounds, M)
{
return 1;
}
0
}
pub fn solve(input: String) -> Vec<u64> {
let (matrix, bounds) = aoc::parse_matrix(input);
let mut count = 0;
let mut count2 = 0;
for y in 0..bounds.1 + 1 {
for x in 0..bounds.0 + 1 {
if matrix[y as usize][x as usize] == X {
count += rek(&matrix, Direction::Up, (x, y), bounds, 1);
count += rek(&matrix, Direction::Down, (x, y), bounds, 1);
count += rek(&matrix, Direction::Left, (x, y), bounds, 1);
count += rek(&matrix, Direction::Right, (x, y), bounds, 1);
count += rek(&matrix, Direction::UpLeft, (x, y), bounds, 1);
count += rek(&matrix, Direction::UpRight, (x, y), bounds, 1);
count += rek(&matrix, Direction::DownLeft, (x, y), bounds, 1);
count += rek(&matrix, Direction::DownRight, (x, y), bounds, 1);
}
if matrix[y as usize][x as usize] == A {
count2 += part_two(&matrix, (x, y), bounds)
}
}
}
vec![count, count2]
}
#[test]
fn test() {
assert_eq!(
solve(
"MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX"
.to_string()
),
[18, 9]
);
assert_eq!(solve(aoc::input(4)), [2462, 1877]);
}