-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.ts
81 lines (72 loc) · 2.12 KB
/
01.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {loadData, sum} from '../shared/utils';
export type DataType = string;
/* istanbul ignore next */
export const parse = async (): Promise<Array<DataType>> =>
(await loadData(2016, 1)).split(', ');
type Direction = 'N' | 'S' | 'E' | 'W';
type Position = [number, number];
const turn = (currentDirection: Direction, turnDirection: string) => {
switch (currentDirection) {
case 'N':
return turnDirection == 'L' ? 'W' : 'E';
case 'S':
return turnDirection == 'L' ? 'E' : 'W';
case 'W':
return turnDirection == 'L' ? 'S' : 'N';
case 'E':
return turnDirection == 'L' ? 'N' : 'S';
}
};
const move = (
position: Position,
direction: Direction,
distance: number
): Position => {
switch (direction) {
case 'N':
return [position[0], position[1] + distance];
case 'S':
return [position[0], position[1] - distance];
case 'W':
return [position[0] - distance, position[1]];
case 'E':
return [position[0] + distance, position[1]];
}
};
export const part1 = (data: Array<DataType>): number => {
return sum(
data
.reduce<[Position, Direction]>(
([position, direction], step) => {
const newDirection = turn(direction, step.substr(0, 1));
const newPosition = move(
position,
newDirection,
Number(step.substr(1))
);
return [newPosition, newDirection];
},
[[0, 0], 'N']
)[0]
.map(Math.abs)
);
// return Math.abs(position[0]) + Math.abs(position[1]);
};
export const part2 = (data: Array<DataType>): number => {
let locations = new Array<string>();
let position: [number, number] = [0, 0];
let direction: Direction = 'N';
for (const step of data) {
const dir = step.substr(0, 1);
const distance = Number(step.substr(1));
direction = turn(direction, dir);
for (let d = 0; d < distance; d += 1) {
position = move(position, direction, 1);
if (locations.includes(position.join('|'))) {
return Math.abs(position[0]) + Math.abs(position[1]);
}
locations.push(position.join('|'));
}
}
return -1;
};