-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.cpp
170 lines (150 loc) · 4.18 KB
/
day10.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
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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <vector>
using Vector2 = std::pair<int, int>;
Vector2 operator-(const Vector2& a, const Vector2& b)
{
return {a.first - b.first, a.second - b.second};
}
Vector2 operator+(const Vector2& a, const Vector2& b)
{
return {a.first + b.first, a.second + b.second};
}
Vector2 operator/(const Vector2& a, int d)
{
return {a.first / d, a.second / d};
}
std::ostream& operator<<(std::ostream& out, const Vector2& a)
{
return out << "(" << a.first << ", " << a.second << ")";
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& a)
{
out << "[";
for (size_t i = 0; i < a.size(); i++)
{
out << a[i];
if (i < a.size() - 1)
{
out << ", ";
}
}
return out << "]";
}
Vector2 normalize(const Vector2& direction)
{
if (direction.first == 0 && direction.second == 0)
{
return direction;
}
auto d = std::gcd(direction.first, direction.second);
return direction / d;
}
std::pair<size_t, Vector2> part_a(std::vector<Vector2>& asteroids)
{
size_t most = 0;
Vector2 loc;
for (const auto& asteroid : asteroids)
{
std::set<Vector2> directions;
for (const auto& other : asteroids)
{
if (&asteroid != &other)
{
auto dir = normalize(asteroid - other);
directions.emplace(dir);
}
}
if (directions.size() > most)
{
most = directions.size();
loc = asteroid;
}
most = std::max(most, directions.size());
}
return {most, loc};
}
double angle_from_top(const Vector2& v)
{
return std::fmod(std::atan2(static_cast<double>(-v.second), static_cast<double>(v.first)) + 1.5 * M_PI, 2 * M_PI);
}
int manhattan(const Vector2& a, const Vector2& b)
{
return std::abs(a.first - b.first) + std::abs(a.second - b.second);
}
int part_b(std::vector<Vector2>& asteroids)
{
auto res = part_a(asteroids);
auto location = res.second;
std::map<Vector2, std::vector<Vector2>> asts_per_dir;
for (const auto& other : asteroids)
{
if (location.first != other.first || location.second != other.second)
{
auto relpos = other - location;
auto dir = normalize(relpos);
asts_per_dir[dir].emplace_back(relpos);
}
}
std::vector<Vector2> keys(asts_per_dir.size());
std::transform(asts_per_dir.begin(), asts_per_dir.end(), keys.begin(),
[](std::pair<Vector2, std::vector<Vector2>> el) { return el.first; });
// Sort keys clockwise by angle from top
std::sort(keys.begin(), keys.end(),
[](const Vector2& a, const Vector2& b) { return angle_from_top(a) > angle_from_top(b); });
// Start up
if (keys.back().first == 0 && keys.back().second == -1)
{
keys.insert(keys.begin(), keys.back());
keys.pop_back();
}
// Sort asteroids per direction by distance from location (reverse)
for (const auto& key : keys)
{
std::sort(asts_per_dir[key].begin(), asts_per_dir[key].end(), [&location](const Vector2& a, const Vector2& b) {
return manhattan(a, location) > manhattan(b, location);
});
}
int i = 0;
while (true)
{
for (const auto& key : keys)
{
if (asts_per_dir[key].size() > 0)
{
auto asteroid = asts_per_dir[key].back();
asts_per_dir[key].pop_back();
if (++i == 200)
{
asteroid = asteroid + location;
return asteroid.first * 100 + asteroid.second;
}
}
}
}
}
int main()
{
std::ifstream in("input10.txt");
std::vector<Vector2> asteroids;
std::string line;
for (int y = 0; in.good(); y++)
{
in >> line;
for (int x = 0; x < line.size(); x++)
{
if (line[x] == '#')
{
asteroids.emplace_back(x, y);
}
}
}
std::cout << part_b(asteroids) << std::endl;
return 0;
}