forked from vanessayuenn/advent-of-code-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.js
32 lines (29 loc) · 730 Bytes
/
day18.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const input = require('./day18-input.js');
const NUM_ROW = 40;
const possibleTraps = [
'^^.'
, '.^^'
, '^..'
, '..^'
];
const isTrap = (input) => possibleTraps.includes(input);
let rowCount = 1;
let row = input;
let safeTiles = row
.split('')
.reduce((total, x) => x === '.' ? total+1 : total, 0);
while (rowCount < NUM_ROW) {
console.log(row, safeTiles);
let nextRow = '';
for (let i = 0; i < row.length; i++) {
if (isTrap(`${row[i-1] ? row[i-1] : '.'}${row[i]}${row[i+1] ? row[i+1] : '.'}`)) {
nextRow += '^';
} else {
nextRow += '.';
safeTiles++;
}
}
row = nextRow;
rowCount++;
}
console.log(`there are in total ${safeTiles} safe tiles.`);