Compare commits
No commits in common. 'f86a8fea43214c28b73a424ac1ba751db3a624dc' and '233addac521b60dedad512ed856d271b20b14670' have entirely different histories.
f86a8fea43
...
233addac52
3 changed files with 5 additions and 102 deletions
@ -1,95 +1,9 @@ |
|||||||
#include "Day15.h" |
#include "Day15.h" |
||||||
|
|
||||||
Result Day15::Task1() { |
Result Day15::Task1() { |
||||||
int64 lowX = INT64_MAX, highX = INT64_MIN; |
return Day::Task1(); |
||||||
map<Point, uint64> sensorToBeaconDist; |
|
||||||
Set sensors, beacons; |
|
||||||
parseSystem(sensorToBeaconDist, sensors, beacons, lowX, highX); |
|
||||||
|
|
||||||
int64 count = 0; |
|
||||||
int64 y = 2000000; |
|
||||||
for (int64 x = lowX; x <= highX; x++){ |
|
||||||
if (beacons.contains({x, y})) |
|
||||||
continue; |
|
||||||
for (const auto& pair : sensorToBeaconDist){ |
|
||||||
if (ManDist({x, y}, pair.first) <= pair.second){ |
|
||||||
count++; |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
return to_string(count); |
|
||||||
} |
} |
||||||
|
|
||||||
Result Day15::Task2() { |
Result Day15::Task2() { |
||||||
int64 lowX = INT64_MAX, highX = INT64_MIN; |
return Day::Task2(); |
||||||
map<Point, uint64> sensorToBeaconDist; |
|
||||||
Set sensors, beacons; |
|
||||||
parseSystem(sensorToBeaconDist, sensors, beacons, lowX, highX); |
|
||||||
|
|
||||||
const int64 low = 0; |
|
||||||
const int64 high = 4000000; |
|
||||||
|
|
||||||
for (const auto& pair : sensorToBeaconDist){ |
|
||||||
const Point sensor = pair.first; |
|
||||||
const auto dist = int64(pair.second); |
|
||||||
|
|
||||||
// Check all points touching the diamond, but not inside
|
|
||||||
for (int64 x = -dist - 1; x <= dist + 1; x++){ |
|
||||||
int64 yUp = dist + 1 - std::abs(x); |
|
||||||
int64 yDown = -yUp; |
|
||||||
int64 testX = x + sensor.first; |
|
||||||
if (testX < low || testX > high) |
|
||||||
continue; |
|
||||||
int64 yArr[2] {yUp, yDown}; |
|
||||||
for (int64 &y : yArr){ |
|
||||||
int64 testY = y + sensor.second; |
|
||||||
Point possibleSolution = {testX, testY}; |
|
||||||
if (testY < low || testY > high) |
|
||||||
continue; |
|
||||||
bool overlapped = false; |
|
||||||
for (const auto& testPair : sensorToBeaconDist){ |
|
||||||
if (ManDist(possibleSolution, testPair.first) <= testPair.second){ |
|
||||||
overlapped = true; |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
if (!overlapped){ |
|
||||||
int64 result = testX * 4000000 + testY; |
|
||||||
return to_string(result); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return "Something went wrong"; |
|
||||||
} |
|
||||||
|
|
||||||
void Day15::parseSystem(map<Point, uint64> &sensorToBeaconDist, Set &sensors, Set &beacons, int64& lowX, int64& highX) { |
|
||||||
for (const string &line : input){ |
|
||||||
size_t xPos = line.find('x') + 2; |
|
||||||
size_t yPos = line.find('y') + 2; |
|
||||||
int64 sx = stoi(line.substr(xPos, line.find(',') - xPos)); |
|
||||||
int64 sy = stoi(line.substr(yPos, line.find(':') - yPos)); |
|
||||||
|
|
||||||
xPos = line.find("is at") + 8; |
|
||||||
yPos = line.find('y', xPos) + 2; |
|
||||||
int64 bx = stoi(line.substr(xPos, line.find(',', xPos) - xPos)); |
|
||||||
int64 by = stoi(line.substr(yPos)); |
|
||||||
|
|
||||||
uint64 dist = ManDist({sx, sy}, {bx, by}); |
|
||||||
int64 outerLeft = sx - int64(dist); |
|
||||||
int64 outerRight = sx + int64(dist); |
|
||||||
if (outerLeft < lowX) lowX = outerLeft; |
|
||||||
if (outerRight > highX) highX = outerRight; |
|
||||||
sensorToBeaconDist[{sx, sy}] = dist; |
|
||||||
sensors.insert({sx, sy}); |
|
||||||
beacons.insert({bx, by}); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
uint64 Day15::ManDist(Day15::Point p1, Day15::Point p2) { |
|
||||||
return std::abs(p2.first - p1.first) + std::abs(p2.second - p1.second); |
|
||||||
} |
} |
Loading…
Reference in new issue