Compare commits

...

2 Commits

Author SHA1 Message Date
Benjamin Kraft f86a8fea43 Day15 2 years ago
Benjamin Kraft d1863f213d start 2 years ago
  1. 92
      src/days/15/Day15.cpp
  2. 9
      src/days/15/Day15.h
  3. 6
      src/util.h

@ -1,9 +1,95 @@
#include "Day15.h"
Result Day15::Task1() {
return Day::Task1();
int64 lowX = INT64_MAX, highX = INT64_MIN;
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() {
return Day::Task2();
}
int64 lowX = INT64_MAX, highX = INT64_MIN;
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);
}

@ -3,6 +3,15 @@
#include "../../Day.h"
class Day15 : public Day {
typedef pair<int64, int64> Point;
static uint64 ManDist(Point p1, Point p2);
struct hashFunction {
size_t operator()(const Point &p) const {
return p.first ^ p.second;
}
};
typedef unordered_set<Point, hashFunction> Set;
void parseSystem(map<Point, uint64>&, Set&, Set&, int64&, int64&);
protected:
Result Task1() override;

@ -6,16 +6,18 @@
#include <tuple>
#include <iostream>
#include <set>
#include <unordered_set>
#include <map>
#include <list>
#include <queue>
typedef uint64_t uint64;
typedef int64_t int64;
using std::stoi, std::to_string;
using std::cout, std::endl;
using std::string, std::vector, std::set, std::pair, std::map;
using std::list;
using std::string, std::vector, std::pair, std::map;
using std::list, std::set, std::unordered_set;
using std::priority_queue;
inline vector<size_t> findAll(const string& data, const string& toSearch){

Loading…
Cancel
Save