-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.cpp
150 lines (133 loc) · 3.29 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
/*
* day10.cpp
*
* Created on: Dec 10, 2019
* Author: sanja
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <unordered_set>
#include <cmath>
#include <algorithm>
const int noOfVaporized = 200;
std::pair<int, int> laser;
//for putting angle in range (0, 360)
double calculate_angle(std::pair<int, int> laser, std::pair<int, int> asteroid)
{
int delta_x = abs(asteroid.first - laser.first);
int delta_y = abs(asteroid.second - laser.second);
double angle = atan((double) delta_y / (double) delta_x) * 180.0 / (atan(1) * 4);
if (delta_y == 0)
{
if (asteroid.first < laser.first) angle = 0.0;
else angle = 180.0;
}
else if (delta_x == 0)
{
if (asteroid.second > laser.second) angle = 90.0;
else angle = 270.0;
}
else
{
if (asteroid.first < laser.first)
{
if (asteroid.second < laser.second) angle += 270.0;
else angle = 90.0 - angle;
}
else
{
if (asteroid.second < laser.second) angle = 270.0 - angle;
else angle += 90.0;
}
}
return angle;
}
int main() {
std::vector<std::pair<int, int>> asteroids;
std::string line;
int length;
std::ifstream input;
input.open("input.txt");
int row = 0;
while (std::getline(input,line))
{
for(int j = 0; j < line.length(); j++) {
if(line[j] == '#') asteroids.push_back(std::make_pair(j, row));
}
++row;
}
int max = -1, index;
for (int i = 0; i < asteroids.size(); i++)
{
std::vector<double> lines;
for (int j = 0; j < asteroids.size(); j++)
{
if (i == j)
{
continue;
}
lines.push_back(
std::atan2(
(double) asteroids[j].second - asteroids[i].second,
(double) asteroids[j].first - asteroids[i].first));
}
std::sort(lines.begin(), lines.end());
auto it = std::unique(lines.begin(), lines.end());
int num = std::distance(lines.begin(), it);
if (num > max)
{
max = num;
index = i;
}
}
std::cout << max << std::endl;
laser = asteroids[index];
std::map<double, std::vector<std::pair<int, int>>> map;
for (std::pair<int, int> p : asteroids) {
if (p == asteroids[index])
{
continue;
}
double angle = calculate_angle(laser, p);
std::map<double, std::vector<std::pair<int, int>>>::iterator it = map.find(angle);
if (it != map.end())
{
map[angle].push_back(p);
}
else
{
std::vector<std::pair<int, int>> vec;
vec.push_back(p);
map[angle] = vec;
}
}
for (std::map<double, std::vector<std::pair<int, int>>>::iterator it = map.begin(); it != map.end(); ++it)
{
std::sort(it->second.begin(), it->second.end(),
[](const std::pair<int, int> &p1, const std::pair<int, int> &p2)
{
return std::hypot(p1.first - laser.first, p1.second - laser.second)
< std::hypot(p2.first - laser.first, p2.second - laser.second);
});
}
int removed = 1;
while (true)
{
for (std::map<double, std::vector<std::pair<int, int>>>::iterator it = map.begin(); it != map.end(); ++it)
{
std::vector<std::pair<int, int>> vec = it->second;
if (vec.size() == 0)
continue;
if (removed == noOfVaporized)
{
std::cout << 100 * vec[0].first + vec[0].second << std::endl;
return 1;
}
++removed;
it->second.erase(it->second.begin());
}
}
}