-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
76 lines (56 loc) · 1.57 KB
/
index.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
function getCode(
instructions: string[],
buttons: (string | undefined)[][],
start: [number, number],
) {
const codes: (string | undefined)[] = [];
let [x, y] = start;
let currentButton = buttons[y][x];
instructions.forEach((instruction) => {
const moves = instruction.split('');
moves.forEach((move) => {
if (move === 'U') {
const nextButton = buttons[y - 1]?.[x];
y = nextButton ? y - 1 : y;
}
if (move === 'R') {
const nextButton = buttons[y]?.[x + 1];
x = nextButton ? x + 1 : x;
}
if (move === 'D') {
const nextButton = buttons[y + 1]?.[x];
y = nextButton ? y + 1 : y;
}
if (move === 'L') {
const nextButton = buttons[y]?.[x - 1];
x = nextButton ? x - 1 : x;
}
currentButton = buttons[y][x];
});
codes.push(currentButton);
});
return codes.join('');
}
function part1(instructions: string[]): string {
const buttons = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
];
// start at `5`
const start: [number, number] = [1, 1];
return getCode(instructions, buttons, start);
}
function part2(instructions: string[]): string {
const buttons = [
[undefined, undefined, '1', undefined, undefined],
[undefined, '2', '3', '4', undefined],
['5', '6', '7', '8', '9'],
[undefined, 'A', 'B', 'C', undefined],
[undefined, undefined, 'D', undefined, undefined],
];
// start at `5`
const start: [number, number] = [0, 2];
return getCode(instructions, buttons, start);
}
export { part1, part2 };