aboutsummaryrefslogtreecommitdiff
path: root/2024/13/day13b.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-13 06:51:26 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-13 06:51:26 +0100
commit77552dcbbb3281246358b24092c4e51156982ac3 (patch)
treeb7b784580345f10693b6dcf398298130854bec37 /2024/13/day13b.cpp
parent3418444a8da97e5a05f87f9737bed94edd43ec60 (diff)
downloadaoc-77552dcbbb3281246358b24092c4e51156982ac3.tar.gz
aoc-77552dcbbb3281246358b24092c4e51156982ac3.zip
Day 13 2024
Diffstat (limited to '2024/13/day13b.cpp')
-rw-r--r--2024/13/day13b.cpp57
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>
10using namespace std;
11
12#define N 10000000000000
13#define INF 999999
14
15class Machine {
16public:
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
24int64_t det(int64_t a, int64_t b, int64_t c, int64_t d) {
25 return a*d - b*c;
26}
27
28pair<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
35int 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}

Generated with cgit - Back to sebastiano.tronto.net