-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.ts
52 lines (48 loc) · 1.44 KB
/
02.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
52
import {loadData, product} from '../shared/utils';
export type DataType = [string, number];
/* istanbul ignore next */
export const parse = async (): Promise<Array<DataType>> =>
(await loadData(2021, 2)).split('\n').map(row => {
const [direction, distance] = row.split(' ');
return [direction, Number(distance)];
});
export const part1 = (data: Array<DataType>): number =>
product(
data.reduce(
([depth, position], movement) => {
switch (movement[0]) {
case 'forward':
return [depth, position + movement[1]];
case 'down':
return [depth + movement[1], position];
case 'up':
return [depth - movement[1], position];
/* istanbul ignore next */
default:
return [depth, position];
}
},
[0, 0]
)
);
export const part2 = (data: Array<DataType>): number =>
product(
data
.reduce(
([aim, depth, position], movement) => {
switch (movement[0]) {
case 'forward':
return [aim, depth + aim * movement[1], position + movement[1]];
case 'down':
return [aim + movement[1], depth, position];
case 'up':
return [aim - movement[1], depth, position];
/* istanbul ignore next */
default:
return [aim, depth, position];
}
},
[0, 0, 0]
)
.slice(1)
);