Skip to content

Commit 9a0e15b

Browse files
committed
day 4
1 parent 6a6b959 commit 9a0e15b

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

Diff for: 2024/src/day4.rs

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
use aoc_runner_derive::{aoc, aoc_generator};
2+
3+
/*
4+
5+
--- Day 4: Ceres Search ---
6+
"Looks like the Chief's not here. Next!" One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the Ceres monitoring station!
7+
8+
As the search for the Chief continues, a small Elf who lives on the station tugs on your shirt; she'd like to know if you could help her with her word search (your puzzle input). She only has to find one word: XMAS.
9+
10+
This word search allows words to be horizontal, vertical, diagonal, written backwards, or even overlapping other words. It's a little unusual, though, as you don't merely need to find one instance of XMAS - you need to find all of them. Here are a few ways XMAS might appear, where irrelevant characters have been replaced with .:
11+
12+
13+
..X...
14+
.SAMX.
15+
.A..A.
16+
XMAS.S
17+
.X....
18+
The actual word search will be full of letters instead. For example:
19+
20+
MMMSXXMASM
21+
MSAMXMSMSA
22+
AMXSXMAAMM
23+
MSAMASMSMX
24+
XMASAMXAMM
25+
XXAMMXXAMA
26+
SMSMSASXSS
27+
SAXAMASAAA
28+
MAMMMXMMMM
29+
MXMXAXMASX
30+
In this word search, XMAS occurs a total of 18 times; here's the same word search again, but where letters not involved in any XMAS have been replaced with .:
31+
32+
....XXMAS.
33+
.SAMXMS...
34+
...S..A...
35+
..A.A.MS.X
36+
XMASAMX.MM
37+
X.....XA.A
38+
S.S.S.S.SS
39+
.A.A.A.A.A
40+
..M.M.M.MM
41+
.X.X.XMASX
42+
Take a look at the little Elf's word search. How many times does XMAS appear?
43+
44+
*/
45+
46+
struct Grid(Vec<Vec<char>>);
47+
48+
impl Grid {
49+
fn get(&self, r: usize, c: usize) -> Option<char> {
50+
self.0.get(r).and_then(|row| row.get(c)).copied()
51+
}
52+
53+
fn get_offset(&self, r: usize, r_offset: isize, c: usize, c_offset: isize) -> Option<char> {
54+
let r: isize = r.try_into().ok()?;
55+
let r: usize = (r + r_offset).try_into().ok()?;
56+
let c: isize = c.try_into().ok()?;
57+
let c: usize = (c + c_offset).try_into().ok()?;
58+
self.get(r, c)
59+
}
60+
}
61+
62+
63+
#[aoc_generator(day4)]
64+
fn parse_input(input: &str) -> Grid {
65+
let input = input.trim();
66+
Grid(input.lines().map(|l| l.trim().chars().collect()).collect())
67+
}
68+
69+
const WORD: [char; 4] = ['X', 'M', 'A', 'S'];
70+
const DIRS: [(isize, isize); 8] = [
71+
(0, 1),
72+
(1, 0),
73+
(1, 1),
74+
(1, -1),
75+
(0, -1),
76+
(-1, 0),
77+
(-1, 1),
78+
(-1, -1),
79+
];
80+
81+
#[aoc(day4, part1)]
82+
fn part1(grid: &Grid) -> u32 {
83+
let mut count = 0;
84+
for (r, row) in grid.0.iter().enumerate() {
85+
for (c, ch) in row.iter().enumerate() {
86+
if *ch == WORD[0] {
87+
'dir:
88+
for (dr, dc) in DIRS.iter() {
89+
for i in 1..WORD.len() {
90+
if grid.get_offset(r, i as isize * dr, c, i as isize * dc) != Some(WORD[i]) {
91+
continue 'dir;
92+
}
93+
}
94+
95+
count += 1;
96+
}
97+
}
98+
}
99+
}
100+
101+
count
102+
}
103+
104+
#[aoc(day4, part2)]
105+
fn part2(grid: &Grid) -> u32 {
106+
let mut count = 0;
107+
for (r, row) in grid.0.iter().enumerate() {
108+
for (c, ch) in row.iter().enumerate() {
109+
if *ch != 'A' {
110+
continue;
111+
}
112+
113+
let up_left = grid.get_offset(r, -1, c, -1);
114+
let up_right = grid.get_offset(r, -1, c, 1);
115+
let down_left = grid.get_offset(r, 1, c, -1);
116+
let down_right = grid.get_offset(r, 1, c, 1);
117+
118+
let up_left_diag =
119+
(up_left == Some('M') && down_right == Some('S')) ||
120+
(up_left == Some('S') && down_right == Some('M'));
121+
122+
let down_left_diag =
123+
(down_left == Some('M') && up_right == Some('S')) ||
124+
(down_left == Some('S') && up_right == Some('M'));
125+
126+
if up_left_diag && down_left_diag {
127+
count += 1;
128+
}
129+
}
130+
}
131+
count
132+
}
133+
134+
135+
#[cfg(test)]
136+
mod tests {
137+
use super::*;
138+
139+
#[test]
140+
fn part1_example() {
141+
let input = parse_input(
142+
r#"
143+
MMMSXXMASM
144+
MSAMXMSMSA
145+
AMXSXMAAMM
146+
MSAMASMSMX
147+
XMASAMXAMM
148+
XXAMMXXAMA
149+
SMSMSASXSS
150+
SAXAMASAAA
151+
MAMMMXMMMM
152+
MXMXAXMASX
153+
"#
154+
.trim(),
155+
);
156+
assert_eq!(part1(&input), 18);
157+
}
158+
159+
#[test]
160+
fn part2_example() {
161+
let input = parse_input(
162+
r#"
163+
.M.S......
164+
..A..MSMS.
165+
.M.S.MAA..
166+
..A.ASMSM.
167+
.M.S.M....
168+
..........
169+
S.S.S.S.S.
170+
.A.A.A.A..
171+
M.M.M.M.M.
172+
..........
173+
"#
174+
.trim(),
175+
);
176+
assert_eq!(part2(&input), 9);
177+
}
178+
}

Diff for: 2024/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use aoc_runner_derive::aoc_lib;
33
mod day1;
44
mod day2;
55
mod day3;
6+
mod day4;
67

78
aoc_lib! {
89
year = 2024, extra_alternatives = ["fnv"]

0 commit comments

Comments
 (0)