-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
236 lines (214 loc) · 8.3 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import run from "aocrunner";
Array.prototype.toString = function() {
return `[${this.join(",")}]`;
}
interface Detection {
sensor: number[];
beacon: number[];
distance: number
}
const manhattanDistance = (a: number[], b: number[]): number => {
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
}
const positionInRange = (position: number[], detection: Detection): boolean => {
// console.log(`===${position} vs ${detection.sensor}, ${detection.beacon}, ${detection.distance}===`)
// console.log(`Checking ${position[0]} against x check: ${detection.sensor[0] - detection.distance} to ${(detection.sensor[0] + detection.distance)}`);
// console.log(`Checking ${position[1]} against y check: ${detection.sensor[1] - detection.distance} to ${(detection.sensor[1] + detection.distance)}`);
return manhattanDistance(position, detection.sensor) <= detection.distance;
};
const parseInput = (rawInput: string) => {
const detections: Detection[] = [];
rawInput.split("\n").forEach(line => {
const match = line
.match(/Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)/)!!
.map(Number);
const sensor = [match[1], match[2]];
const beacon = [match[3], match[4]];
detections.push({
sensor,
beacon,
distance: manhattanDistance(sensor, beacon),
});
});
return detections;
}
const parseInput2 = (rawInput: string) => {
const detections: SnazzyDetection[] = [];
let count = 0;
rawInput.split("\n").forEach(line => {
console.log(`Detection ${++count}`);
const match = line
.match(/Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)/)!!
.map(Number);
const sensor = [match[1], match[2]];
const beacon = [match[3], match[4]];
detections.push(new SnazzyDetection(sensor, beacon));
});
return detections;
}
const part1 = (rawInput: string) => {
const detections = parseInput(rawInput);
const minX = detections.map(d => [d.sensor[0], d.beacon[0], d.sensor[0] - d.distance]).flat(1)
.sort((a, b) => a - b)[0];
const minY = detections.map(d => [d.sensor[1], d.beacon[1], d.sensor[1] - d.distance]).flat(1)
.sort((a, b) => a - b)[0];
const maxX = detections.map(d => [d.sensor[0], d.beacon[0], d.sensor[0] + d.distance]).flat(1)
.sort((a, b) => b - a)[0];
const maxY = detections.map(d => [d.sensor[1], d.beacon[1], d.sensor[1] + d.distance]).flat(1)
.sort((a, b) => b - a)[0];
return 1;
const row = 2000000;
let excludedPositions = 0;
for (let x = minX; x <= maxX; x++) {
const checkPosition = [x, row];
let detected = false;
let detectionIndex = 0;
while (!detected && detectionIndex < detections.length) {
detected = positionInRange(checkPosition, detections[detectionIndex++]);
}
// console.log(`[${x}, ${row}] ${detectedArr}`);
if (detected && !detections.map(d => d.beacon.toString()).includes(checkPosition.toString())) {
excludedPositions++;
}
}
return excludedPositions;
};
class SnazzyDetection implements Detection {
beacon: number[];
distance: number;
sensor: number[];
edges: number[][];
constructor(sensor: number[], beacon: number[]) {
this.sensor = sensor;
this.beacon = beacon;
this.distance = manhattanDistance(sensor, beacon);
this.edges = [];
const minX = this.sensor[0] - this.distance;
// const maxX = Math.min(4000000, this.sensor[0] + this.distance);
const maxX = this.sensor[0] + this.distance;
// console.log(`minX ${minX}, maxX ${maxX}, minY ${minY}, maxY ${maxY}`)
let yMod = 0;
let count = 0;
for (let x = minX; x <= maxX; x++) {
if (count % 100000 === 0) {
console.log(`column count is ${count++}`);
}
const midY = sensor[1];
// console.log(`yMod: ${yMod} midY: ${midY}`);
const foo = [midY + yMod, midY - yMod];
// console.log(foo);
// const nextBigY = Math.min(midY + yMod, 20);
const nextBigY = Math.min(midY + yMod, 4000000);
const nextSmallY = Math.max(midY - yMod, 0);
// if (x >= 0 && x <= 20) {
if (x >= 0 && x <= 4000000) {
this.edges[x] = [nextBigY, nextSmallY];
}
// console.log(this.edges[x]);
if (x < sensor[0]) {
yMod++;
} else if (x >= sensor[0]) {
yMod--;
}
}
// console.log(this.edges);
}
}
const part2 = (rawInput: string) => {
const detections = parseInput2(rawInput);
const collectedEdges = detections.reduce((accum, next) => {
next.edges.forEach((set, index) => {
if (!accum[index]) {
accum[index] = [];
}
accum[index].push(set.sort((a, b) => a - b));
});
return accum;
}, [] as number[][][]);
// console.log(collectedEdges.map((value, index) => `${index}: ${value}`));
const edgesMap: number[][][] = [];
collectedEdges.forEach((edges, xIndex) => {
let hitBottom = 0;
while (edges.length > 1 && hitBottom < 2) {
// console.log(`==== ${xIndex}`)
const edge = edges.pop()!!;
// console.log(edge);
// console.log(edges);
const foundIndex = edges.findIndex(value => {
const edgeIntersectsValue = (value[0] <= edge[0] && edge[0] <= value[1]) || (value[0] <= edge[1] && edge[1] <= value[1]);
const valueIntersectsEdge = (edge[0] <= value[0] && value[0] <= edge[1]) || (edge[0] <= value[1] && value[1] <= edge[1]);
// console.log(`value: ${value} edge ${edge} - eiv ${edgeIntersectsValue} vie ${valueIntersectsEdge}`)
const edgeBordersValue = value[1] === edge[0] + 1 || edge[0] === value[1] + 1;
const valueBordersEdge = edge[1] === value[0] + 1 || edge[1] === value[0] + 1;
// console.log(`value: ${value} edge ${edge} - ebv ${edgeBordersValue} vbe ${valueBordersEdge}`)
return edgeIntersectsValue || valueIntersectsEdge || edgeBordersValue || valueBordersEdge;
});
// console.log(foundIndex);
if (foundIndex < 0) {
hitBottom++;
edges.unshift(edge);
continue;
}
const foundEdge = edges.splice(foundIndex, 1)[0];
const combinedEdge = [Math.min(foundEdge[0], edge[0], foundEdge[1], edge[1]), Math.max(foundEdge[0], edge[0], foundEdge[1], edge[1])];
edges.push(combinedEdge);
}
edgesMap[xIndex] = edges;
});
// console.log(edgesMap.map((value, index) => `${index}: ${value}`))
const safeX = edgesMap.findIndex(value => value.length > 1);
const safeColumnRanges = edgesMap[safeX];
const safeY = safeColumnRanges.flat(2).sort((a, b) => a - b )[1] + 1;
return (safeX * 4000000) + safeY;
};
run({
part1: {
tests: [
// {
// input: `
// Sensor at x=2, y=18: closest beacon is at x=-2, y=15
// Sensor at x=9, y=16: closest beacon is at x=10, y=16
// Sensor at x=13, y=2: closest beacon is at x=15, y=3
// Sensor at x=12, y=14: closest beacon is at x=10, y=16
// Sensor at x=10, y=20: closest beacon is at x=10, y=16
// Sensor at x=14, y=17: closest beacon is at x=10, y=16
// Sensor at x=8, y=7: closest beacon is at x=2, y=10
// Sensor at x=2, y=0: closest beacon is at x=2, y=10
// Sensor at x=0, y=11: closest beacon is at x=2, y=10
// Sensor at x=20, y=14: closest beacon is at x=25, y=17
// Sensor at x=17, y=20: closest beacon is at x=21, y=22
// Sensor at x=16, y=7: closest beacon is at x=15, y=3
// Sensor at x=14, y=3: closest beacon is at x=15, y=3
// Sensor at x=20, y=1: closest beacon is at x=15, y=3`,
// expected: 26,
// },
],
solution: part1,
},
part2: {
tests: [
{
// Sensor at x=8, y=7: closest beacon is at x=2, y=10`,
input: `
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3`,
expected: 56000011,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});