-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10-part2.cpp
59 lines (46 loc) · 1.34 KB
/
day10-part2.cpp
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
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#include "Location.hpp"
#include "los.hpp"
int main() {
std::set<Location> asteroids;
coordinate n = 1;
for (coordinate y = 0; y < n; ++y) {
std::string line;
std::getline(std::cin, line);
n = line.size();
for (coordinate x = 0; x < n; ++x) {
if (line[x] != '.') {
asteroids.emplace(x, y);
}
}
}
size_t visibleNumber = 0;
Location origin{0, 0};
for (const Location &asteroid : asteroids) {
std::set<Location> visible = getVisible(asteroids, asteroid, n);
if (visible.size() > visibleNumber) {
visibleNumber = visible.size();
origin = asteroid;
}
}
asteroids.erase(origin);
auto compare = [&origin](const Location &a, const Location &b) {
return a.phiAround(origin) < b.phiAround(origin);
};
std::vector<Location> vaporization;
while (!asteroids.empty() && vaporization.size() < 200) {
std::set<Location> visible = getVisible(asteroids, origin, n);
std::vector<Location> turn(visible.begin(), visible.end());
for (const Location &asteroid : visible) {
asteroids.erase(asteroid);
}
std::sort(turn.begin(), turn.end(), compare);
vaporization.insert(vaporization.end(), turn.begin(), turn.end());
}
const Location &asteroid200 = vaporization.at(199);
std::cout << asteroid200.x * 100 + asteroid200.y << std::endl;
return 0;
}