-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.ts
92 lines (80 loc) · 1.71 KB
/
01.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
import Task, { TaskPartSolution } from "../utils/task.js";
const part1: TaskPartSolution = (input) => {
const instructions = input.split(", ");
let x = 0;
let y = 0;
let direction = 0;
for (const instruction of instructions) {
const turn = instruction[0];
const distance = Number(instruction.slice(1));
if (turn === "R") {
direction = (direction + 1) % 4;
} else {
direction = (direction + 3) % 4;
}
switch (direction) {
case 0:
y += distance;
break;
case 1:
x += distance;
break;
case 2:
y -= distance;
break;
case 3:
x -= distance;
break;
}
}
return Math.abs(x) + Math.abs(y);
};
const part2: TaskPartSolution = (input) => {
const instructions = input.split(", ");
let x = 0;
let y = 0;
let direction = 0;
const visited = new Set<string>();
for (const instruction of instructions) {
const turn = instruction[0];
const distance = Number(instruction.slice(1));
if (turn === "R") {
direction = (direction + 1) % 4;
} else {
direction = (direction + 3) % 4;
}
for (let i = 0; i < distance; i++) {
switch (direction) {
case 0:
y++;
break;
case 1:
x++;
break;
case 2:
y--;
break;
case 3:
x--;
break;
}
const key = `${x},${y}`;
if (visited.has(key)) {
return Math.abs(x) + Math.abs(y);
}
visited.add(key);
}
}
return -1;
};
const task = new Task(2016, 1, part1, part2, {
part1: {
input: ``,
result: "",
},
part2: {
input: ``,
result: "",
},
});
export default task;