From 5adc85d5cd69f5e1ac017aed6fae4a13c63ef966 Mon Sep 17 00:00:00 2001 From: skasch Date: Tue, 26 Dec 2023 19:58:48 +0100 Subject: [PATCH 1/3] Fix rounding error --- day-24/part-2/skasch.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/day-24/part-2/skasch.cpp b/day-24/part-2/skasch.cpp index f2d22c8..8042d86 100644 --- a/day-24/part-2/skasch.cpp +++ b/day-24/part-2/skasch.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,14 @@ __int128_t gcd(__int128_t a, __int128_t b) { return b; } +std::ostream& operator<<(std::ostream& o, const __int128& x) { + if (x == std::numeric_limits<__int128>::min()) + return o << "-170141183460469231731687303715884105728"; + if (x < 0) return o << "-" << -x; + if (x < 10) return o << (char)(x + '0'); + return o << x / 10 << (char)(x % 10 + '0'); +} + struct Vec { __int128_t x; __int128_t y; @@ -45,6 +54,10 @@ struct Vec { Vec operator^(const Vec& o) const { return {y * o.z - z * o.y, z * o.x - x * o.z, x * o.y - y * o.x}; } + + friend std::ostream& operator<<(std::ostream& out, const Vec& v) { + return out << "(" << v.x << "," << v.y << "," << v.z << ")"; + } }; struct Hail { @@ -57,8 +70,8 @@ std::optional> solve( __int128_t b1, __int128_t b2) { __int128_t delta = x11 * x22 - x21 * x12; if (delta == 0) return std::nullopt; - return std::make_pair(x22 * b1 / delta - x12 * b2 / delta, - -x21 * b1 / delta + x11 * b2 / delta); + return std::make_pair((x22 * b1 - x12 * b2) / delta, + -(x21 * b1 + x11 * b2) / delta); } static std::array kHails; From 95db80789b3689b80a8ed19cefb6c1491284fb0b Mon Sep 17 00:00:00 2001 From: skasch Date: Tue, 26 Dec 2023 20:00:19 +0100 Subject: [PATCH 2/3] Remove ostream helpers --- day-24/part-2/skasch.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/day-24/part-2/skasch.cpp b/day-24/part-2/skasch.cpp index 8042d86..e0ad47a 100644 --- a/day-24/part-2/skasch.cpp +++ b/day-24/part-2/skasch.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -30,14 +29,6 @@ __int128_t gcd(__int128_t a, __int128_t b) { return b; } -std::ostream& operator<<(std::ostream& o, const __int128& x) { - if (x == std::numeric_limits<__int128>::min()) - return o << "-170141183460469231731687303715884105728"; - if (x < 0) return o << "-" << -x; - if (x < 10) return o << (char)(x + '0'); - return o << x / 10 << (char)(x % 10 + '0'); -} - struct Vec { __int128_t x; __int128_t y; @@ -54,10 +45,6 @@ struct Vec { Vec operator^(const Vec& o) const { return {y * o.z - z * o.y, z * o.x - x * o.z, x * o.y - y * o.x}; } - - friend std::ostream& operator<<(std::ostream& out, const Vec& v) { - return out << "(" << v.x << "," << v.y << "," << v.z << ")"; - } }; struct Hail { From 2866b2cc5d5469004ddcc68025a312de178864a1 Mon Sep 17 00:00:00 2001 From: skasch Date: Tue, 26 Dec 2023 20:01:59 +0100 Subject: [PATCH 3/3] Simplify solver --- day-24/part-2/skasch.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/day-24/part-2/skasch.cpp b/day-24/part-2/skasch.cpp index e0ad47a..b9c0070 100644 --- a/day-24/part-2/skasch.cpp +++ b/day-24/part-2/skasch.cpp @@ -52,13 +52,11 @@ struct Hail { Vec v; }; -std::optional> solve( - __int128_t x11, __int128_t x12, __int128_t x21, __int128_t x22, - __int128_t b1, __int128_t b2) { +std::optional<__int128_t> solve(__int128_t x11, __int128_t x12, __int128_t x21, + __int128_t x22, __int128_t b1, __int128_t b2) { __int128_t delta = x11 * x22 - x21 * x12; if (delta == 0) return std::nullopt; - return std::make_pair((x22 * b1 - x12 * b2) / delta, - -(x21 * b1 + x11 * b2) / delta); + return (x22 * b1 - x12 * b2) / delta; } static std::array kHails; @@ -156,7 +154,7 @@ std::string Run(const std::string& input) { auto solution = solve(v1.x - vx, -v2.x + vx, v1.y - vy, -v2.y + vy, p2.x - p1.x, p2.y - p1.y); if (!solution.has_value()) continue; - const auto& [t1, t2] = *solution; + __int128 t1 = *solution; for (int vz = -kMaxVelocity; vz < kMaxVelocity; ++vz) { if (invalid_vz.contains(vz)) continue; Vec v(vx, vy, vz);