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
183
184
185
186
187
188
189
use crate::input::Input;
use std::collections::HashSet;
use std::slice::Iter;

#[derive(Copy, Clone)]
enum Direction {
    Up,
    Right,
    Down,
    Left,
}

impl Direction {
    const fn delta(self) -> (i32, i32) {
        match self {
            Self::Up => (0, 1),
            Self::Right => (1, 0),
            Self::Down => (0, -1),
            Self::Left => (-1, 0),
        }
    }
    fn iterator() -> Iter<'static, Self> {
        static DIRECTIONS: [Direction; 4] = [
            Direction::Up,
            Direction::Right,
            Direction::Down,
            Direction::Left,
        ];
        DIRECTIONS.iter()
    }
}

#[derive(PartialEq, Eq, Hash, Copy, Clone)]
struct Grid {
    value: u32,
}

impl Grid {
    const fn zeroed() -> Self {
        Self { value: 0 }
    }

    fn parse(input: &str) -> Result<Self, String> {
        if input.chars().filter(|&c| c == '#' || c == '.').count() != 25
            || input.chars().any(|c| !matches!(c, '#' | '.' | '\n'))
            || input.lines().count() != 5
        {
            return Err("Invalid input - expected 5x5 grid of '#' and '.'".to_string());
        }
        Ok(Self {
            value: input
                .lines()
                .enumerate()
                .flat_map(|(y, line)| {
                    line.chars()
                        .enumerate()
                        .filter_map(move |(x, character)| match character {
                            '#' => Some((x, y)),
                            _ => None,
                        })
                })
                .fold(0, |value, (x, y)| value | 1 << ((y * 5) + x)),
        })
    }

    fn at(self, x: i32, y: i32) -> u32 {
        if x < 0 || y < 0 || x >= 5 || y >= 5 {
            0
        } else {
            let bit = 1 << ((y * 5) + x);
            u32::from(bit & self.value == bit)
        }
    }

    fn advance_minute(&mut self) {
        let mut new_value = 0_u32;

        for y in 0..5 {
            for x in 0..5 {
                let adjacent_bugs =
                    self.at(x - 1, y) + self.at(x + 1, y) + self.at(x, y - 1) + self.at(x, y + 1);
                if adjacent_bugs == 1 || (self.at(x, y) == 0 && adjacent_bugs == 2) {
                    let bit = 1 << ((y * 5) + x);
                    new_value |= bit;
                }
            }
        }

        self.value = new_value;
    }

    fn advance_until_repeat(&mut self) -> u32 {
        let mut seen = HashSet::new();

        while seen.insert(self.value) {
            self.advance_minute();
        }

        self.value
    }

    const fn count_bugs(self) -> u32 {
        self.value.count_ones()
    }

    const fn count_bugs_at_edge(self, coming_from: Direction) -> u32 {
        #![allow(clippy::unusual_byte_groupings)]
        (self.value
            & match coming_from {
                Direction::Up => 0b00000_00000_00000_00000_11111_u32,
                Direction::Left => 0b10000_10000_10000_10000_10000_u32,
                Direction::Down => 0b11111_00000_00000_00000_00000_u32,
                Direction::Right => 0b00001_00001_00001_00001_00001_u32,
            })
        .count_ones()
    }

    fn advance(&mut self, inner_grid: Self, outer_grid: Self) -> Self {
        let mut new_value = 0_u32;

        for y in 0..5 {
            for x in 0..5 {
                if (x, y) == (2, 2) {
                    continue;
                }

                let mut adjacent_bugs = 0;

                for &direction in Direction::iterator() {
                    let delta = direction.delta();
                    let new_x = x + delta.0;
                    let new_y = y + delta.1;
                    adjacent_bugs += if !((0..5).contains(&new_x) && (0..5).contains(&new_y)) {
                        outer_grid.at(2 + delta.0, 2 + delta.1)
                    } else if (new_x, new_y) == (2, 2) {
                        inner_grid.count_bugs_at_edge(direction)
                    } else {
                        self.at(new_x, new_y)
                    };
                }

                if adjacent_bugs == 1 || (self.at(x, y) == 0 && adjacent_bugs == 2) {
                    let bit = 1 << ((y * 5) + x);
                    new_value |= bit;
                }
            }
        }

        Self { value: new_value }
    }
}

pub fn solve(input: &mut Input) -> Result<u32, String> {
    const MINUTES: usize = 200;
    const MAX_LEVELS: usize = MINUTES * 2;

    let mut grid = Grid::parse(input.text)?;

    if input.is_part_one() {
        return Ok(grid.advance_until_repeat());
    }

    let mut current_generation = vec![Grid::zeroed(); MAX_LEVELS];
    let mut next_generation = vec![Grid::zeroed(); MAX_LEVELS];

    current_generation[MAX_LEVELS / 2] = grid;

    for _minute in 0..MINUTES {
        for i in 1..(current_generation.len() - 1) {
            let mut this_grid = current_generation[i];
            next_generation[i] =
                this_grid.advance(current_generation[i - 1], current_generation[i + 1]);
        }
        std::mem::swap(&mut current_generation, &mut next_generation);
    }

    Ok(current_generation
        .iter()
        .map(|value| value.count_bugs())
        .sum::<u32>())
}

#[test]
pub fn tests() {
    use crate::input::{test_part_one, test_part_two};
    let input = include_str!("day24_input.txt");
    test_part_one!(input => 11_042_850);
    test_part_two!(input => 1967);
}