commit 77552dcbbb3281246358b24092c4e51156982ac3
parent 3418444a8da97e5a05f87f9737bed94edd43ec60
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Fri, 13 Dec 2024 06:51:26 +0100
Day 13 2024
Diffstat:
4 files changed, 146 insertions(+), 0 deletions(-)
diff --git a/2024/13/Makefile b/2024/13/Makefile
@@ -0,0 +1,24 @@
+CC=g++ -std=c++20 -g -Wall
+
+a:
+ ${CC} -o a.out day13a.cpp
+
+b:
+ ${CC} -o b.out day13b.cpp
+
+clean:
+ rm -f a b
+
+atest: a
+ ./a.out
+
+btest: b
+ ./b.out
+
+arun: a
+ ./a.out < input
+
+brun: b
+ ./b.out < input
+
+.PHONY: a b clean atest btest arun brun
diff --git a/2024/13/clean.sh b/2024/13/clean.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+sed 's/Button .: X+//; s/, Y./ /; s/Prize: X=//'
diff --git a/2024/13/day13a.cpp b/2024/13/day13a.cpp
@@ -0,0 +1,62 @@
+/*
+This is the dumb solution I wrote before actually thinking how to solve
+the problem. Check out day13b.cpp for a better solution (just remove the
++N in the input-reading part).
+
+Also, both this file and day13b.cpp take a different input format than
+what was given. I used clean.sh to convert from 'input-original' to 'input'.
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <map>
+#include <ranges>
+#include <set>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
+using namespace std;
+
+#define INF 999999
+
+class Machine {
+public:
+ pair<int, int> a, b, prize;
+
+ Machine(int a1, int a2, int b1, int b2, int p1, int p2) :
+ a(a1, a2), b(b1, b2), prize(p1, p2) {}
+};
+
+int main() {
+ string line;
+ int a1, a2, b1, b2, p1, p2;
+ vector <Machine> machines;
+ while (cin >> a1 >> a2) {
+ cin >> b1 >> b2;
+ cin >> p1 >> p2;
+ getline(cin, line);
+ machines.push_back(Machine(a1, a2, b1, b2, p1, p2));
+ }
+
+ int tot = 0;
+ for (auto m : machines) {
+ int minp = INF;
+ for (int i = 0; i <= 100; i++) {
+ int x = m.prize.first-m.a.first*i;
+ int y = m.prize.second-m.a.second*i;
+ if (x % m.b.first != 0 || y % m.b.second != 0)
+ continue;
+ int a = x / m.b.first;
+ int b = y / m.b.second;
+ if (a != b)
+ continue;
+ minp = min(minp, 3*i+a);
+ }
+ tot += minp == INF ? 0 : minp;
+ }
+
+ cout << tot << endl;
+
+ return 0;
+}
diff --git a/2024/13/day13b.cpp b/2024/13/day13b.cpp
@@ -0,0 +1,57 @@
+#include <algorithm>
+#include <iostream>
+#include <map>
+#include <ranges>
+#include <set>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
+using namespace std;
+
+#define N 10000000000000
+#define INF 999999
+
+class Machine {
+public:
+ pair<int64_t, int64_t> 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<int64_t, int64_t> 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 <Machine> 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;
+}