-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.ts
103 lines (91 loc) · 2.13 KB
/
9.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
import "./utils/helpers.ts";
import { range } from "./utils/helpers.ts";
let input = await Deno.readTextFile("9.txt");
let exampleInput = `R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2`;
let exampleInput2 = `R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20`;
const data = input
.lines()
.trim()
.map((line) => line.split(" "))
.map((v) => [v[0], parseInt(v[1])])
.map((v) => {
return {
move: { R: [1, 0], L: [-1, 0], U: [0, -1], D: [0, 1] }[v[0]] as number[],
amount: v[1] as number,
};
});
interface Point {
x: number;
y: number;
}
function print(knots: Point[]) {
const size = 10;
for (let i = -5; i < size / 2; i++) {
let line = range(size)
.map((_) => ".")
.join("");
for (let j = -5; j < size / 2; j++) {
let foundKnot = false;
for (let k = 0; k < knots.length; k++) {
if (knots[k].x === j && knots[k].y === i && !foundKnot) {
line = line.substring(0, j) + k + line.substring(j + 1);
foundKnot = true;
break;
}
}
}
console.log(line);
}
}
function calcVisited(tailLength: number): number {
const result = data.reduce(
(av, cv) => {
const { move, amount } = cv;
const [x, y] = move;
const { head, tails, visited } = av;
const knots = [head, ...tails];
function moveKnots(a: Point, b: Point) {
const xdiff = Math.abs(a.x - b.x);
const ydiff = Math.abs(a.y - b.y);
if (xdiff > 1 || ydiff > 1) {
if (a.x - b.x > 0) b.x++;
else if (a.x - b.x < 0) b.x--;
if (a.y - b.y > 0) b.y++;
else if (a.y - b.y < 0) b.y--;
}
}
for (let i = 0; i < amount; i++) {
head.x += x;
head.y += y;
for (let k = 0; k < knots.length - 1; k++) {
moveKnots(knots[k], knots[k + 1]);
visited.add(`${tails.last().x},${tails.last().y}`);
}
// print();
}
return av;
},
{
head: { x: 0, y: 0 },
tails: range(tailLength).map((_) => ({ x: 0, y: 0 })),
visited: new Set<string>(),
}
);
return result.visited.size;
}
console.log(calcVisited(1));
console.log(calcVisited(9));