-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.ts
145 lines (121 loc) · 3.21 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { input } from './input';
const myCodes = input.split('\n');
type Point = [number, number];
const dirs: Map<string, Point> = new Map([
['>', [0, 1]],
['v', [1, 0]],
['^', [-1, 0]],
['<', [0, -1]],
]);
const NumKeypad: Map<string, Point> = new Map([
['7', [0, 0]],
['8', [0, 1]],
['9', [0, 2]],
['4', [1, 0]],
['5', [1, 1]],
['6', [1, 2]],
['1', [2, 0]],
['2', [2, 1]],
['3', [2, 2]],
['X', [3, 0]],
['0', [3, 1]],
['A', [3, 2]],
]);
const DirKeyPad: Map<string, Point> = new Map([
['X', [0, 0]],
['^', [0, 1]],
['A', [0, 2]],
['<', [1, 0]],
['v', [1, 1]],
['>', [1, 2]],
]);
const cache = new Map<string, number>();
function getPaths(keypad: Map<string, Point>, start: string, end: string) {
const startPos = keypad.get(start);
const endPos = keypad.get(end);
const blankPos = keypad.get('X');
if (startPos == null || endPos == null || blankPos == null) {
throw new Error('invalid something');
}
const validCoords = Array.from(keypad.values());
const q: [Point, string][] = [[startPos, '']];
const visited: Map<string, number> = new Map();
if (start === end) {
return ['A'];
}
let minPath = Number.MAX_VALUE;
let allPaths: string[] = [];
while (q.length > 0) {
const [[x, y], path] = q.pop()!;
if (x === endPos[0] && y === endPos[1]) {
allPaths.push(path + 'A');
minPath = Math.min(minPath, path.length + 1);
}
if ((visited.get(`${x},${y}`) ?? Number.MAX_VALUE) < path.length) {
continue;
}
for (let [direction, [dx, dy]] of dirs.entries()) {
const position: Point = [x + dx, y + dy];
if (blankPos[0] === position[0] && blankPos[1] === position[1]) {
continue;
}
const button = validCoords.find(
(c) => c[0] === position[0] && c[1] === position[1],
);
if (button != null) {
const newPath = path + direction;
if (
(visited.get(`${position[0]},${position[1]}`) ?? Number.MAX_VALUE) >=
newPath.length
) {
q.push([position, newPath]);
visited.set(`${position[0]},${position[1]}`, newPath.length);
}
}
}
}
return allPaths.filter((a) => a.length == minPath);
}
function getKeyCnt(
keypad: Map<string, Point>,
code: string,
robot: number,
): number {
const key = `${code},${robot}`;
const cacheVal = cache.get(key);
if (cacheVal != null) {
return cacheVal;
}
let start = 'A';
let length = 0;
for (let c of code) {
const moves = getPaths(keypad, start, c);
if (robot === 0) {
length += moves.reduce(
(min, cur) => Math.min(min, cur.length),
Number.MAX_VALUE,
);
} else {
let newMin = Number.MAX_VALUE;
for (let m of moves) {
const cnt = getKeyCnt(DirKeyPad, m, robot - 1);
newMin = Math.min(newMin, cnt);
}
length += newMin;
}
start = c;
}
cache.set(key, length);
return length;
}
function doWork(codes: string[], robots: number = 2) {
let result = 0;
for (let c of codes) {
const mult = Number(c.substring(0, 3));
const cnt = getKeyCnt(NumKeypad, c, robots);
result += mult * cnt;
}
return result;
}
console.log(doWork(myCodes));
console.log(doWork(myCodes, 25));