-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.js
54 lines (50 loc) · 1.1 KB
/
Day11.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
fs = require('fs');
fs.readFile('Day11.txt', 'utf8', function(err, data) {
if(err) {
return console.log(err);
}
let directions = data.split(',');
let length = directions.length;
directions[length-1] = directions[length-1].split('\n')[0];
let coordinates = {
x:0,
y:0,
z:0
}
let furthest = 0;
let distance = 0;
for (var i = 0; i < length; i++) {
switch(directions[i]) {
case 'n':
coordinates.y++;
coordinates.z--;
break;
case 's':
coordinates.y--;
coordinates.z++;
break;
case 'ne':
coordinates.x++;
coordinates.z--;
break;
case 'se':
coordinates.x++;
coordinates.y--;
break;
case 'nw':
coordinates.y++;
coordinates.x--;
break;
case 'sw':
coordinates.z++;
coordinates.x--;
default:
break;
}
distance = (Math.abs(coordinates.x) + Math.abs(coordinates.y) + Math.abs(coordinates.z))/2;
if(distance > furthest) furthest = distance;
}
distance = (Math.abs(coordinates.x) + Math.abs(coordinates.y) + Math.abs(coordinates.z))/2;
console.log("Answer 1: " + distance);
console.log("Answer 2: " + furthest);
});