-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Like a GIF For Your Yard (part 2)
- Loading branch information
1 parent
090db7c
commit e730e81
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters