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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! # [Day 18: Like a GIF For Your Yard](https://adventofcode.com/2015/day/18)
//!
//! After the [million lights incident](https://adventofcode.com/2015/day/6), the fire code has
//! gotten stricter: now, at most ten thousand lights are allowed.
//! You arrange them in a 100x100 grid.
//!
//! Never one to let you down, Santa again mails you instructions on the ideal lighting
//! configuration. With so few lights, he says, you'll have to resort to animation.
//!
//! Start by setting your lights to the included initial configuration (your puzzle input).
//! A `#` means "on", and a `.` means "off".
//!
//! Then, animate your grid in steps, where each step decides the next configuration based on
//! the current one. Each light's next state (either on or off) depends on its current state
//! and the current states of the eight lights adjacent to it (including diagonals).
//! Lights on the edge of the grid might have fewer than eight neighbors;
//! the missing ones always count as "off".
//!
//! For example, in a simplified 6x6 grid, the light marked `A` has the neighbors numbered `1`
//! through `8`, and the light marked `B`, which is on an edge, only has the neighbors marked `1`
//! through `5`:
//!
//! ```plain
//! 1B5...
//! 234...
//! ......
//! ..123.
//! ..8A4.
//! ..765.
//!
//! ```
//!
//! The state a light should have next is based on its current state (on or off) plus
//! the number of neighbors that are on:
//!
//! - A light which is on stays on when `2` or `3` neighbors are on, and turns off otherwise.
//! - A light which is off turns on if exactly `3` neighbors are on, and stays off otherwise.
//!
//! All of the lights update simultaneously; they all consider the same current state
//! before moving to the next.
//!
//! Here's a few steps from an example configuration of another 6x6 grid:
//!
//! ```plain
//! Initial state:
//! .#.#.#
//! ...##.
//! #....#
//! ..#...
//! #.#..#
//! ####..
//!
//! After 1 step:
//! ..##..
//! ..##.#
//! ...##.
//! ......
//! #.....
//! #.##..
//!
//! After 2 steps:
//! ..###.
//! ......
//! ..###.
//! ......
//! .#....
//! .#....
//!
//! After 3 steps:
//! ...#..
//! ......
//! ...#..
//! ..##..
//! ......
//! ......
//!
//! After 4 steps:
//! ......
//! ......
//! ..##..
//! ..##..
//! ......
//! ......
//!
//! ```
//!
//! After `4` steps, this example has four lights on.
//!
//! In your grid of 100x100 lights, given your initial configuration,
//! how many lights are on after 100 steps?
//!
//! # Part Two
//!
//! You flip the instructions over; Santa goes on to point out that this is all just an
//! implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway's_Game_of_Life).
//! At least, it was, until you notice that something's wrong with the grid of lights you bought:
//! four lights, one in each corner, are stuck on and can't be turned off.
//!
//! The example above will actually run like this:
//!
//! ```plain
//! Initial state:
//! ##.#.#
//! ...##.
//! #....#
//! ..#...
//! #.#..#
//! ####.#
//!
//! After 1 step:
//! #.##.#
//! ####.#
//! ...##.
//! ......
//! #...#.
//! #.####
//!
//! After 2 steps:
//! #..#.#
//! #....#
//! .#.##.
//! ...##.
//! .#..##
//! ##.###
//!
//! After 3 steps:
//! #...##
//! ####.#
//! ..##.#
//! ......
//! ##....
//! ####.#
//!
//! After 4 steps:
//! #.####
//! #....#
//! ...#..
//! .##...
//! #.....
//! #.#..#
//!
//! After 5 steps:
//! ##.###
//! .##..#
//! .##...
//! .##...
//! #.#...
//! ##...#
//!
//! ```
//!
//! After `5` steps, this example now has `17` lights on.
//!
//! In your grid of 100x100 lights, given your initial configuration, but with the four
//! corners always in the on state, how many lights are on after 100 steps?

#[aoc_generator(day18)]
fn parse_input(input: &str) -> Vec<Vec<bool>> {
    input
        .lines()
        .map(|line| line.chars().map(|c| c == '#').collect())
        .collect()
}

/// Part 1: In your grid of 100x100 lights, given your initial configuration,
/// how many lights are on after 100 steps?
#[aoc(day18, part1)]
fn part1(input: &[Vec<bool>]) -> usize {
    let grid = evolve1(input, 100);
    count_on(&grid)
}

fn evolve1(input: &[Vec<bool>], steps: usize) -> Vec<Vec<bool>> {
    let mut next = Vec::from(input);
    for _ in 0..steps {
        next = execute_step(&next);
    }
    next
}

fn execute_step(grid: &[Vec<bool>]) -> Vec<Vec<bool>> {
    let width = grid[0].len();
    let height = grid.len();
    let mut next = create_grid(width, height);
    for y in 0..height {
        for x in 0..width {
            let mut alive_neighbors = 0;
            for dy in -1isize..=1 {
                for dx in -1isize..=1 {
                    let y = y as isize + dy;
                    let x = x as isize + dx;
                    if (dx == 0 && dy == 0)
                        || y < 0
                        || y >= height as isize
                        || x < 0
                        || x >= width as isize
                    {
                        continue;
                    }
                    if grid[y as usize][x as usize] {
                        alive_neighbors += 1;
                    }
                }
            }

            next[y][x] = match grid[y][x] {
                // A light which is on stays on when 2 or 3 neighbors are on, and turns off otherwise.
                true => alive_neighbors == 2 || alive_neighbors == 3,
                // A light which is off turns on if exactly 3 neighbors are on, and stays off otherwise.
                false => alive_neighbors == 3,
            }
        }
    }
    next
}

fn create_grid(width: usize, height: usize) -> Vec<Vec<bool>> {
    let mut rows = Vec::new();
    for _ in 0..height {
        rows.push(vec![false; width]);
    }
    rows
}

fn count_on(grid: &[Vec<bool>]) -> usize {
    grid.iter()
        .map(|row| row.iter().filter(|col| **col).count())
        .sum()
}

/// Part 2: In your grid of 100x100 lights, given your initial configuration, but with the four
/// corners always in the on state, how many lights are on after 100 steps?
#[aoc(day18, part2)]
fn part2(input: &[Vec<bool>]) -> usize {
    let grid = evolve2(input, 100);
    count_on(&grid)
}

fn evolve2(input: &[Vec<bool>], steps: usize) -> Vec<Vec<bool>> {
    let mut next = Vec::from(input);
    for _ in 0..steps {
        fill_corners(&mut next);
        next = execute_step(&next);
    }
    fill_corners(&mut next);
    next
}

/// implements "Four lights, one in each corner, are stuck on and can't be turned off." on grid
fn fill_corners(grid: &mut [Vec<bool>]) {
    let width = grid[0].len();
    let height = grid.len();
    grid[0][0] = true;
    grid[0][width - 1] = true;
    grid[height - 1][0] = true;
    grid[height - 1][width - 1] = true;
}

#[cfg(test)]
mod tests {
    use super::*;

    const EXAMPLE_1: &str = ".#.#.#
...##.
#....#
..#...
#.#..#
####..";

    const EXAMPLE_2: &str = "##.#.#
...##.
#....#
..#...
#.#..#
####.#";

    fn display_grid(grid: &[Vec<bool>]) -> String {
        let mut s = String::new();
        for row in grid.iter() {
            for col in row {
                if *col {
                    s += "#";
                } else {
                    s += ".";
                }
            }
            s += "\n";
        }
        s.trim().to_string()
    }

    #[test]
    fn part1_examples() {
        // After `4` steps, this example has four lights on.
        let grid = evolve1(&parse_input(EXAMPLE_1), 4);
        assert_eq!(4, count_on(&grid));
        assert_eq!(
            "......
......
..##..
..##..
......
......",
            display_grid(&grid)
        );
    }

    #[test]
    fn part2_examples() {
        // After `5` steps, this example now has `17` lights on.
        let grid = evolve2(&parse_input(EXAMPLE_2), 5);
        assert_eq!(17, count_on(&grid));
        assert_eq!(
            "##.###
.##..#
.##...
.##...
#.#...
##...#",
            display_grid(&grid)
        );
    }
}