Skip to content

Commit e730e81

Browse files
committed
feature: Like a GIF For Your Yard (part 2)
1 parent 090db7c commit e730e81

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

day-18-like-a-gif-for-your-yard/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,63 @@ After 4 steps, this example has four lights on.
7474

7575
In your grid of 100x100 lights, given your initial configuration, **how many lights are on after 100 steps**?
7676

77+
## Part Two
78+
79+
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:
80+
81+
```
82+
Initial state:
83+
##.#.#
84+
...##.
85+
#....#
86+
..#...
87+
#.#..#
88+
####.#
89+
90+
After 1 step:
91+
#.##.#
92+
####.#
93+
...##.
94+
......
95+
#...#.
96+
#.####
97+
98+
After 2 steps:
99+
#..#.#
100+
#....#
101+
.#.##.
102+
...##.
103+
.#..##
104+
##.###
105+
106+
After 3 steps:
107+
#...##
108+
####.#
109+
..##.#
110+
......
111+
##....
112+
####.#
113+
114+
After 4 steps:
115+
#.####
116+
#....#
117+
...#..
118+
.##...
119+
#.....
120+
#.#..#
121+
122+
After 5 steps:
123+
##.###
124+
.##..#
125+
.##...
126+
.##...
127+
#.#...
128+
##...#
129+
```
130+
131+
After `5` steps, this example now has `17` lights on.
132+
133+
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**?
134+
77135
## References
78136
- https://adventofcode.com/2015/day/18
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class Grid {
2+
constructor (input) {
3+
this._state = input
4+
.split('\n')
5+
.map((x) => x
6+
.trim()
7+
.split('')
8+
.map((l) => l === '#')
9+
);
10+
}
11+
12+
getLight (x, y) {
13+
return (this._state[y] || [])[x] || false;
14+
}
15+
16+
getTurnedOnLights () {
17+
return this._state.reduce((accumulator, currentValue) => {
18+
return accumulator + currentValue.filter((x) => x).length;
19+
}, 0);
20+
}
21+
22+
_nextLightState (x, y) {
23+
const getLight = this.getLight.bind(this);
24+
25+
if ((x === 0 && y === 0) ||
26+
(x === 0 && y === this._state.length - 1) ||
27+
(x === this._state[0].length - 1 && y === 0) ||
28+
(x === this._state[0].length - 1 && y === this._state.length - 1)) {
29+
return true;
30+
}
31+
32+
const currentState = getLight(x, y);
33+
const onNeighbors = [
34+
getLight(x - 1, y - 1), getLight(x, y - 1), getLight(x + 1, y - 1),
35+
getLight(x - 1, y), getLight(x + 1, y),
36+
getLight(x - 1, y + 1), getLight(x, y + 1), getLight(x + 1, y + 1),
37+
].filter((l) => l).length;
38+
39+
return currentState
40+
? onNeighbors >= 2 && onNeighbors <= 3
41+
: onNeighbors === 3;
42+
}
43+
44+
tick () {
45+
const nextState = [];
46+
47+
for (let y = 0; y < this._state.length; y++) {
48+
const row = [];
49+
50+
for (let x = 0; x < this._state[0].length; x++) {
51+
const light = this._nextLightState(x, y);
52+
53+
row.push(light);
54+
}
55+
56+
nextState.push(row);
57+
}
58+
59+
this._state = nextState;
60+
}
61+
62+
print () {
63+
return this._state
64+
.map((row) => row
65+
.map((col) => col ? '#' : '.')
66+
.join(''))
67+
.join('\n');
68+
}
69+
}
70+
71+
const gif = (input, steps) => {
72+
const grid = new Grid(input);
73+
74+
for (let i = 0; i < steps; i++) {
75+
grid.tick();
76+
}
77+
78+
return grid.getTurnedOnLights();
79+
};
80+
81+
module.exports = gif;

day-18-like-a-gif-for-your-yard/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert');
22

33
const gif = require('./gif');
4+
const gif2 = require('./gif2');
45

56
describe('Day 18: Like a GIF For Your Yard', () => {
67
it('should calculate final state lights', () => {
@@ -15,4 +16,19 @@ describe('Day 18: Like a GIF For Your Yard', () => {
1516

1617
assert.strictEqual(gif(grid, steps), 4);
1718
});
19+
20+
describe('Part Two', () => {
21+
it('should consider stuck lights', () => {
22+
const grid =
23+
`##.#.#
24+
...##.
25+
#....#
26+
..#...
27+
#.#..#
28+
####.#`;
29+
const steps = 5;
30+
31+
assert.strictEqual(gif2(grid, steps), 17);
32+
});
33+
});
1834
});

0 commit comments

Comments
 (0)