diff options
author | Leah Neukirchen <leah@vuxu.org> | 2017-12-21 23:50:55 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2017-12-21 23:50:55 +0100 |
commit | dcbe8e18e579996e33e4200e4c58eadb2fc17815 (patch) | |
tree | 07649fc4efc7a0db01c25094216177af4dd2986c /day20.cc | |
parent | f6ecf88a2d9da1f140abedc52a0b9b46108a6563 (diff) | |
download | adventofcode2017-dcbe8e18e579996e33e4200e4c58eadb2fc17815.tar.gz adventofcode2017-dcbe8e18e579996e33e4200e4c58eadb2fc17815.tar.xz adventofcode2017-dcbe8e18e579996e33e4200e4c58eadb2fc17815.zip |
day20
Diffstat (limited to 'day20.cc')
-rw-r--r-- | day20.cc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/day20.cc b/day20.cc new file mode 100644 index 0000000..33c1fb3 --- /dev/null +++ b/day20.cc @@ -0,0 +1,67 @@ +#include <algorithm> +#include <iostream> +#include <numeric> +#include <sstream> +#include <tuple> +#include <vector> + +using namespace std; + +struct particle { + struct v3 { int x, y, z; }; + v3 p, v, a; + bool operator<(const particle &o) const { + return tie(p.x, p.y, p.z) < tie(o.p.x, o.p.y, o.p.z); + } + int dist() const { + return abs(p.x) + abs(p.y) + abs(p.z); + } + void move() { + v.x += a.x; v.y += a.y; v.z += a.z; + p.x += v.x; p.y += v.y; p.z += v.z; + } +}; + +int +main() { + + vector<particle> particles; + + for (string line; getline(cin, line); ) { + replace_if(begin(line), end(line), + [](char c1) { return !(isdigit(c1) || c1 == '-'); }, ' '); + + particle t; + istringstream{line} >> t.p.x >> t.p.y >> t.p.z + >> t.v.x >> t.v.y >> t.v.z + >> t.a.x >> t.a.y >> t.a.z; + particles.push_back(t); + } + + vector<particle> particles1 = particles; + for (auto &p : particles1) + for (int i = 0; i < 1000; i++) + p.move(); + auto x = min_element(begin(particles1), end(particles1), + [](auto& p1, auto& p2) { return p1.dist() < p2.dist(); }); + cout << distance(begin(particles1), x) << endl; + + vector<particle> new_particles; + for (int i = 0; i < 1000; i++) { + new_particles.clear(); + + sort(begin(particles), end(particles)); + + for (auto i = begin(particles); i != end(particles); ) { + auto j = upper_bound(i, end(particles), *i); + if (distance(i, j) == 1) { + i->move(); + new_particles.push_back(*i); + } + i = j; + } + + particles.swap(new_particles); + } + cout << size(particles) << endl; +} |