year2024/
day04.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
use utils::grid;
use utils::prelude::*;

/// Counting matches in a word search.
#[derive(Clone, Debug)]
pub struct Day04 {
    cols: usize,
    grid: Vec<u8>,
}

impl Day04 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        let (_, cols, grid) = grid::from_str_padded(input, 3, b'\0', |c| match c {
            b'X' | b'M' | b'A' | b'S' => Some(c),
            _ => None,
        })?;
        Ok(Self { cols, grid })
    }

    #[must_use]
    pub fn part1(&self) -> u32 {
        self.check_offset(self.cols as isize)
            + self.check_offset(-(self.cols as isize))
            + self.check_offset(1)
            + self.check_offset(-1)
            + self.check_offset((self.cols as isize) + 1)
            + self.check_offset((self.cols as isize) - 1)
            + self.check_offset(-(self.cols as isize) + 1)
            + self.check_offset(-(self.cols as isize) - 1)
    }

    fn check_offset(&self, offset: isize) -> u32 {
        let start = 3 * self.cols + 3;
        let mut count = 0;
        for (((first, second), third), fourth) in self.grid[start..]
            .iter()
            .zip(&self.grid[start.wrapping_add_signed(offset)..])
            .zip(&self.grid[start.wrapping_add_signed(offset * 2)..])
            .zip(&self.grid[start.wrapping_add_signed(offset * 3)..])
        {
            count += u32::from(
                (*first == b'X') & (*second == b'M') & (*third == b'A') & (*fourth == b'S'),
            );
        }
        count
    }

    #[must_use]
    pub fn part2(&self) -> u32 {
        let mut count = 0;
        for ((((middle, nw), ne), sw), se) in self.grid[self.cols * 4 + 4..]
            .iter()
            .zip(&self.grid[self.cols * 3 + 3..])
            .zip(&self.grid[self.cols * 3 + 5..])
            .zip(&self.grid[self.cols * 5 + 3..])
            .zip(&self.grid[self.cols * 5 + 5..])
        {
            count += u32::from(
                (*middle == b'A') & ((*nw ^ *se) == (b'M' ^ b'S')) & ((*ne ^ *sw) == (b'M' ^ b'S')),
            );
        }
        count
    }
}

examples!(Day04 -> (u32, u32) [
    {file: "day04_example0.txt", part1: 18, part2: 9},
]);