-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.rs
56 lines (50 loc) · 1.5 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
#![feature(test)]
type Input = Vec<Vec<u8>>;
const NEIGH: [(isize, isize); 8] = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
];
fn setup(input: &str) -> Input {
input.trim().lines().map(|l| l.bytes().collect()).collect()
}
fn part1(input: &Input) -> usize {
let height = input.len() as isize;
let width = input[0].len() as isize;
(0..height)
.flat_map(|i| {
(0..width).flat_map(move |j| {
NEIGH.iter().filter(move |&(di, dj)| {
"XMAS".bytes().enumerate().all(|(k, b)| {
let k = k as isize;
let i = i + di * k;
let j = j + dj * k;
(0..height).contains(&i)
&& (0..width).contains(&j)
&& input[i as usize][j as usize] == b
})
})
})
})
.count()
}
fn part2(input: &Input) -> usize {
let height = input.len();
let width = input[0].len();
(1..height - 1)
.flat_map(|i| {
(1..width - 1).filter(move |&j| {
const OUTER: [(u8, u8); 2] = [(b'M', b'S'), (b'S', b'M')];
input[i][j] == b'A'
&& OUTER.contains(&(input[i - 1][j - 1], input[i + 1][j + 1]))
&& OUTER.contains(&(input[i - 1][j + 1], input[i + 1][j - 1]))
})
})
.count()
}
aoc::main!(2024, 4, ex: 1);