-
Notifications
You must be signed in to change notification settings - Fork 4
/
day02.js
57 lines (50 loc) · 1 KB
/
day02.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
const fs = require("fs");
const lines = fs
.readFileSync("day02.txt", { encoding: "utf-8" })
.split("\n")
.filter((x) => Boolean(x))
.map((x) => {
const [direction, n] = x.split(" ");
return {
direction,
x: parseInt(n),
};
});
let submarine = {
position: 0,
depth: 0,
};
for (const line of lines) {
switch (line.direction) {
case "forward":
submarine.position += line.x;
break;
case "down":
submarine.depth += line.x;
break;
case "up":
submarine.depth -= line.x;
break;
}
}
console.log(submarine.position * submarine.depth);
submarine = {
position: 0,
depth: 0,
aim: 0,
};
for (const line of lines) {
switch (line.direction) {
case "forward":
submarine.position += line.x;
submarine.depth += submarine.aim * line.x;
break;
case "down":
submarine.aim += line.x;
break;
case "up":
submarine.aim -= line.x;
break;
}
}
console.log(submarine.position * submarine.depth);