advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit 0ec0bb8327ec020333db58355dc500d3dd8c4b26
parent 328ed172dcc9451f8ab3bb440f2374666af52432
Author: bsandro <email@bsandro.tech>
Date:   Thu, 26 Dec 2024 23:24:04 +0200

Day 13 p1

Diffstat:
Mday13/Makefile | 12+++++++++---
Mday13/main.cpp | 21++++++++++-----------
2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/day13/Makefile b/day13/Makefile @@ -1,8 +1,14 @@ -NAME=$(shell basename ${PWD}) -SRC=$(wildcard *.cpp) +NAME:=$(shell basename ${PWD}) +UNAME_S:=$(shell uname -s) +SRC:=$(wildcard *.cpp) DEPS:=$(wildcard *.hpp) OBJ:=$(SRC:.cpp=.o) -CXXFLAGS=-O2 -std=c++23 -Werror -Wall -Wextra -I. -I../include +CXXSTD:="c++23" +ifeq (${UNAME_S},OpenBSD) +CXXSTD="c++2b" +endif +$(info advent of code on ${UNAME_S}) +CXXFLAGS=-O2 -std=${CXXSTD} -Werror -Wall -Wextra -I. -I../include LDFLAGS=-lstdc++ all: $(NAME) diff --git a/day13/main.cpp b/day13/main.cpp @@ -65,28 +65,27 @@ T read_file(const std::string &path) { return buf; } -void simulate(const Machine &m, int pA, int pB, std::vector<int> &out) { - if (pA>100||pB>100) return; +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; - //std::printf("pA=%d, pB=%d, curX=%d, curY=%d\n", pA, pB, curX, curY); - if (curX>m.prize.x||curY>m.prize.y) return; - if (m.btnA.x*pA==m.prize.x&&m.btnB.y*pB==m.prize.y) { + 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); - } else { - simulate(m, ++pA, pB, out); - simulate(m, pA, ++pB, out); } + return true; } int64_t part1(Data &input [[ maybe_unused ]]) { int64_t res = 0; for (auto &m:input) { - m.print(); std::vector<int> runs; - simulate(m, 0, 0, runs); + for (int a=0;a<100;a++) { + for (int b=0;b<100;b++) { + if (!simulate(m, a, b, runs)) break; + } + } std::sort(runs.begin(), runs.end()); - std::printf("%ld successful results\n", runs.size()); + if (!runs.empty()) res += runs.back(); } return res;