commit 9c1579e5f46c278778821ab129deec7fe342e94f
parent 0ec0bb8327ec020333db58355dc500d3dd8c4b26
Author: bsandro <email@bsandro.tech>
Date: Fri, 27 Dec 2024 00:40:00 +0200
Day 13 p2
Diffstat:
2 files changed, 34 insertions(+), 15 deletions(-)
diff --git a/day13/Makefile b/day13/Makefile
@@ -3,9 +3,9 @@ UNAME_S:=$(shell uname -s)
SRC:=$(wildcard *.cpp)
DEPS:=$(wildcard *.hpp)
OBJ:=$(SRC:.cpp=.o)
-CXXSTD:="c++23"
+CXXSTD:=c++23
ifeq (${UNAME_S},OpenBSD)
-CXXSTD="c++2b"
+CXXSTD=c++2b
endif
$(info advent of code on ${UNAME_S})
CXXFLAGS=-O2 -std=${CXXSTD} -Werror -Wall -Wextra -I. -I../include
diff --git a/day13/main.cpp b/day13/main.cpp
@@ -10,11 +10,17 @@
#include "utils.hpp"
struct Vec2 {
- int x;
- int y;
+ int64_t x;
+ int64_t y;
void fromStr(const std::string &s) {
[[ maybe_unused ]] char btn;
- std::sscanf(s.c_str(), "Button %c: X+%d, Y+%d", &btn, &x, &y);
+ std::sscanf(s.c_str(), "Button %c: X+%lld, Y+%lld", &btn, &x, &y);
+ }
+ bool operator==(const Vec2 &other) const {
+ return this->x==other.x && this->y==other.y;
+ }
+ bool operator!=(const Vec2 &other) const {
+ return this->x!=other.x || this->y!=other.y;
}
};
@@ -23,10 +29,10 @@ struct Machine {
Vec2 btnB;
Vec2 prize;
void setPrize(const std::string &s) {
- std::sscanf(s.c_str(), "Prize: X=%d, Y=%d", &prize.x, &prize.y);
+ std::sscanf(s.c_str(), "Prize: X=%lld, Y=%lld", &prize.x, &prize.y);
}
void print() const {
- std::printf("A(%d,%d), B(%d,%d), prize at (%d,%d)\n", btnA.x, btnA.y, btnB.x, btnB.y, prize.x, prize.y);
+ std::printf("A(%lld,%lld), B(%lld,%lld), prize at (%lld,%lld)\n", btnA.x, btnA.y, btnB.x, btnB.y, prize.x, prize.y);
}
};
@@ -65,9 +71,9 @@ T read_file(const std::string &path) {
return buf;
}
-bool simulate(const Machine &m, int pA, int pB, std::vector<int> &out) {
- int curX = m.btnA.x*pA + m.btnB.x*pB;
- int curY = m.btnA.y*pA + m.btnB.y*pB;
+bool simulate(const Machine &m, int64_t pA, int64_t pB, std::vector<int64_t> &out) {
+ int64_t curX = m.btnA.x*pA + m.btnB.x*pB;
+ int64_t curY = m.btnA.y*pA + m.btnB.y*pB;
if (curX>m.prize.x||curY>m.prize.y) return false;
if (curX==m.prize.x&&curY==m.prize.y) {
out.push_back(pA*3+pB);
@@ -75,10 +81,10 @@ bool simulate(const Machine &m, int pA, int pB, std::vector<int> &out) {
return true;
}
-int64_t part1(Data &input [[ maybe_unused ]]) {
+int64_t part1(const Data &input [[ maybe_unused ]]) {
int64_t res = 0;
for (auto &m:input) {
- std::vector<int> runs;
+ std::vector<int64_t> runs;
for (int a=0;a<100;a++) {
for (int b=0;b<100;b++) {
if (!simulate(m, a, b, runs)) break;
@@ -90,8 +96,22 @@ int64_t part1(Data &input [[ maybe_unused ]]) {
return res;
}
-int64_t part2(Data &input [[ maybe_unused ]]) {
- return 0;
+
+int64_t part2(Data input [[ maybe_unused ]]) {
+ int64_t res = 0;
+ for (auto &m:input) {
+ m.prize.x += 10000000000000;
+ m.prize.y += 10000000000000;
+ int64_t pA = (m.prize.x*m.btnB.y-m.prize.y*m.btnB.x)/(m.btnA.x*m.btnB.y-m.btnA.y*m.btnB.x);
+ int64_t pB = (m.prize.y*m.btnA.x-m.prize.x*m.btnA.y)/(m.btnA.x*m.btnB.y-m.btnA.y*m.btnB.x);
+ Vec2 c;
+ c.x = m.btnA.x*pA + m.btnB.x*pB;
+ c.y = m.btnA.y*pA + m.btnB.y*pB;
+ if (c==m.prize) {
+ res += pA*3+pB;
+ }
+ }
+ return res;
}
int main(int argc, char **argv) {
@@ -101,6 +121,5 @@ int main(int argc, char **argv) {
Data input = read_file<Data>(fname);
std::cout << "part1: " << part1(input) << std::endl;
std::cout << "part2: " << part2(input) << std::endl;
-
return 0;
}