From 77552dcbbb3281246358b24092c4e51156982ac3 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 13 Dec 2024 06:51:26 +0100 Subject: Day 13 2024 --- 2024/13/day13b.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2024/13/day13b.cpp (limited to '2024/13/day13b.cpp') 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define N 10000000000000 +#define INF 999999 + +class Machine { +public: + pair a, b, p; + + Machine(int64_t a1, int64_t a2, + int64_t b1, int64_t b2, int64_t p1, int64_t p2) : + a(a1, a2), b(b1, b2), p(p1, p2) {} +}; + +int64_t det(int64_t a, int64_t b, int64_t c, int64_t d) { + return a*d - b*c; +} + +pair solve_system(const Machine m) { + auto d = det(m.a.first, m.b.first, m.a.second, m.b.second); + auto dx = det(m.p.first, m.b.first, m.p.second, m.b.second); + auto dy = det(m.a.first, m.p.first, m.a.second, m.p.second); + return make_pair(dx/d, dy/d); +} + +int main() { + string line; + int64_t a1, a2, b1, b2, p1, p2; + vector machines; + while (cin >> a1 >> a2) { + cin >> b1 >> b2; + cin >> p1 >> p2; + getline(cin, line); + machines.push_back(Machine(a1, a2, b1, b2, p1+N, p2+N)); + } + + int64_t tot = 0; + for (auto m : machines) { + auto [x, y] = solve_system(m); + if (m.a.first * x + m.b.first * y == m.p.first && + m.a.second * x + m.b.second * y == m.p.second) + tot += 3*x + y; + } + + cout << tot << endl; + + return 0; +} -- cgit v1.3