-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.js
51 lines (47 loc) · 1.25 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
var fs = require('fs')
function load(file) {
var input = fs.readFileSync(file, 'utf8')
let points = []
input.split("\n").forEach(line => {
let items = line.split(" ")
points.push({type:items[0], distance: +items[1]})
})
return points
}
function runDay02(file) {
let actions = load(file)
let depth = 0
let x = 0
actions.forEach(action => {
switch(action.type) {
case "forward": x+= action.distance; break;
case "up": depth -= action.distance; break;
case "down": depth += action.distance; break;
}
})
console.log("step1", file, x*depth)
}
function runDay02Step2(file) {
let actions = load(file)
let depth = 0
let x = 0
let aim = 0
actions.forEach(action => {
switch(action.type) {
case "forward":
x += action.distance;
depth += aim*action.distance;
break;
case "up":
aim -= action.distance;
break;
case "down": aim += action.distance;
break;
}
})
console.log("step2", file, x*depth)
}
runDay02('input-test.txt')
runDay02('input.txt')
runDay02Step2('input-test.txt')
runDay02Step2('input.txt')