-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
51 lines (39 loc) · 1 KB
/
index.ts
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
function part1(steps: string[]): number {
let horizontalPosition = 0;
let depth = 0;
steps.forEach((step) => {
const [direction, length] = step.split(' ');
const parsedLength = parseInt(length, 10);
if (direction === 'forward') {
horizontalPosition += parsedLength;
}
if (direction === 'down') {
depth += parsedLength;
}
if (direction === 'up') {
depth -= parsedLength;
}
});
return horizontalPosition * depth;
}
function part2(steps: string[]): number {
let horizontalPosition = 0;
let depth = 0;
let aim = 0;
steps.forEach((step) => {
const [direction, length] = step.split(' ');
const parsedLength = parseInt(length, 10);
if (direction === 'forward') {
horizontalPosition += parsedLength;
depth += aim * parsedLength;
}
if (direction === 'down') {
aim += parsedLength;
}
if (direction === 'up') {
aim -= parsedLength;
}
});
return horizontalPosition * depth;
}
export { part1, part2 };