diff options
Diffstat (limited to '2024/13/day13b.cpp')
| -rw-r--r-- | 2024/13/day13b.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/2024/13/day13b.cpp b/2024/13/day13b.cpp new file mode 100644 index 0000000..b531b7a --- /dev/null +++ b/2024/13/day13b.cpp | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <ranges> | ||
| 5 | #include <set> | ||
| 6 | #include <sstream> | ||
| 7 | #include <string> | ||
| 8 | #include <string_view> | ||
| 9 | #include <vector> | ||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | #define N 10000000000000 | ||
| 13 | #define INF 999999 | ||
| 14 | |||
| 15 | class Machine { | ||
| 16 | public: | ||
| 17 | pair<int64_t, int64_t> a, b, p; | ||
| 18 | |||
| 19 | Machine(int64_t a1, int64_t a2, | ||
| 20 | int64_t b1, int64_t b2, int64_t p1, int64_t p2) : | ||
| 21 | a(a1, a2), b(b1, b2), p(p1, p2) {} | ||
| 22 | }; | ||
| 23 | |||
| 24 | int64_t det(int64_t a, int64_t b, int64_t c, int64_t d) { | ||
| 25 | return a*d - b*c; | ||
| 26 | } | ||
| 27 | |||
| 28 | pair<int64_t, int64_t> solve_system(const Machine m) { | ||
| 29 | auto d = det(m.a.first, m.b.first, m.a.second, m.b.second); | ||
| 30 | auto dx = det(m.p.first, m.b.first, m.p.second, m.b.second); | ||
| 31 | auto dy = det(m.a.first, m.p.first, m.a.second, m.p.second); | ||
| 32 | return make_pair(dx/d, dy/d); | ||
| 33 | } | ||
| 34 | |||
| 35 | int main() { | ||
| 36 | string line; | ||
| 37 | int64_t a1, a2, b1, b2, p1, p2; | ||
| 38 | vector <Machine> machines; | ||
| 39 | while (cin >> a1 >> a2) { | ||
| 40 | cin >> b1 >> b2; | ||
| 41 | cin >> p1 >> p2; | ||
| 42 | getline(cin, line); | ||
| 43 | machines.push_back(Machine(a1, a2, b1, b2, p1+N, p2+N)); | ||
| 44 | } | ||
| 45 | |||
| 46 | int64_t tot = 0; | ||
| 47 | for (auto m : machines) { | ||
| 48 | auto [x, y] = solve_system(m); | ||
| 49 | if (m.a.first * x + m.b.first * y == m.p.first && | ||
| 50 | m.a.second * x + m.b.second * y == m.p.second) | ||
| 51 | tot += 3*x + y; | ||
| 52 | } | ||
| 53 | |||
| 54 | cout << tot << endl; | ||
| 55 | |||
| 56 | return 0; | ||
| 57 | } | ||
