-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.ts
129 lines (103 loc) · 3.36 KB
/
day21.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { Day } from './Day.ts';
interface Pos { x: number; y: number }
const BFS_DIRECTIONS: { [key: string]: Pos } = {
'^': { x: 0, y: -1 },
'>': { x: 1, y: 0 },
'v': { x: 0, y: 1 },
'<': { x: -1, y: 0 },
};
const DIRECTIONS: { [key: string]: Pos } = {
'X': { x: 0, y: 0 },
'^': { x: 1, y: 0 },
'A': { x: 2, y: 0 },
'<': { x: 0, y: 1 },
'v': { x: 1, y: 1 },
'>': { x: 2, y: 1 },
};
const KEYPAD: { [key: string]: Pos } = {
7: { x: 0, y: 0 },
8: { x: 1, y: 0 },
9: { x: 2, y: 0 },
4: { x: 0, y: 1 },
5: { x: 1, y: 1 },
6: { x: 2, y: 1 },
1: { x: 0, y: 2 },
2: { x: 1, y: 2 },
3: { x: 2, y: 2 },
X: { x: 0, y: 3 },
0: { x: 1, y: 3 },
A: { x: 2, y: 3 },
};
export class DayImpl implements Day {
private readonly input: string[];
constructor(input: string) {
this.input = this.parseInput(input);
}
parseInput(input: string) {
return input
.trim()
.split('\n');
}
partOne() {
const memo: { [key: string]: number } = {};
return this.input.reduce((acc, code) => {
const numerical = Number(code.split('').filter(character => character.match(/\d/)).join(''));
return acc + numerical * getKeyPresses(KEYPAD, code, 2, memo);
}, 0);
}
partTwo() {
const memo: { [key: string]: number } = {};
return this.input.reduce((acc, code) => {
const numerical = Number(code.split('').filter(character => character.match(/\d/)).join(''));
return acc + numerical * getKeyPresses(KEYPAD, code, 25, memo);
}, 0);
}
}
function getKeyPresses(input: { [key: string]: Pos }, code: string, robot: number, memo: { [key: string]: number }): number {
const key = `${code},${robot}`;
if (memo[key] !== undefined) {
return memo[key];
}
let current = 'A';
let length = 0;
for (let i = 0; i < code.length; i++) {
const moves = getCommand(input, current, code[i]);
length += robot === 0 ? moves[0].length : Math.min(...moves.map(move => getKeyPresses(DIRECTIONS, move, robot - 1, memo)));
current = code[i];
}
memo[key] = length;
return length;
}
function getCommand(input: { [key: string]: Pos }, start: string, end: string) {
const queue = [{ ...input[start], path: '' }];
const distances: { [key: string]: number } = {};
if (start === end)
return ['A'];
const allPaths: string[] = [];
while (queue.length) {
const current = queue.shift()!;
const currKey = getKey(current);
if (current.x === input[end].x && current.y === input[end].y)
allPaths.push(`${current.path}A`);
if (distances[currKey] !== undefined && distances[currKey] < current.path.length)
continue;
Object.entries(BFS_DIRECTIONS).forEach(([direction, vector]) => {
const position = { x: current.x + vector.x, y: current.y + vector.y };
if (input.X.x === position.x && input.X.y === position.y)
return;
const button = Object.values(input).find(button => button.x === position.x && button.y === position.y);
if (button) {
const newPath = current.path + direction;
const posKey = getKey(position);
if (distances[posKey] === undefined || distances[posKey] >= newPath.length) {
queue.push({ ...position, path: newPath });
distances[posKey] = newPath.length;
}
}
});
}
return allPaths.sort((a, b) => a.length - b.length);
}
function getKey(pos: Pos) {
return `${pos.x},${pos.y}`;
}