-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.h
69 lines (64 loc) · 1.52 KB
/
day12.h
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
#include <array>
#include <fstream>
#include <numeric>
#include <regex>
#include <vector>
namespace {
using namespace std;
auto parse(ifstream input) {
array<vector<pair<int, int>>, 3> axes;
string line;
while (getline(input, line)) {
smatch match;
regex_match(line, match, regex(R"(<x=(-?\d+), y=(-?\d+), z=(-?\d+)>)"));
axes[0].push_back({ stoi(match[1]), 0 });
axes[1].push_back({ stoi(match[2]), 0 });
axes[2].push_back({ stoi(match[3]), 0 });
}
return axes;
}
void step(vector<pair<int, int>>& moons) {
for (auto& [p1, v1] : moons) {
for (auto&& [p2, v2] : moons) {
if (p2 > p1)
v1++;
if (p2 < p1)
v1--;
}
}
for (auto& [p, v] : moons)
p += v;
}
auto part1(ifstream input) {
auto axes = parse(move(input));
for (int i = 0; i < 1000; i++)
for (auto& axis : axes)
step(axis);
int energy = 0;
for (int i = 0; i < axes[0].size(); i++) {
int potential = 0;
int kinetic = 0;
for (auto&& axis : axes) {
auto&& [p, v] = axis[i];
potential += abs(p);
kinetic += abs(v);
}
energy += potential * kinetic;
}
return energy;
}
auto part2(ifstream input) {
auto axes = parse(move(input));
long long total = 1;
for (auto&& first : axes) {
auto axis = first;
int steps = 0;
do {
step(axis);
steps++;
} while (axis != first);
total = lcm(total, steps);
}
return total;
}
}