advent2024

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

commit e819cc8bb54f51d1be2e65984da7bbbef204e79f
parent 0707d69626781fad2f435f8827b3796e1f8b9898
Author: bsandro <email@bsandro.tech>
Date:   Tue, 17 Dec 2024 08:39:31 +0200

Day 17 p1

Diffstat:
Aday17/Makefile | 28++++++++++++++++++++++++++++
Aday17/input.txt | 5+++++
Aday17/main.cpp | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday17/test1.txt | 5+++++
4 files changed, 161 insertions(+), 0 deletions(-)

diff --git a/day17/Makefile b/day17/Makefile @@ -0,0 +1,28 @@ +NAME=$(shell basename ${PWD}) +SRC=$(wildcard *.cpp) +DEPS:=$(wildcard *.hpp) +OBJ:=$(SRC:.cpp=.o) +CXXFLAGS=-O2 -std=c++23 -Werror -Wall -Wextra -I. -I../include +LDFLAGS=-lstdc++ -lm + +all: $(NAME) + +.PHONY: clean run + +clean: + rm -f $(OBJ) $(NAME) + +%.o : %.c $(DEPS) + @$(CC) $(CFLAGS) -c $< -o $@ + +$(NAME): $(OBJ) + @$(CC) $(OBJ) -o $@ $(LDFLAGS) + +run: $(NAME) + @./$(NAME) input.txt + +test1: $(NAME) + @./$(NAME) test1.txt + +test2: $(NAME) + @./$(NAME) test2.txt diff --git a/day17/input.txt b/day17/input.txt @@ -0,0 +1,5 @@ +Register A: 30886132 +Register B: 0 +Register C: 0 + +Program: 2,4,1,1,7,5,0,3,1,4,4,4,5,5,3,0 diff --git a/day17/main.cpp b/day17/main.cpp @@ -0,0 +1,123 @@ +#include <iostream> +#include <filesystem> +#include <fstream> +#include <vector> +#include <cstring> +#include <cstdio> +#include <tuple> +#include <algorithm> +#include <cmath> +#include "utils.hpp" + +typedef std::vector<std::pair<int, int>> Data; + +class Cpu { +public: +// instruction pointer + int ip = 0; +// registers + int a = 0; + int b = 0; + int c = 0; +// combo operand + int combo(int o) { + if (o>=0&&o<=3) return o; + if (o==4) return a; + if (o==5) return b; + if (o==6) return c; + std::printf("invalid combo operand %d\n", o); + return 0; + } +// opcodes 0-7 + int adv(int o) { a /= std::pow(2, combo(o)); return ++ip; } + int bxl(int o) { b ^= o; return ++ip; }; + int bst(int o) { b = combo(o) % 8; return ++ip; }; + int jnz(int o) { if (a==0) ip++; else ip=o; return ip; }; + int bxc(int o [[maybe_unused]]) { b ^= c; return ++ip; }; + int out(int o, std::stringstream &ss) { ss << (combo(o)%8) << ","; return ++ip; }; + int bdv(int o) { b = a/std::pow(2, combo(o)); return ++ip; }; + int cdv(int o) { c = a/std::pow(2, combo(o)); return ++ip; }; +// main thing + int eval(int op, int o, std::stringstream &ss); + std::string run(const Data &input); + void print() { std::printf("ip:%d,a:%d,b:%d,c:%d\n", ip, a, b, c); } +}; + +int Cpu::eval(int op, int o, std::stringstream &ss) { + switch (op) { + case 0: return adv(o); + case 1: return bxl(o); + case 2: return bst(o); + case 3: return jnz(o); + case 4: return bxc(o); + case 5: return out(o, ss); + case 6: return bdv(o); + case 7: return cdv(o); + } + std::printf("invalid opcode\n"); + return -1; +} + +std::string Cpu::run(const Data &input) { + std::stringstream ss; + while (ip>=0&&ip<(int)input.size()) ip=eval(input[ip].first, input[ip].second, ss); + return ss.str(); +} + +std::tuple<Cpu, Data> read_file(const std::string &path) { + std::ifstream ifs(path, std::ios::binary); + if (!ifs.is_open()) { + throw std::runtime_error(path+":"+std::strerror(errno)); + } + Data buf; + Cpu cpu; + for (int l=0;;l++) { + std::string str; + std::getline(ifs, str); + if (!str.empty()) { + if (l==0) std::sscanf(str.c_str(), "Register A: %d", &cpu.a); + if (l==1) std::sscanf(str.c_str(), "Register B: %d", &cpu.b); + if (l==2) std::sscanf(str.c_str(), "Register C: %d", &cpu.c); + if (l==4) { + char tmp[256] = {0}; + std::sscanf(str.c_str(), "Program: %[^\n]", tmp); + std::istringstream iss(tmp); + std::pair<int,int> instr; + int num = 0; + for (std::string sn; std::getline(iss, sn, ',');) { + if (num%2==0) { + instr.first = std::stoi(sn); + } else { + instr.second = std::stoi(sn); + buf.push_back(instr); + } + num++; + } + } + } + if (!ifs) break; + } + return std::make_pair(cpu, buf); +} + +std::string part1(Cpu cpu [[ maybe_unused ]], Data input [[ maybe_unused ]]) { + //cpu.print(); + //for (auto [opcode, operand] : input) std::printf("[%d,%d]", opcode, operand); + + return cpu.run(input); +} + +int64_t part2(Data &input [[ maybe_unused ]]) { + return 0; +} + +int main(int argc, char **argv) { + Performance perf; + const std::string fname = argc>1 ? argv[1] : "test1.txt"; + std::cout << "AoC 2024 day 17 " << fname << std::endl; + auto [cpu, input] = read_file(fname); + std::cout << "part1: " << part1(cpu, input) << std::endl; + std::cout << "part2: " << part2(input) << std::endl; + + return 0; +} diff --git a/day17/test1.txt b/day17/test1.txt @@ -0,0 +1,5 @@ +Register A: 729 +Register B: 0 +Register C: 0 + +Program: 0,1,5,4,3,0