-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
p2.ts
44 lines (42 loc) · 1.15 KB
/
p2.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
import { split, task } from '@alexaegis/advent-of-code-lib';
import { Vec2 } from '@alexaegis/advent-of-code-lib/model';
import packageJson from '../package.json';
import { FerryAction, parse } from './parse.function.js';
export const p2 = (input: string): number => {
const position = new Vec2(0, 0);
const waypoint = new Vec2(10, 1);
for (const { action, value } of split(input).map(parse)) {
switch (action) {
case FerryAction.NORTH: {
waypoint.addMut({ x: 0, y: 1 }, { times: value });
break;
}
case FerryAction.EAST: {
waypoint.addMut({ x: 1, y: 0 }, { times: value });
break;
}
case FerryAction.SOUTH: {
waypoint.addMut({ x: 0, y: -1 }, { times: value });
break;
}
case FerryAction.WEST: {
waypoint.addMut({ x: -1, y: 0 }, { times: value });
break;
}
case FerryAction.LEFT: {
waypoint.rotateLeft(value / 90);
break;
}
case FerryAction.RIGHT: {
waypoint.rotateRight(value / 90);
break;
}
case FerryAction.FORWARD: {
position.addMut(waypoint, { times: value });
break;
}
}
}
return Vec2.ORIGIN.manhattan(position);
};
await task(p2, packageJson.aoc); // 39140 ~1.07ms