aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2024/13/Makefile24
-rwxr-xr-x2024/13/clean.sh3
-rw-r--r--2024/13/day13a.cpp62
-rw-r--r--2024/13/day13b.cpp57
4 files changed, 146 insertions, 0 deletions
diff --git a/2024/13/Makefile b/2024/13/Makefile
new file mode 100644
index 0000000..d597553
--- /dev/null
+++ b/2024/13/Makefile
@@ -0,0 +1,24 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day13a.cpp
5
6b:
7 ${CC} -o b.out day13b.cpp
8
9clean:
10 rm -f a b
11
12atest: a
13 ./a.out
14
15btest: b
16 ./b.out
17
18arun: a
19 ./a.out < input
20
21brun: b
22 ./b.out < input
23
24.PHONY: a b clean atest btest arun brun
diff --git a/2024/13/clean.sh b/2024/13/clean.sh
new file mode 100755
index 0000000..d67cf84
--- /dev/null
+++ b/2024/13/clean.sh
@@ -0,0 +1,3 @@
1#!/bin/sh
2
3sed 's/Button .: X+//; s/, Y./ /; s/Prize: X=//'
diff --git a/2024/13/day13a.cpp b/2024/13/day13a.cpp
new file mode 100644
index 0000000..9dccff9
--- /dev/null
+++ b/2024/13/day13a.cpp
@@ -0,0 +1,62 @@
1/*
2This is the dumb solution I wrote before actually thinking how to solve
3the problem. Check out day13b.cpp for a better solution (just remove the
4+N in the input-reading part).
5
6Also, both this file and day13b.cpp take a different input format than
7what was given. I used clean.sh to convert from 'input-original' to 'input'.
8*/
9
10#include <algorithm>
11#include <iostream>
12#include <map>
13#include <ranges>
14#include <set>
15#include <sstream>
16#include <string>
17#include <string_view>
18#include <vector>
19using namespace std;
20
21#define INF 999999
22
23class Machine {
24public:
25 pair<int, int> a, b, prize;
26
27 Machine(int a1, int a2, int b1, int b2, int p1, int p2) :
28 a(a1, a2), b(b1, b2), prize(p1, p2) {}
29};
30
31int main() {
32 string line;
33 int a1, a2, b1, b2, p1, p2;
34 vector <Machine> machines;
35 while (cin >> a1 >> a2) {
36 cin >> b1 >> b2;
37 cin >> p1 >> p2;
38 getline(cin, line);
39 machines.push_back(Machine(a1, a2, b1, b2, p1, p2));
40 }
41
42 int tot = 0;
43 for (auto m : machines) {
44 int minp = INF;
45 for (int i = 0; i <= 100; i++) {
46 int x = m.prize.first-m.a.first*i;
47 int y = m.prize.second-m.a.second*i;
48 if (x % m.b.first != 0 || y % m.b.second != 0)
49 continue;
50 int a = x / m.b.first;
51 int b = y / m.b.second;
52 if (a != b)
53 continue;
54 minp = min(minp, 3*i+a);
55 }
56 tot += minp == INF ? 0 : minp;
57 }
58
59 cout << tot << endl;
60
61 return 0;
62}
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