-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8a.cpp
More file actions
184 lines (167 loc) · 3.7 KB
/
Copy pathday8a.cpp
File metadata and controls
184 lines (167 loc) · 3.7 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cassert>
#include <algorithm>
#include <array>
#include <iostream>
#include <set>
namespace day8a
{
int main()
{
// BEGIN BOILERPLATE
std::string input0;
//std::ifstream ifile("day8_sample.txt");
std::ifstream ifile("day8.txt");
if (ifile.is_open())
{
while (!ifile.eof())
{
std::string line;
std::getline(ifile, line);
input0 += line + '\n';
}
assert(input0.back() == '\n');
input0.pop_back(); // no trailing newline character please
//__debugbreak();
}
else
{
assert(0);
}
std::vector<std::string> values;
std::string temp;
for (char c : input0)
{
if (c == '\n')
{
values.push_back(temp);
temp = "";
continue;
}
temp += c;
}
values.push_back(temp);
struct coordinate
{
int x;
int y;
int z;
coordinate(int xx, int yy, int zz) // because we are using C++14 with emplace_back we don't have improved aggregate initialization ;(
{
x = xx;
y = yy;
z = zz;
}
double distance(const coordinate& c)
{
double dx = x - c.x;
double dy = y - c.y;
double dz = z - c.z;
return std::sqrt(dx * dx + dy * dy + dz * dz);
}
bool operator<(const coordinate& c) const // forgot to make it const...
{
bool test;
if (x < c.x)
test = true;
else if (c.x < x) // it's supposed to be other is LESS than!!!
test = false;
else if (y < c.y)
test = true;
else if (c.y < y)
test = false;
else if (z < c.z)
test = true;
else if (c.z < z)
test = false;
else
test = false;
bool test2;
if (x != c.x)
test2 = x < c.x;
else if (y != c.y)
test2 = y < c.y;
else
test2 = z < c.z;
if (test != test2)
__debugbreak();
return test;
}
};
std::vector<coordinate> coordinates;
for (auto& s : values)
{
int x;
int y;
int z;
char c;
std::istringstream iss{ s };
iss >> x;
iss >> c;
iss >> y;
iss >> c;
iss >> z;
coordinates.emplace_back( x, y, z );
}
struct distance_pair
{
double distance;
int i;
int j;
};
std::vector<distance_pair> distances2;
for (int i = 0; i < coordinates.size() - 1; i++)
{
for (int j = i + 1; j < coordinates.size(); j++)
{
distance_pair d;
d.distance = coordinates[i].distance(coordinates[j]);
d.i = i;
d.j = j;
distances2.push_back(d);
}
}
std::sort(distances2.begin(), distances2.end(), [](auto& a, auto& b) -> bool { return a.distance < b.distance; });
std::vector<std::set<coordinate>> circuits;
for (int i = 0; i < 1000; i++)
{
std::set<coordinate> s;
int i0 = distances2[i].i;
int i1 = distances2[i].j;
auto c0 = coordinates[i0];
auto c1 = coordinates[i1];
s.insert(c0);
s.insert(c1);
circuits.push_back(s);
}
for (int i = 0; i < circuits.size() - 1; i++)
{
for (int j = i + 1; j < circuits.size(); j++)
{
auto& c0 = circuits[i];
auto& c1 = circuits[j];
auto c2 = c0;
c2.insert(c1.begin(), c1.end());
if (c2.size() < c0.size() + c1.size())
{
circuits.erase(circuits.begin() + j);
circuits.erase(circuits.begin() + i);
circuits.push_back(c2);
i--;
break;
}
}
}
std::sort(circuits.begin(), circuits.end(), [](auto& a, auto& b) -> bool { return a.size() < b.size(); });
int result =
(circuits.end() - 1)->size() *
(circuits.end() - 2)->size() *
(circuits.end() - 3)->size();
// 769 * 7 * 4 = 21532 TOO LOW
__debugbreak();
return 0;
}
}