|
| 1 | +#include <algorithm> |
| 2 | +#include <fstream> |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | +#include <limits> |
| 6 | +#include <unordered_map> |
| 7 | +#include <unordered_set> |
| 8 | +#include <vector> |
| 9 | +#include <cmath> |
| 10 | +#include <regex> |
| 11 | + |
| 12 | +// Note: increment velocity first, then position (mentioned in part 1 i.e. part a) |
| 13 | +// time: position(time) |
| 14 | +// 0: p(0) = p0 |
| 15 | +// 1: p(1) = p0 + v + a |
| 16 | +// 2: p(2) = p0 + v + a + v + 2a |
| 17 | +// n: p(n) = p0 + v + a + ... + v + na = p0 + n * v + n(n+1)/2 * a |
| 18 | +// p(t) = (a * t^2 + (a + 2v) * t + 2 * p0) / 2 |
| 19 | + |
| 20 | +struct Coord3D { |
| 21 | + long long x; |
| 22 | + long long y; |
| 23 | + long long z; |
| 24 | + |
| 25 | + long long dist () const { |
| 26 | + return std::abs(x) + std::abs(y) + std::abs(z); |
| 27 | + } |
| 28 | + |
| 29 | + long long sum () const { |
| 30 | + return x + y + z; |
| 31 | + } |
| 32 | + |
| 33 | + Coord3D operator + (const Coord3D& c) const { |
| 34 | + Coord3D ans; |
| 35 | + ans.x = x + c.x; |
| 36 | + ans.y = y + c.y; |
| 37 | + ans.z = z + c.z; |
| 38 | + return ans; |
| 39 | + } |
| 40 | + |
| 41 | + Coord3D operator - (const Coord3D& c) const { |
| 42 | + Coord3D ans; |
| 43 | + ans.x = x - c.x; |
| 44 | + ans.y = y - c.y; |
| 45 | + ans.z = z - c.z; |
| 46 | + return ans; |
| 47 | + } |
| 48 | + |
| 49 | + Coord3D operator += (const Coord3D& c) { |
| 50 | + x += c.x; |
| 51 | + y += c.y; |
| 52 | + z += c.z; |
| 53 | + return *this; |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +long long dot_product(const Coord3D& v0, const Coord3D& v1) { |
| 58 | + return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z; |
| 59 | +} |
| 60 | + |
| 61 | +struct Particle { |
| 62 | + Coord3D p; |
| 63 | + Coord3D v; |
| 64 | + Coord3D a; |
| 65 | +}; |
| 66 | + |
| 67 | +struct Collision { |
| 68 | + long long id0; |
| 69 | + long long id1; |
| 70 | + long long time; |
| 71 | +}; |
| 72 | + |
| 73 | +int find_position_at_time (const int p, const int v, const int a, const int time) { |
| 74 | + // std::cout << "Values: " << s << ' ' << v << ' ' << a << ' ' << time << '\n'; |
| 75 | + // std::cout << "Position: " << s + (time + 1 ) * v + (time * time + 1 * a) / 2 << '\n'; |
| 76 | + return (a * time * time + (a + 2 * v) * time + 2 * p) / 2; |
| 77 | +} |
| 78 | + |
| 79 | +std::vector<long long> find_time_of_potential_collision(const int p0, const int p1, const int v0, const int v1, const int a0, const int a1) { |
| 80 | + const auto a = float(a1 - a0); |
| 81 | + float t0 = 0; |
| 82 | + float t1 = 0; |
| 83 | + if (a == 0) { |
| 84 | + t0 = -float(p1 -p0) / float(v1 - v0); |
| 85 | + t1 = -1; |
| 86 | + } else { |
| 87 | + const auto b = float((a1 - a0) + 2 * (v1 - v0)); |
| 88 | + const auto c = float(2.f * (p1 - p0)); |
| 89 | + // std::cout << "Quad equation values: " << a << ' ' << b << ' ' << c << '\n'; |
| 90 | + const auto discriminent_2 = (b * b) - 4 * a *c; |
| 91 | + if (discriminent_2 < 0) { |
| 92 | + // std::cout << "No collision possible; negative b2-4ac" << '\n'; |
| 93 | + return {-1, -1}; |
| 94 | + } |
| 95 | + // TODO: improve this check |
| 96 | + const auto discriminent = std::sqrt(discriminent_2); |
| 97 | + if (discriminent != int(discriminent)) { |
| 98 | + // std::cout << "No collision possible, not a perfect square: " << discriminent << '\n'; |
| 99 | + return {-1, -1}; |
| 100 | + } |
| 101 | + t0 = (-b + discriminent) / (2 * a); |
| 102 | + t1 = (-b - discriminent) / (2 * a); |
| 103 | + } |
| 104 | + if (t0 < 0) t0 = -1; |
| 105 | + if (t1 < 0) t1 = -1; |
| 106 | + // std::cout << t0 << ' ' << t1 << '\n'; |
| 107 | + if (t0 != int(t0)) t0 = -1; |
| 108 | + if (t1 != int(t1)) t1 = -1; |
| 109 | + // std::cout << "Possible times: " << t0 << ' ' << t1 << '\n'; |
| 110 | + return {static_cast<long long>(t0), static_cast<long long>(t1)}; |
| 111 | +} |
| 112 | + |
| 113 | +int main(int argc, char* argv[]) { |
| 114 | + const std::string input = (argc > 1) ? argv[1] : "../input/day_20_input" ; |
| 115 | + std::ifstream file(input); |
| 116 | + std::string line; |
| 117 | + const std::regex pattern(R"(p=<(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)>, v=<(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)>, a=<(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)>)"); |
| 118 | + std::vector<Particle> particles; |
| 119 | + long long min_acc = std::numeric_limits<long long>::max(); |
| 120 | + while(std::getline(file, line)) { |
| 121 | + std::smatch match; |
| 122 | + std::regex_search(line, match, pattern); |
| 123 | + Particle particle; |
| 124 | + particle.p.x = std::stoi(match[1]); |
| 125 | + particle.p.y = std::stoi(match[2]); |
| 126 | + particle.p.z = std::stoi(match[3]); |
| 127 | + particle.v.x = std::stoi(match[4]); |
| 128 | + particle.v.y = std::stoi(match[5]); |
| 129 | + particle.v.z = std::stoi(match[6]); |
| 130 | + particle.a.x = std::stoi(match[7]); |
| 131 | + particle.a.y = std::stoi(match[8]); |
| 132 | + particle.a.z = std::stoi(match[9]); |
| 133 | + particles.push_back(particle); |
| 134 | + } |
| 135 | + |
| 136 | + std::vector<Collision> collisions; |
| 137 | + for (int i = 0; i < particles.size(); i++) { |
| 138 | + for (int j = i + 1; j < particles.size(); j++) { |
| 139 | + // std::cout << "\nPair: (" << i << "," << j << "): " << '\n'; |
| 140 | + const auto times = find_time_of_potential_collision(particles[i].p.x, particles[j].p.x, particles[i].v.x, particles[j].v.x, particles[i].a.x, particles[j].a.x); |
| 141 | + for (const auto time : times) { |
| 142 | + // std::cout << "Time: " << time << '\n'; |
| 143 | + if (time != -1 && find_position_at_time(particles[i].p.y, particles[i].v.y, particles[i].a.y, time) |
| 144 | + == find_position_at_time(particles[j].p.y, particles[j].v.y, particles[j].a.y, time) && |
| 145 | + find_position_at_time(particles[i].p.z, particles[i].v.z, particles[i].a.z, time) |
| 146 | + == find_position_at_time(particles[j].p.z, particles[j].v.z, particles[j].a.z, time) ) { |
| 147 | + // std::cout << "Collision!!!!!!!!!! Time: " << time << '\n'; |
| 148 | + Collision c; |
| 149 | + c.id0 = i; |
| 150 | + c.id1 = j; |
| 151 | + c.time = time; |
| 152 | + collisions.push_back(c); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + std::sort(std::begin(collisions), std::end(collisions), [](const auto& c1, const auto& c2) {return c1.time < c2.time; }); |
| 159 | + // for (const auto & c : collisions) { |
| 160 | + // std::cout << c.time << '\n'; |
| 161 | + // } |
| 162 | + std::unordered_map<long long, long long> id_to_destruction_time; |
| 163 | + for (const auto& c : collisions) { |
| 164 | + if (id_to_destruction_time.find(c.id0) == id_to_destruction_time.end() && id_to_destruction_time.find(c.id1) == id_to_destruction_time.end()) { |
| 165 | + id_to_destruction_time[c.id0] = c.time; |
| 166 | + id_to_destruction_time[c.id1] = c.time; |
| 167 | + // std::cout << "Adding " << c.id0 << '\n'; |
| 168 | + // std::cout << "Adding " << c.id1 << '\n'; |
| 169 | + } else if (id_to_destruction_time.find(c.id0) != id_to_destruction_time.end() && id_to_destruction_time[c.id0] == c.time) { |
| 170 | + id_to_destruction_time[c.id1] = c.time; |
| 171 | + // std::cout << "Adding " << c.id1 << '\n'; |
| 172 | + } else if (id_to_destruction_time.find(c.id1) != id_to_destruction_time.end() && id_to_destruction_time[c.id1] == c.time) { |
| 173 | + id_to_destruction_time[c.id0] = c.time; |
| 174 | + // std::cout << "Adding " << c.id0 << '\n'; |
| 175 | + } |
| 176 | + } |
| 177 | + std::cout << particles.size() - id_to_destruction_time.size() << '\n'; |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | + |
0 commit comments