Created
December 16, 2022 18:54
-
-
Save iglesias/8eb3e64ebc311e3797084cd29143e472 to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
struct p {int x, y; p(int ax, int ay) : x(ax), y(ay) {} p() : x(0), y(0) {} bool operator!=(const p &o) { return x != o.x || y != o.y; } }; | |
std::vector<std::pair<p, int>> read_input(); | |
int distance(p p1, p p2); | |
int main() | |
{ | |
auto sensors_and_distances = read_input(); | |
const int idx = 6; | |
auto sensor = sensors_and_distances[idx].first; | |
int d = sensors_and_distances[idx].second; | |
/* | |
std::cout << sensor.x-d-1 << ' ' << sensor.y << '\n'; | |
std::cout << sensor.x << ' ' << sensor.y-d-1 << '\n'; | |
std::cout << sensor.x+d+1 << ' ' << sensor.y << '\n'; | |
std::cout << sensor.x << ' ' << sensor.y+d+1 << '\n'; | |
*/ | |
const std::vector<p> deltas{{+1, -1}, {+1, +1}, {-1, +1}, {-1, -1}}; | |
int jdx = 0; | |
p cur(sensor.x-d-1, sensor.y); | |
const std::vector<p> next{{sensor.x, sensor.y-d-1}, {sensor.x+d+1, sensor.y}, {sensor.x, sensor.y+d+1}, {sensor.x-d-1, sensor.y}}; | |
while(jdx < 4) | |
{ | |
while(cur != next[jdx]) | |
{ | |
//std::cout << cur.x << ' ' << cur.y << ' ' << next[jdx].x << ' ' << next[jdx].y << '\n'; | |
cur.x += deltas[jdx].x; | |
cur.y += deltas[jdx].y; | |
bool possible = true; | |
for(int i = 0; i<static_cast<int>(sensors_and_distances.size()) && possible; i++) | |
{ | |
if(i == idx) continue; | |
if(distance(cur, sensors_and_distances[i].first) <= sensors_and_distances[i].second) possible = false; | |
} | |
if(possible) std::cout << cur.x << ' ' << cur.y << ' ' << 4000000*static_cast<long long>(cur.x) + cur.y << '\n'; | |
} | |
jdx++; | |
} | |
} | |
int distance(p p1, p p2) | |
{ | |
return std::abs(p1.x-p2.x) + std::abs(p1.y-p2.y); | |
} | |
std::vector<std::pair<p, int>> read_input() | |
{ | |
std::string str; | |
// std::set<int> set; | |
std::vector<std::pair<p, int>> sensors_and_distances; | |
while(std::getline(std::cin, str)) | |
{ | |
std::istringstream ss(str); | |
std::string Sensor, at, colon, closest, beacon, is, comma; | |
p s, b; | |
char c, eq; | |
ss >> Sensor >> at >> c >> eq >> s.x >> comma >> c >> eq >> s.y >> colon | |
>> closest >> beacon >> is >> at >> c >> eq >> b.x >> comma >> c >> eq >> b.y; | |
assert(Sensor=="Sensor"); assert(at=="at"); assert(eq=='='); | |
//std::cout << s.x << ' ' << s.y << ' ' << b.x << ' ' << b.y << std::endl; | |
sensors_and_distances.push_back(std::make_pair(s, distance(s, b))); | |
} | |
return sensors_and_distances; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment