-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.js
54 lines (46 loc) · 1.49 KB
/
Day2.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
require('./utils.js');
module.exports = {
this:part1 = function () {
var h_pos = 0, v_pos = 0;
readFile('../data/day2.txt').forEach((line) => {
var value = Number(line.split(' ')[1]);
switch (line.split(' ')[0])
{
case "forward":
h_pos += value;
break;
case "down":
v_pos += value;
break;
case "up":
v_pos -= value;
break;
default:
console.error("Unsupported direction " + line.split(' ')[0]);
}
})
console.info("Day: 2 | Part: 1 | Result: " + (h_pos * v_pos))
},
this:part2 = function () {
var h_pos = 0, v_pos = 0, aim = 0;
readFile('../data/day2.txt').forEach((line) => {
var value = Number(line.split(' ')[1]);
switch (line.split(' ')[0])
{
case "forward":
h_pos += value;
v_pos += value * aim;
break;
case "down":
aim += value;
break;
case "up":
aim -= value;
break;
default:
console.error("Unsupported direction " + line.split(' ')[0]);
}
})
console.info("Day: 2 | Part: 2 | Result: " + (h_pos * v_pos))
}
}