Skip to content

Commit

Permalink
Working part one and two
Browse files Browse the repository at this point in the history
Changed the export storage format, namely, the safe squares are
represented by a `1` instead of a `0`. Makes the reduce a _bit_ easier.
  • Loading branch information
romellem committed Mar 9, 2019
1 parent 45ba386 commit c7812fa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
51 changes: 50 additions & 1 deletion 2016/18/grid.js
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 16 additions & 14 deletions 2016/18/input.js
Original file line number Diff line number Diff line change
@@ -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,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0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 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],
};
4 changes: 4 additions & 0 deletions 2016/18/part-one.js
Original file line number Diff line number Diff line change
@@ -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());
4 changes: 4 additions & 0 deletions 2016/18/part-two.js
Original file line number Diff line number Diff line change
@@ -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());

0 comments on commit c7812fa

Please sign in to comment.