generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.rs
174 lines (154 loc) · 4.33 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
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
168
169
170
171
172
173
174
use itertools::Itertools;
advent_of_code::solution!(4);
#[derive(Copy, Clone, PartialEq, Eq)]
enum Orientation {
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
NorthWest,
}
impl Orientation {
fn direction(&self) -> (i32, i32) {
match self {
Orientation::North => (0, -1),
Orientation::NorthEast => (1, -1),
Orientation::East => (1, 0),
Orientation::SouthEast => (1, 1),
Orientation::South => (0, 1),
Orientation::SouthWest => (-1, 1),
Orientation::West => (-1, 0),
Orientation::NorthWest => (-1, -1),
}
}
}
fn dfs(
grid: &Vec<Vec<char>>,
row: usize,
col: usize,
orientation: Option<Orientation>,
index: usize,
) -> usize {
if index == 4 {
return 1;
}
let current_char = "XMAS".as_bytes()[index] as char;
if grid[row][col] != current_char {
return 0;
}
if let Some(orientation) = orientation {
let (col_move, row_move) = orientation.direction();
let next_row = row as i32 + row_move;
let next_col = col as i32 + col_move;
if index == 3 {
return 1;
}
if next_row < 0
|| next_row >= grid.len() as i32
|| next_col < 0
|| next_col >= grid[0].len() as i32
{
return 0;
}
dfs(
grid,
next_row as usize,
next_col as usize,
Some(orientation),
index + 1,
)
} else {
let directions = [
Orientation::North,
Orientation::NorthEast,
Orientation::East,
Orientation::SouthEast,
Orientation::South,
Orientation::SouthWest,
Orientation::West,
Orientation::NorthWest,
];
let mut total_matches = 0;
for dir in directions.iter() {
let (col_move, row_move) = dir.direction();
let next_row = row as i32 + row_move;
let next_col = col as i32 + col_move;
if next_row >= 0
&& next_row < grid.len() as i32
&& next_col >= 0
&& next_col < grid[0].len() as i32
{
total_matches += dfs(
grid,
next_row as usize,
next_col as usize,
Some(*dir),
index + 1,
);
}
}
total_matches
}
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = input
.lines()
.map(|line| line.chars().collect_vec())
.collect_vec();
let row_size = grid.len();
let col_size = grid[0].len();
let mut result = 0;
for x in 0..row_size {
for y in 0..col_size {
if grid[x][y] == 'X' {
let d = dfs(&grid, x, y, None, 0);
result += d;
}
}
}
Some(result as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = input
.lines()
.map(|line| line.chars().collect_vec())
.collect_vec();
let row_size = grid.len();
let col_size = grid[0].len();
let mut result = 0;
for x in 0..row_size {
for y in 0..col_size {
if grid[x][y] == 'A' && x > 0 && y > 0 && x < row_size - 1 && y < col_size - 1 {
let north_west = grid[x - 1][y - 1];
let south_east = grid[x + 1][y + 1];
let north_east = grid[x - 1][y + 1];
let south_west = grid[x + 1][y - 1];
if ((north_west == 'S' && south_east == 'M')
|| (north_west == 'M' && south_east == 'S'))
&& ((north_east == 'S' && south_west == 'M')
|| (north_east == 'M' && south_west == 'S'))
{
result += 1;
}
}
}
}
Some(result as u32)
}
#[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));
}
}