-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2question2.ts
More file actions
38 lines (30 loc) · 797 Bytes
/
day2question2.ts
File metadata and controls
38 lines (30 loc) · 797 Bytes
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
import { readFileSync } from "fs";
let inputs: string[];
const rawData = readFileSync("./day2inputs.txt", "utf8");
inputs = rawData.split("\r\n");
let aim = 0;
let depth = 0;
let horizontalPosition = 0;
for (let input of inputs) {
let seperatorIndex = input.indexOf(" ");
const instruction = input.substring(0, seperatorIndex);
const quantity = Number(input.substring(seperatorIndex + 1));
switch (instruction) {
case "forward": {
horizontalPosition += quantity;
depth += aim * quantity;
break;
}
case "up": {
aim -= quantity;
break;
}
case "down": {
aim += quantity;
break;
}
}
}
console.log(depth);
console.log(horizontalPosition);
console.log(depth * horizontalPosition);