Skip to content

Commit

Permalink
feature: Like a GIF For Your Yard (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotacke committed Nov 14, 2018
1 parent 090db7c commit e730e81
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
58 changes: 58 additions & 0 deletions day-18-like-a-gif-for-your-yard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,63 @@ 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:

```
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**?

## References
- https://adventofcode.com/2015/day/18
81 changes: 81 additions & 0 deletions day-18-like-a-gif-for-your-yard/gif2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class Grid {
constructor (input) {
this._state = input
.split('\n')
.map((x) => x
.trim()
.split('')
.map((l) => l === '#')
);
}

getLight (x, y) {
return (this._state[y] || [])[x] || false;
}

getTurnedOnLights () {
return this._state.reduce((accumulator, currentValue) => {
return accumulator + currentValue.filter((x) => x).length;
}, 0);
}

_nextLightState (x, y) {
const getLight = this.getLight.bind(this);

if ((x === 0 && y === 0) ||
(x === 0 && y === this._state.length - 1) ||
(x === this._state[0].length - 1 && y === 0) ||
(x === this._state[0].length - 1 && y === this._state.length - 1)) {
return true;
}

const currentState = getLight(x, y);
const onNeighbors = [
getLight(x - 1, y - 1), getLight(x, y - 1), getLight(x + 1, y - 1),
getLight(x - 1, y), getLight(x + 1, y),
getLight(x - 1, y + 1), getLight(x, y + 1), getLight(x + 1, y + 1),
].filter((l) => l).length;

return currentState
? onNeighbors >= 2 && onNeighbors <= 3
: onNeighbors === 3;
}

tick () {
const nextState = [];

for (let y = 0; y < this._state.length; y++) {
const row = [];

for (let x = 0; x < this._state[0].length; x++) {
const light = this._nextLightState(x, y);

row.push(light);
}

nextState.push(row);
}

this._state = nextState;
}

print () {
return this._state
.map((row) => row
.map((col) => col ? '#' : '.')
.join(''))
.join('\n');
}
}

const gif = (input, steps) => {
const grid = new Grid(input);

for (let i = 0; i < steps; i++) {
grid.tick();
}

return grid.getTurnedOnLights();
};

module.exports = gif;
16 changes: 16 additions & 0 deletions day-18-like-a-gif-for-your-yard/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');

const gif = require('./gif');
const gif2 = require('./gif2');

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

assert.strictEqual(gif(grid, steps), 4);
});

describe('Part Two', () => {
it('should consider stuck lights', () => {
const grid =
`##.#.#
...##.
#....#
..#...
#.#..#
####.#`;
const steps = 5;

assert.strictEqual(gif2(grid, steps), 17);
});
});
});

0 comments on commit e730e81

Please sign in to comment.