-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.js
63 lines (54 loc) · 1.04 KB
/
02.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
const parseEntries = (input) =>
input
.trim()
.split('\n')
.map((x) => {
const [command, value] = x.split(' ')
return [command, parseInt(value, 10)]
})
const part1 = (input) => {
const entries = parseEntries(input)
let depth = 0,
horizontal = 0
for (const [command, value] of entries) {
switch (command) {
case 'forward':
horizontal += value
break
case 'down':
depth += value
break
case 'up':
depth -= value
break
default:
}
}
return depth * horizontal
}
const part2 = (input) => {
const entries = parseEntries(input)
let depth = 0,
horizontal = 0,
aim = 0
for (const [command, value] of entries) {
switch (command) {
case 'forward':
horizontal += value
depth += aim * value
break
case 'down':
aim += value
break
case 'up':
aim -= value
break
default:
}
}
return depth * horizontal
}
module.exports = {
part1,
part2,
}