Part 1
Description
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
or3
neighbors are on, and turns off otherwise. - A light which is off turns on if exactly
3
neighbors are on, and stays off otherwise.
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 100×100 lights, given your initial configuration, how many lights are on after 100 steps?
Input
Solution
In order to simplify calculation, I decided to replace#
with 1
and .
with 0
. Then, I pasted those input into a 100×100 grid square in Excel. Next, I generated 100 100×100 grid squares, each of which representing each step. In the end, I counted the sum of the numbers in the last square (the numbers of lights that are on). Here is my Excel sheet:In CZ4, my formula is:
=IF(B4=1,IF(OR(SUM(A3:C5)=3,SUM(A3:C5)=4),1,0),IF(SUM(A3:C5)=3,1,0))
I had to modified the sum conditions for the first if (when the lights are on) to be either 4 or 3 because they include the middle number (which is
1
).Part 2
Description
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?
Solution
For the part 2, I had to change the number in the four corners in my initial configuration (because not all of them are#
or 1
). Then, in a new sheet, I pasted those input numbers. Next, similarly, I generated 100 100×100 grid squares. I changed the formula into=IF(OR(AND(ISBLANK(A1),ISBLANK(B1),ISBLANK(A2)),AND(ISBLANK(B1),ISBLANK(C1),ISBLANK(C2)),AND(ISBLANK(A2),ISBLANK(A3),ISBLANK(B3)),AND(ISBLANK(C2),ISBLANK(B3),ISBLANK(C3))),1,IF(B2=1,IF(OR(SUM(A1:C3)=4,SUM(A1:C3)=3),1,0),IF(SUM(A1:C3)=3,1,0)))
so that it will detect if the corresponding cell is a corner or not, and set the value to
1
if it is.
0 komentar:
Post a Comment