-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.ts
123 lines (99 loc) · 3.23 KB
/
day8.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
import { parseInput } from "../utils/inputParser";
export const solvePart1 = (input: string): number => {
const lines = parseInput(input);
let maxX = 0;
const maxY = lines.length - 1;
// A: [[y1,x1], [y2,x2], ...]
const locations: Record<string, [number, number][]> = {};
lines.forEach((line, y) => {
if (maxX === 0) {
maxX = line.length - 1;
}
line.split("").forEach((char, x) => {
if (char !== ".") {
locations[char.toString()] ??= [];
locations[char.toString()].push([y, x]);
}
});
});
const placeAntinodes = (coordinatesList: [number, number][], i: number, j: number) => {
const [Y1, X1] = coordinatesList[i];
const [Y2, X2] = coordinatesList[j];
const deltaY = Y1 - Y2;
const deltaX = X1 - X2;
const antinode1Y = Y1 + deltaY;
const antinode1X = X1 + deltaX;
while (0 <= antinode1Y && antinode1Y <= maxY && 0 <= antinode1X && antinode1X <= maxX) {
uniqueAntinodes.add([antinode1Y, antinode1X].toString());
break;
}
const antinode2Y = Y2 - deltaY;
const antinode2X = X2 - deltaX;
while (0 <= antinode2Y && antinode2Y <= maxY && 0 <= antinode2X && antinode2X <= maxX) {
uniqueAntinodes.add([antinode2Y, antinode2X].toString());
break;
}
};
const uniqueAntinodes = new Set<string>();
for (const coordinates of Object.values(locations)) {
if (coordinates.length < 2) {
continue;
}
for (let i = 0; i < coordinates.length - 1; i++) {
for (let j = i + 1; j < coordinates.length; j++) {
placeAntinodes(coordinates, i, j);
}
}
}
return uniqueAntinodes.size;
};
export const solvePart2 = (input: string): number => {
const lines = parseInput(input);
let maxX = 0;
const maxY = lines.length - 1;
// A: [[y1,x1], [y2,x2], ...]
const locations: Record<string, [number, number][]> = {};
lines.forEach((line, y) => {
if (maxX === 0) {
maxX = line.length - 1;
}
line.split("").forEach((char, x) => {
if (char !== ".") {
locations[char.toString()] ??= [];
locations[char.toString()].push([y, x]);
}
});
});
const placeAntinodes = (coordinatesList: [number, number][], i: number, j: number) => {
const [Y1, X1] = coordinatesList[i];
const [Y2, X2] = coordinatesList[j];
const deltaY = Y1 - Y2;
const deltaX = X1 - X2;
let antinode1Y = Y1;
let antinode1X = X1;
while (0 <= antinode1Y && antinode1Y <= maxY && 0 <= antinode1X && antinode1X <= maxX) {
uniqueAntinodes.add([antinode1Y, antinode1X].toString());
antinode1Y += deltaY;
antinode1X += deltaX;
}
let antinode2Y = Y2;
let antinode2X = X2;
while (0 <= antinode2Y && antinode2Y <= maxY && 0 <= antinode2X && antinode2X <= maxX) {
uniqueAntinodes.add([antinode2Y, antinode2X].toString());
antinode2Y -= deltaY;
antinode2X -= deltaX;
}
};
const uniqueAntinodes = new Set<string>();
for (const coordinates of Object.values(locations)) {
if (coordinates.length < 2) {
continue;
}
for (let i = 0; i < coordinates.length - 1; i++) {
for (let j = i + 1; j < coordinates.length; j++) {
placeAntinodes(coordinates, i, j);
}
}
}
return uniqueAntinodes.size;
};