-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rs
141 lines (118 loc) · 3.69 KB
/
day4.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
//! [Day 4: Ceres Search](https://adventofcode.com/2024/day/4)
use aoc::Coord;
type Grid = aoc::Grid<char>;
struct Puzzle {
grid: Grid,
}
impl Puzzle {
fn new() -> Self {
Self {
grid: Grid::default(),
}
}
/// Get the puzzle input.
fn configure(&mut self, data: &str) {
self.grid = Grid::parse(data);
}
/// Solve part one.
fn part1(&self) -> u32 {
let mut n = 0;
let (sx, sy) = (self.grid.width(), self.grid.height());
let mas = |i| match i {
1 => 'M',
2 => 'A',
3 => 'S',
_ => unreachable!(),
};
for (Coord { x, y }, _) in self.grid.iter().filter(|(_, p)| *p == &'X') {
//
if x <= sx - 4 && (1..4).all(|i| self.grid[(x + i, y)] == mas(i)) {
n += 1;
}
if y <= sy - 4 && (1..4).all(|i| self.grid[(x, y + i)] == mas(i)) {
n += 1;
}
if x >= 3 && (1..4).all(|i| self.grid[(x - i, y)] == mas(i)) {
n += 1;
}
if y >= 3 && (1..4).all(|i| self.grid[(x, y - i)] == mas(i)) {
n += 1;
}
if x <= sx - 4 && y <= sy - 4 && (1..4).all(|i| self.grid[(x + i, y + i)] == mas(i)) {
n += 1;
}
if x >= 3 && y >= 3 && (1..4).all(|i| self.grid[(x - i, y - i)] == mas(i)) {
n += 1;
}
if x <= sx - 4 && y >= 3 && (1..4).all(|i| self.grid[(x + i, y - i)] == mas(i)) {
n += 1;
}
if x >= 3 && y <= sy - 4 && (1..4).all(|i| self.grid[(x - i, y + i)] == mas(i)) {
n += 1;
}
}
n
}
/// Solve part two.
fn part2(&self) -> u32 {
let mut n = 0;
let (sx, sy) = (self.grid.width(), self.grid.height());
for x in 1..(sx - 1) {
for y in 1..(sy - 1) {
if self.grid[(x, y)] == 'A' {
let ul = self.grid[(x - 1, y - 1)];
let ur = self.grid[(x + 1, y - 1)];
let bl = self.grid[(x - 1, y + 1)];
let br = self.grid[(x + 1, y + 1)];
if ((ul == 'M' && br == 'S') || (ul == 'S' && br == 'M'))
&& ((ur == 'M' && bl == 'S') || (ur == 'S' && bl == 'M'))
{
n += 1;
}
}
}
}
n
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(&args.input);
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_p1_1() {
let mut puzzle = Puzzle::new();
let data = aoc::load_input_data("sample_1.txt");
puzzle.configure(&data);
assert_eq!(puzzle.part1(), 4);
}
#[test]
fn test_p1_2() {
let mut puzzle = Puzzle::new();
puzzle.configure(&aoc::load_input_data("sample_2.txt"));
assert_eq!(puzzle.part1(), 18);
puzzle.configure(&aoc::load_input_data("sample_3.txt"));
assert_eq!(puzzle.part1(), 18);
}
#[test]
fn test_p2_1() {
let mut puzzle = Puzzle::new();
puzzle.configure(&aoc::load_input_data("sample_4.txt"));
assert_eq!(puzzle.part2(), 1);
}
#[test]
fn test_p2_2() {
let mut puzzle = Puzzle::new();
puzzle.configure(&aoc::load_input_data("sample_2.txt"));
assert_eq!(puzzle.part2(), 9);
puzzle.configure(&aoc::load_input_data("sample_5.txt"));
assert_eq!(puzzle.part2(), 9);
}
}