Last active
January 5, 2016 16:42
-
-
Save ghaiklor/50aa8cccefc5b203d8b2 to your computer and use it in GitHub Desktop.
Advent of Code (Day 18 Part 1)
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
const fs = require('fs'); | |
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split(/\n|/); | |
const WIDTH = 100; | |
const HEIGHT = 100; | |
class Grid { | |
constructor(width, height, cells) { | |
this.width = width; | |
this.height = height; | |
this.cells = cells; | |
this.onSymbol = '#'; | |
this.offSymbol = '.'; | |
} | |
getCell(x, y) { | |
return this.cells[this.width * x + y]; | |
} | |
setCell(x, y, value) { | |
this.cells[this.width * x + y] = value; | |
return this; | |
} | |
toggleCell(x, y) { | |
return this.setCell(x, y, !this.isOn(x, y)); | |
} | |
isOn(x, y) { | |
return this.getCell(x, y) === this.onSymbol; | |
} | |
isOff(x, y) { | |
return this.getCell(x, y) === this.offSymbol; | |
} | |
isInGrid(x, y) { | |
return (x >= 0 && x < this.width && y >= 0 && y < this.height); | |
} | |
getNeighboursCount(x, y) { | |
let count = this.isOn(x, y) ? -1 : 0; | |
for (let yD = 0; yD < 3; yD++) { | |
for (let xD = 0; xD < 3; xD++) { | |
if (this.isInGrid(x + xD - 1, y + yD - 1) && this.isOn(x + xD - 1, y + yD - 1)) { | |
count++; | |
} | |
} | |
} | |
return count; | |
} | |
tick() { | |
let cells = new Array(this.width * this.height).fill(this.offSymbol); | |
for (let y = 0; y < this.height; y++) { | |
for (let x = 0; x < this.width; x++) { | |
let onLightsCount = this.getNeighboursCount(x, y); | |
if (this.isOn(x, y)) { | |
if (onLightsCount === 2 || onLightsCount === 3) { | |
cells[this.width * x + y] = this.onSymbol; | |
} | |
} else if (onLightsCount === 3) { | |
cells[this.width * x + y] = this.onSymbol; | |
} | |
} | |
} | |
this.cells = cells; | |
} | |
render() { | |
for (let y = 0; y < this.height; y++) { | |
for (let x = 0; x < this.width; x++) { | |
process.stdout.write(this.isOn(x, y) ? this.onSymbol : this.offSymbol); | |
} | |
process.stdout.write('\n'); | |
} | |
} | |
} | |
let grid = new Grid(WIDTH, HEIGHT, INPUT); | |
for (let i = 0; i < 100; i++) grid.tick(); | |
const result = grid.cells.reduce((total, cell) => cell === '#' ? ++total : total, 0); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment