-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24.cpp
More file actions
495 lines (403 loc) · 18.1 KB
/
day24.cpp
File metadata and controls
495 lines (403 loc) · 18.1 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "day24.h"
#include "helpers.h"
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <ranges>
#include <sstream>
#include <string>
#include <vector>
namespace day24
{
long long solvePart1(std::ifstream& file, bool example);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 24 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day24_example.txt" : "inputs/day24_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file, example) << '\n';
file.close();
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
struct Point
{
double x{};
double y{};
double z{};
[[nodiscard]] Point operator+(const Point& other) const
{
return Point{ x + other.x, y + other.y, z + other.z };
}
[[nodiscard]] Point operator-(const Point& other) const
{
return Point{ x - other.x, y - other.y, z - other.z };
}
[[nodiscard]] Point operator*(const double factor) const
{
return Point{ factor * x, factor * y, factor * z };
}
};
struct Hailstone
{
Point origin{};
Point velocity{};
[[nodiscard]] Point afterOneInterval() const
{
return Point{ origin.x + velocity.x, origin.y + velocity.y, origin.z + velocity.z };
}
[[nodiscard]] std::pair<bool, Point> intersectPointInTwoDimensions(const Hailstone& other) const
{
const Point secondPoint{ afterOneInterval() };
const Point otherSecondPoint{ other.afterOneInterval() };
const double denominator = (origin.x - secondPoint.x) * (other.origin.y - otherSecondPoint.y) - (origin.y - secondPoint.y) * (other.origin.x - otherSecondPoint.x);
if (std::abs(denominator) < 0.000001)
{
// No intersection
return std::pair{ false, Point{} };
}
const double xNumerator = (origin.x * secondPoint.y - origin.y * secondPoint.x) * (other.origin.x - otherSecondPoint.x) - (origin.x - secondPoint.x) * (other.origin.x * otherSecondPoint.y - other.origin.y * otherSecondPoint.x);
const double yNumerator = (origin.x * secondPoint.y - origin.y * secondPoint.x) * (other.origin.y - otherSecondPoint.y) - (origin.y - secondPoint.y) * (other.origin.x * otherSecondPoint.y - other.origin.y * otherSecondPoint.x);
double x = xNumerator / denominator;
double y = yNumerator / denominator;
return std::pair{ true, Point{x, y, 0} };
}
[[nodiscard]] bool isFutureCoincidentWithOnXY(const Hailstone& other) const
{
const Point secondPoint{ afterOneInterval() };
const Point otherSecondPoint{ other.afterOneInterval() };
const double denominator = (origin.x - secondPoint.x) * (other.origin.y - otherSecondPoint.y) - (origin.y - secondPoint.y) * (other.origin.x - otherSecondPoint.x);
if (std::abs(denominator) >= 0.000001)
{
return false;
}
// parallel or coincident
// Check for coincident case
auto dx = origin.x - other.origin.x;
auto dy = origin.y - other.origin.y;
auto dt = dx / velocity.x;
if (std::abs(dt * velocity.y - dy) < 0.00001)
{
if (dt >= 0)
{
return true;
}
}
// In other direction
dx = other.origin.x - origin.x;
dy = other.origin.y - origin.y;
dt = dx / other.velocity.x;
if (std::abs(dt * other.velocity.y - dy) < 0.00001)
{
if (dt >= 0)
{
return true;
}
}
return false;
}
[[nodiscard]] std::pair<bool, Point> futureIntersectPointInTwoDimensions(const Hailstone& other) const
{
const Point secondPoint{ afterOneInterval() };
const Point otherSecondPoint{ other.afterOneInterval() };
const double denominator = (origin.x - secondPoint.x) * (other.origin.y - otherSecondPoint.y) - (origin.y - secondPoint.y) * (other.origin.x - otherSecondPoint.x);
if (std::abs(denominator) < 0.000001)
{
return std::pair{ false, Point{} };
}
const double tNumerator = (origin.x - other.origin.x) * (other.origin.y - otherSecondPoint.y) - (origin.y - other.origin.y) * (other.origin.x - otherSecondPoint.x);
const double otherTNumerator = (origin.x - other.origin.x) * (origin.y - secondPoint.y) - (origin.y - other.origin.y) * (origin.x - secondPoint.x);
const double t = tNumerator / denominator;
const double otherT = otherTNumerator / denominator;
if (t < 0 || otherT < 0)
{
// In the past
return std::pair{ false, Point{} };
}
Point intersection{ origin + velocity * t};
return std::pair{ true, intersection };
}
[[nodiscard]] bool isFutureCoincidentWithOnXZ(const Hailstone& other) const
{
const Point secondPoint{ afterOneInterval() };
const Point otherSecondPoint{ other.afterOneInterval() };
const double denominator = (origin.x - secondPoint.x) * (other.origin.z - otherSecondPoint.z) - (origin.z - secondPoint.z) * (other.origin.x - otherSecondPoint.x);
if (std::abs(denominator) >= 0.000001)
{
return false;
}
// parallel or coincident
// Check for coincident case
auto dx = origin.x - other.origin.x;
auto dz = origin.z - other.origin.z;
auto dt = dx / velocity.x;
if (std::abs(dt * velocity.z - dz) < 0.00001)
{
if (dt >= 0)
{
return true;
}
}
// In other direction
dx = other.origin.x - origin.x;
dz = other.origin.z - origin.z;
dt = dx / other.velocity.x;
if (std::abs(dt * other.velocity.z - dz) < 0.00001)
{
if (dt >= 0)
{
return true;
}
}
return false;
}
[[nodiscard]] std::pair<bool, Point> futureIntersectPointInTwoDimensionsOnXAndZ(const Hailstone& other) const
{
const Point secondPoint{ afterOneInterval() };
const Point otherSecondPoint{ other.afterOneInterval() };
const double denominator = (origin.x - secondPoint.x) * (other.origin.z - otherSecondPoint.z) - (origin.z - secondPoint.z) * (other.origin.x - otherSecondPoint.x);
if (std::abs(denominator) < 0.000001)
{
return std::pair{ false, Point{} };
}
const double tNumerator = (origin.x - other.origin.x) * (other.origin.z - otherSecondPoint.z) - (origin.z - other.origin.z) * (other.origin.x - otherSecondPoint.x);
const double otherTNumerator = (origin.x - other.origin.x) * (origin.z - secondPoint.z) - (origin.z - other.origin.z) * (origin.x - secondPoint.x);
const double t = tNumerator / denominator;
const double otherT = otherTNumerator / denominator;
if (t < 0 || otherT < 0)
{
// In the past
return std::pair{ false, Point{} };
}
Point intersection{ origin + velocity * t };
return std::pair{ true, intersection };
}
};
bool doublesAreSignificantlyDifferent(const double a, const double b)
{
const auto diff{ std::abs(a - b) };
if (diff < 0.00001)
{
return false;
}
if (diff < (std::max(a, b) * 0.00000001))
{
return false;
}
return true;
}
struct Storm
{
std::vector<Hailstone> stones{};
void parseInput(std::ifstream& file)
{
while (!file.eof())
{
std::string line;
std::getline(file, line);
std::stringstream ss{};
ss << line;
double x, y, z;
ss >> x;
ss.ignore(1);
ss >> y;
ss.ignore(1);
ss >> z;
Point origin{ x, y, z };
ss.ignore(3);
ss >> x;
ss.ignore(1);
ss >> y;
ss.ignore(1);
ss >> z;
Point velocity{ x, y, z };
stones.emplace_back(origin, velocity);
}
}
[[nodiscard]] long long countInterectionsInArea(const double areaLower, const double areaUpper) const
{
long long count{};
for (size_t i = 0; i < stones.size(); i++)
{
for (size_t j = i + 1; j < stones.size(); j++)
{
auto& a{ stones[i] };
auto& b{ stones[j] };
const auto [exists, p] { a.futureIntersectPointInTwoDimensions(b) };
if (exists && p.x >= areaLower && p.x <= areaUpper && p.y >= areaLower && p.y <= areaUpper)
{
count++;
}
}
}
return count;
}
[[nodiscard]] long long solveBreakingThrow() const
{
// Approach is to solve for x+y & x+z seperately
// We approach solving by reframing coordinates, at t=0 we use the 'normal' coordinate frame
// but after we use the coordinate frame shifted by breaking rock velocity * t.
// Effect is that at any t, the breaking rock is at it's starting location (it effectively has v=0 in
// this moving frame).
// All falling hailstone gets their velocity modified by rock velocity
// Any valid solution should have all hailstone 'go' through the now static location of the throwing rock
// but we don't know where it is. So instead check if there is a single point all hailstone intersect,
// by just taking intersection point for each pair & validating if all intersect & at the same spot.
//
// Running the above for 'reasonable' ranges of vx, vy & vz, we can check if any combination would be valid
// This works well for example & real input, resulting in exactly 1 combination for both
//
// Because the rock is static in our moving frame, including at t=0 & our frame is identical to regular frame at t=0,
// the static location of our rock is equal to the location of rock at t=0 (origin) & we know this location in our moving frame
// from the intersection checks.
// Note below code could be refactored a fair bit since logic on x+y & x+z is identical apart from the coordinate used.
std::vector<std::pair<std::pair<double, double>, Point>> possibleXYVelocity{};
for (int vx = -500; vx < 500; vx++)
{
for (int vy = -500; vy < 500; vy++)
{
if (vy == 0 || vx == 0)
{
continue;
}
Point breakingVelocity{ static_cast<double>(vx), static_cast<double>(vy), 0 };
bool failed{ false };
bool intersectFound{ false };
Point intersectPoint{};
long long pairsSucceeded{};
for (size_t i = 0; i < stones.size(); i++)
{
for (size_t j = i + 1; j < stones.size(); j++)
{
auto a{ stones[i] };
auto b{ stones[j] };
// Adjust velocities by possible breaking stone velocity
a.velocity = a.velocity - breakingVelocity;
b.velocity = b.velocity - breakingVelocity;
if (a.isFutureCoincidentWithOnXY(b))
{
// annoying case, for now assume it hits the general intercept point
pairsSucceeded++;
continue;
}
const auto [exists, p] { a.futureIntersectPointInTwoDimensions(b) };
if (!exists)
{
failed = true;
break;
}
if (!intersectFound)
{
intersectFound = true;
intersectPoint = p;
pairsSucceeded++;
continue;
}
// Ran into significant issue with double accuracy here...
if (doublesAreSignificantlyDifferent(intersectPoint.x, p.x)
|| doublesAreSignificantlyDifferent(intersectPoint.y, p.y))
{
failed = true;
break;
}
pairsSucceeded++;
}
if (failed)
{
break;
}
}
if (!failed)
{
possibleXYVelocity.emplace_back(std::pair{ vx, vy }, intersectPoint);
}
}
}
std::vector<std::pair<std::pair<double, double>, Point>> possibleXZVelocity{};
for (int vx = -500; vx < 500; vx++)
{
for (int vz = -500; vz < 500; vz++)
{
if (vz == 0 || vx == 0)
{
continue;
}
Point breakingVelocity{ static_cast<double>(vx), 0, static_cast<double>(vz) };
bool failed{ false };
bool intersectFound{ false };
Point intersectPoint{};
long long pairsSucceeded{};
for (size_t i = 0; i < stones.size(); i++)
{
for (size_t j = i + 1; j < stones.size(); j++)
{
auto a{ stones[i] };
auto b{ stones[j] };
// Adjust velocities by possible breaking stone velocity
a.velocity = a.velocity - breakingVelocity;
b.velocity = b.velocity - breakingVelocity;
if (a.isFutureCoincidentWithOnXZ(b))
{
// annoying case, for now assume it hits the general intercept point
pairsSucceeded++;
continue;
}
const auto [exists, p] { a.futureIntersectPointInTwoDimensionsOnXAndZ(b) };
if (!exists)
{
failed = true;
break;
}
if (!intersectFound)
{
intersectFound = true;
intersectPoint = p;
pairsSucceeded++;
continue;
}
// Running into significant issue with double accuracy here...
if (doublesAreSignificantlyDifferent(intersectPoint.x, p.x)
|| doublesAreSignificantlyDifferent(intersectPoint.z, p.z))
{
failed = true;
break;
}
pairsSucceeded++;
}
if (failed)
{
break;
}
}
if (!failed)
{
possibleXZVelocity.emplace_back(std::pair{ vx, vz }, intersectPoint);
}
}
}
assert(possibleXYVelocity.size() == 1);
assert(possibleXZVelocity.size() == 1);
assert(!doublesAreSignificantlyDifferent(possibleXYVelocity[0].first.first, possibleXZVelocity[0].first.first));
assert(!doublesAreSignificantlyDifferent(possibleXYVelocity[0].second.x, possibleXZVelocity[0].second.x));
Point solutionVelocity{ possibleXYVelocity[0].first.first, possibleXYVelocity[0].first.second, possibleXZVelocity[0].first.first };
Point solutionOrigin{ possibleXYVelocity[0].second.x, possibleXYVelocity[0].second.y, possibleXZVelocity[0].second.z };
double sumOfCoordinates{ solutionOrigin.x + solutionOrigin.y + solutionOrigin.z };
return static_cast<long long>(sumOfCoordinates);
}
};
long long solvePart1(std::ifstream& file, bool example)
{
Storm storm{};
storm.parseInput(file);
return storm.countInterectionsInArea(example ? 7.0 : 200000000000000.0, example ? 27.0 : 400000000000000.0);
}
long long solvePart2(std::ifstream& file)
{
Storm storm{};
storm.parseInput(file);
return storm.solveBreakingThrow();
}
}