# AoC 2015 Puzzle 18

###### Info

I solved this puzzle. You can find the code here, but I have yet to write a good explanation of the solution here. I will do this shortly, thank you for your patience.

## Day 18: Like a GIF For Your Yard

After the million lights incident, 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:

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:

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?

Your puzzle answer was 768.

### Part 2

You flip the instructions over; Santa goes on to point out that this is all just an implementation of 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:

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?

### Complete Solution

int[][] createGrid(int size) { int[][] grid = new int[size][size]; for (int row = 0; row < size; row++) { Arrays.fill(grid[row], 0); } return grid; } void setGridState(int[][] grid, List<String> state) { for (int i = 0; i < state.size(); i++) { int[] chars = state.get(i).chars().toArray(); for (int j = 0; j < chars.length; j++) { if (chars[j] == '#') { grid[i][j] = 1; } else { grid[i][j] = 0; } } } } void printGrid(int[][] grid) { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { switch (grid[i][j]) { case 1: System.out.print("#"); break; default: System.out.print("."); } } System.out.print("\n"); } } int[][] evolveGrid(int[][] grid, int part) { int[][] evolved = new int[grid.length][grid.length]; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { int on = 0; // cycle neighbors for (int row = Math.max(0, i - 1); row <= Math.min(grid.length - 1, i + 1); row++) { for (int col = Math.max(0, j - 1); col <= Math.min(grid.length - 1, j + 1); col++) { if (row != i || col != j) { switch (grid[row][col]) { case 1: ++on; break; } } } } if (grid[i][j] == 1 && (on == 2 || on == 3)) { evolved[i][j] = 1; } else if (grid[i][j] == 0 && on == 3) { evolved[i][j] = 1; } else { evolved[i][j] = 0; } if (part == 2) { evolved[0][0] = 1; evolved[0][evolved.length-1] = 1; evolved[evolved.length-1][0] = 1; evolved[evolved.length-1][evolved.length-1] = 1; } } } return evolved; } int countOn(int[][] grid) { int on = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { if (grid[i][j] == 1) ++on; } } return on; } @Override public String part1(List<String> input) { int[][] grid = createGrid(100); setGridState(grid, input); // printGrid(grid); for (int i = 0; i < 100; i++) { // System.out.println(); int[][] ev1 = evolveGrid(grid,1); // printGrid(ev1); grid = ev1; } return String.valueOf(countOn(grid)); } @Override public String part2(List<String> input) { int[][] grid = createGrid(100); setGridState(grid, input); // printGrid(grid); for (int i = 0; i < 100; i++) { // System.out.println(); int[][] ev1 = evolveGrid(grid,2); // printGrid(ev1); grid = ev1; } return String.valueOf(countOn(grid)); }