-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-02.js
41 lines (35 loc) · 1.1 KB
/
day-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
const { getArgs, slurpLines } = require("./common");
const [ inputFile ] = getArgs();
const parseLine = (line) => {
const [ direction, amountString ] = line.split(" ");
return {
direction,
amount: parseInt(amountString),
};
};
const main = async () => {
const commands = (await slurpLines(inputFile)).map(parseLine);
const { x: part1x, y: part1y } = commands.reduce(
({ x, y }, { direction, amount }) => {
return {
"forward": { x: x + amount, y },
"down": { x , y: y + amount },
"up": { x , y: y - amount },
}[direction]
},
{ x: 0, y: 0 }
);
console.log(part1x * part1y);
const { x: part2x, y: part2y } = commands.reduce(
({ x, y, aim }, { direction, amount }) => {
return {
"forward": { x: x + amount, y: y + aim * amount, aim },
"down": { x , y , aim: aim + amount },
"up": { x , y , aim: aim - amount },
}[direction]
},
{ x: 0, y: 0, aim: 0 }
);
console.log(part2x * part2y);
};
main();