Skip to content
Permalink
Browse files

Day 18

  • Loading branch information...
jacobemerick committed Dec 19, 2016
1 parent 9ec80c5 commit e536d71bf5bf212b2325397697c3adacfe0338cb
Showing with 39 additions and 0 deletions.
  1. +38 −0 18/index.js
  2. +1 −0 18/input
@@ -0,0 +1,38 @@
const Puzzle = require('@thefotios/advent_puzzle');

const p = new Puzzle();
const rows = (process.argv[3] === 'B') ? 400000 : 40;

const nextRow = prevRow => {
let row = '';
for (let tile = 0; tile < prevRow.length; tile++) {
const leftTile = (tile > 0) ? prevRow[tile - 1] : '.';
const centerTile = prevRow[tile];
const rightTile = (tile < (prevRow.length - 1)) ? prevRow[tile + 1] : '.';
row += (
(leftTile === '^' && centerTile === '^' && rightTile === '.') ||
(leftTile === '.' && centerTile === '^' && rightTile === '^') ||
(leftTile === '^' && centerTile === '.' && rightTile === '.') ||
(leftTile === '.' && centerTile === '.' && rightTile === '^')
) ? '^' : '.';
}
return row;
};

p.before = lines => {
const room = lines.slice(0);
for (let i = 1; i < rows; i++) {
room.push(nextRow(room[i - 1]));
}
return room;
};

p.after = room => {
let safeTiles = 0;
for (let row of room) {
safeTiles += (row.match(/\./g)||[]).length;
}
return safeTiles;
};

p.run();
@@ -0,0 +1 @@
.^^^.^.^^^.^.......^^.^^^^.^^^^..^^^^^.^.^^^..^^.^.^^..^.^..^^...^.^^.^^^...^^.^.^^^..^^^^.....^....

0 comments on commit e536d71

Please sign in to comment.
You can’t perform that action at this time.