year2024/
day15.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
175
176
177
178
179
180
181
182
use utils::grid;
use utils::prelude::*;

/// Moving boxes around a grid.
#[derive(Clone, Debug)]
pub struct Day15<'a> {
    cols: usize,
    grid: Vec<u8>,
    robot: usize,
    moves: &'a str,
}

impl<'a> Day15<'a> {
    pub fn new(input: &'a str, _: InputType) -> Result<Self, InputError> {
        let Some((grid, moves)) = input.split_once("\n\n") else {
            return Err(InputError::new(input, 0, "expected grid and moves"));
        };

        let (rows, cols, mut grid) = grid::from_str(grid, |b| match b {
            b'.' | b'#' | b'O' | b'@' => Some(b),
            _ => None,
        })?;
        if !grid::is_enclosed(rows, cols, &grid, |&b| b == b'#') {
            return Err(InputError::new(
                input,
                0,
                "expected grid to be enclosed by walls",
            ));
        }

        let mut robots = grid.iter().enumerate().filter(|&(_, &b)| b == b'@');
        let Some((robot, _)) = robots.next() else {
            return Err(InputError::new(input, 0, "expected a robot"));
        };
        if robots.count() > 0 {
            return Err(InputError::new(input, 0, "expected only one robot"));
        }
        grid[robot] = b'.';

        if let Some(idx) = moves.find(|b| !matches!(b, '^' | 'v' | '<' | '>' | '\n')) {
            return Err(InputError::new(
                input,
                &moves[idx..],
                "expected ^, v, <, or >",
            ));
        }

        Ok(Self {
            cols,
            grid,
            robot,
            moves,
        })
    }

    #[must_use]
    pub fn part1(&self) -> u32 {
        let mut grid = self.grid.clone();
        let mut robot = self.robot;

        for offset in self.moves_iterator(self.cols) {
            let next = robot.wrapping_add_signed(offset);
            if grid[next] == b'.' {
                robot = next;
            } else if grid[next] == b'O' {
                let mut next_free = next.wrapping_add_signed(offset);
                while grid[next_free] == b'O' {
                    next_free = next_free.wrapping_add_signed(offset);
                }
                if grid[next_free] == b'.' {
                    grid[next_free] = b'O';
                    grid[next] = b'.';
                    robot = next;
                }
            }
        }

        Self::sum_box_coords(&grid, self.cols, b'O')
    }

    #[must_use]
    pub fn part2(&self) -> u32 {
        let mut grid = self
            .grid
            .iter()
            .flat_map(|&b| match b {
                b'#' => [b'#', b'#'],
                b'.' => [b'.', b'.'],
                b'O' => [b'[', b']'],
                _ => unreachable!(),
            })
            .collect::<Vec<_>>();
        let cols = self.cols * 2;
        let mut robot = (self.robot / self.cols * cols) + (self.robot % self.cols * 2);

        for offset in self.moves_iterator(cols) {
            let next = robot.wrapping_add_signed(offset);
            if grid[next] == b'.' {
                robot = next;
            } else if (grid[next] == b'[' || grid[next] == b']')
                && Self::can_move_p2(&grid, next, offset)
            {
                Self::move_box_p2(&mut grid, next, offset);
                robot = next;
            }
        }

        Self::sum_box_coords(&grid, cols, b'[')
    }

    #[inline]
    fn moves_iterator(&self, cols: usize) -> impl Iterator<Item = isize> + use<'_> {
        self.moves.bytes().filter_map(move |c| match c {
            b'^' => Some(-(cols as isize)),
            b'v' => Some(cols as isize),
            b'<' => Some(-1),
            b'>' => Some(1),
            _ => None,
        })
    }

    #[inline]
    fn sum_box_coords(grid: &[u8], cols: usize, box_byte: u8) -> u32 {
        grid.iter()
            .enumerate()
            .map(|(i, &b)| {
                if b == box_byte {
                    100 * ((i / cols) as u32) + ((i % cols) as u32)
                } else {
                    0
                }
            })
            .sum()
    }

    fn can_move_p2(grid: &[u8], pos: usize, offset: isize) -> bool {
        let (left, right) = match grid[pos] {
            b'[' => (pos, pos + 1),
            b']' => (pos - 1, pos),
            b'.' => return true,
            b'#' => return false,
            _ => unreachable!(),
        };

        if offset == -1 || offset == 1 {
            Self::can_move_p2(grid, pos.wrapping_add_signed(offset), offset)
        } else if grid[left.wrapping_add_signed(offset)] == b'[' {
            // One box directly above/below, only need to recurse once
            Self::can_move_p2(grid, left.wrapping_add_signed(offset), offset)
        } else {
            Self::can_move_p2(grid, left.wrapping_add_signed(offset), offset)
                && Self::can_move_p2(grid, right.wrapping_add_signed(offset), offset)
        }
    }

    fn move_box_p2(grid: &mut [u8], pos: usize, offset: isize) {
        let (left, right) = match grid[pos] {
            b'[' => (pos, pos + 1),
            b']' => (pos - 1, pos),
            b'.' => return,
            _ => unreachable!(),
        };

        if offset == -1 || offset == 1 {
            Self::move_box_p2(grid, pos.wrapping_add_signed(offset * 2), offset);
        } else {
            Self::move_box_p2(grid, left.wrapping_add_signed(offset), offset);
            Self::move_box_p2(grid, right.wrapping_add_signed(offset), offset);
        }

        grid[left] = b'.';
        grid[right] = b'.';
        grid[left.wrapping_add_signed(offset)] = b'[';
        grid[right.wrapping_add_signed(offset)] = b']';
    }
}

examples!(Day15<'_> -> (u32, u32) [
    {file: "day15_example0.txt", part1: 10092, part2: 9021},
    {file: "day15_example1.txt", part1: 2028},
    {file: "day15_example2.txt", part2: 618},
]);