diff --git a/2016/18/grid.js b/2016/18/grid.js index 8e8afc4e..3d3b7cc5 100644 --- a/2016/18/grid.js +++ b/2016/18/grid.js @@ -1,5 +1,54 @@ class Grid { - + constructor(first_row) { + this.grid = [first_row.slice(0)]; + } + + /** + * @param {int} n - Number of rows to add onto grid + */ + addRows(n) { + for (let i = 0; i < n; i++) { + let row = []; + let row_above = this.grid[this.grid.length - 1]; + row_above.forEach((tile, t) => { + let left = row_above[t - 1]; + let center = tile; + let right = row_above[t + 1]; + + // `1` is a safe tile + if (left === undefined) left = 1; + if (right === undefined) right = 1; + + /** + * A new tile is a _trap_ only in one of the following situations: + * + * - Its _left_ and _center_ tiles are traps, but its _right_ tile is not. + * - Its _center_ and _right_ tiles are traps, but its _left_ tile is not. + * - Only its _left_ tile is a trap. + * - Only its _right_ tile is a trap. + */ + if ( + (left === 0 && center === 0 && right === 1) || + (left === 1 && center === 0 && right === 0) || + (left === 0 && center === 1 && right === 1) || + (left === 1 && center === 1 && right === 0) + ) { + // Trap tile + row.push(0); + } else { + // Safe tile + row.push(1); + } + }); + + this.grid.push(row); + } + } + + // Assumes safe tiles are stored as a `1` + countSafeTiles() { + return this.grid.map(row => row.reduce((a, b) => a + b, 0)).reduce((a, b) => a + b, 0); + } } module.exports = Grid; diff --git a/2016/18/input.js b/2016/18/input.js index d7bb7886..cec0ee82 100644 --- a/2016/18/input.js +++ b/2016/18/input.js @@ -1,13 +1,15 @@ -// `0` is safe, `1` is trap +// `1` is safe, `0` is trap +// This is because I'm counting safe tiles for the puzzle, +// so its easier to reduce them if they are equal to `1`. module.exports = { sampleInput: { // ..^^. // .^^^^ // ^^..^ small: [ - [0, 0, 1, 1, 0], - [0, 1, 1, 1, 1], [1, 1, 0, 0, 1], + [1, 0, 0, 0, 0], + [0, 0, 1, 1, 0], ], // .^^.^.^^^^ @@ -21,17 +23,17 @@ module.exports = { // .^^^..^.^^ // ^^.^^^..^^ larger: [ - [0, 1, 1, 0, 1, 0, 1, 1, 1, 1], - [1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, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1], }; diff --git a/2016/18/part-one.js b/2016/18/part-one.js index e8455ffb..d01f20f2 100644 --- a/2016/18/part-one.js +++ b/2016/18/part-one.js @@ -1,3 +1,7 @@ const assert = require('assert'); const Grid = require('./grid'); const { input, sampleInput } = require('./input'); + +let grid = new Grid(input); +grid.addRows(39); +console.log(grid.countSafeTiles()); diff --git a/2016/18/part-two.js b/2016/18/part-two.js index e8455ffb..8804401f 100644 --- a/2016/18/part-two.js +++ b/2016/18/part-two.js @@ -1,3 +1,7 @@ const assert = require('assert'); const Grid = require('./grid'); const { input, sampleInput } = require('./input'); + +let grid = new Grid(input); +grid.addRows(400000 - 1); +console.log(grid.countSafeTiles());