-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.js
68 lines (60 loc) · 1.36 KB
/
Day13.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
fs = require('fs');
fs.readFile('Day13.txt', 'utf8', function(err, data) {
if(err) {
return console.log(err);
}
data = data.trim();
lines = data.split('\n');
let layers = new Array(7);
lines.forEach(function(line) {
let match = line.match(/^(\d+):\s(\d+)$/);
let array = new Array(parseInt(match[2]));
array[0] = '.';
layers[match[1]] = {
direction: "forward",
arr: array.slice()
}
});
let severity = 0;
for(var i = 0; i < 89; i++) {
if(layers[i] && layers[i].arr[0] === '.') {
severity += i * layers[i].arr.length;
}
layers.forEach(function(layer) {
let position = layer.arr.indexOf('.');
layer.arr[position] = null;
if (layer.direction === "forward") {
if (position === layer.arr.length - 1) {
layer.direction = "backward";
layer.arr[(position-1)] = '.';
}
else {
layer.arr[(position+1)] = '.';
}
}
else {
if (position === 0) {
layer.direction = "forward";
layer.arr[(position+1)] = '.';
}
else layer.arr[(position-1)] = '.';
}
});
}
console.log("Answer 1: " + severity);
let safe = false;
let picoseconds = 2;
while(!safe) {
safe = true;
for(var i = 0; i < 89; i++) {
if(layers[i] && (picoseconds + i)%(layers[i].arr.length*2-2) === 0) {
safe = false;
break;
}
}
if (safe) {
console.log("Answer 2: " + picoseconds);
}
picoseconds += 2;
}
});