-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.ts
85 lines (82 loc) · 1.75 KB
/
02.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
import Task, { TaskPartSolution } from "../utils/task.js";
const part1: TaskPartSolution = (input) => {
const lines = input.trim().split("\n");
const keypad = [
["1", "2", "3"],
["4", "5", "6"],
["7", "8", "9"],
];
let x = 1;
let y = 1;
const code: string[] = [];
for (const line of lines) {
for (const char of line) {
switch (char) {
case "U":
y = Math.max(0, y - 1);
break;
case "D":
y = Math.min(2, y + 1);
break;
case "L":
x = Math.max(0, x - 1);
break;
case "R":
x = Math.min(2, x + 1);
break;
}
}
code.push(keypad[y][x]);
}
return code.join("");
};
const part2: TaskPartSolution = (input) => {
const lines = input.trim().split("\n");
const keypad = [
[null, null, "1", null, null],
[null, "2", "3", "4", null],
["5", "6", "7", "8", "9"],
[null, "A", "B", "C", null],
[null, null, "D", null, null],
];
let x = 0;
let y = 2;
const code: string[] = [];
for (const line of lines) {
for (const char of line) {
let newX = x;
let newY = y;
switch (char) {
case "U":
newY = Math.max(0, y - 1);
break;
case "D":
newY = Math.min(4, y + 1);
break;
case "L":
newX = Math.max(0, x - 1);
break;
case "R":
newX = Math.min(4, x + 1);
break;
}
if (keypad[newY][newX] !== null) {
x = newX;
y = newY;
}
}
code.push(keypad[y][x]!);
}
return code.join("");
};
const task = new Task(2016, 2, part1, part2, {
part1: {
input: ``,
result: "",
},
part2: {
input: ``,
result: "",
},
});
export default task;