advent2024

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

commit 1ef73fadbacf267c38ca06b63edab52da8bb4366
parent 485d0c28244aa1471467502a579ccf939b972dbe
Author: bsandro <email@bsandro.tech>
Date:   Wed, 11 Dec 2024 23:14:05 +0200

Day 11 p1

Diffstat:
Aday11/Makefile | 28++++++++++++++++++++++++++++
Aday11/input.txt | 1+
Aday11/main.cpp | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday11/test1.txt | 1+
Aday11/test2.txt | 1+
5 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/day11/Makefile b/day11/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++ + +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/day11/input.txt b/day11/input.txt @@ -0,0 +1 @@ +1 24596 0 740994 60 803 8918 9405859 diff --git a/day11/main.cpp b/day11/main.cpp @@ -0,0 +1,91 @@ +#include <iostream> +#include <filesystem> +#include <fstream> +#include <vector> +#include <cstring> +#include <cstdio> +#include <tuple> +#include <algorithm> +#include <thread> +#include <future> +#include "utils.hpp" + +typedef std::vector<uint64_t> Data; + +template<typename T> +T 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)); + } + T buf; + for (std::string str; std::getline(ifs, str, ' ');) { + if (!str.empty()) buf.push_back(std::stoi(str)); + } + return buf; +} + +void blink(Data &stones) { + for (auto it=stones.begin(); it!=stones.end(); ++it) { + std::string s = std::to_string(*it); + if (*it==0) { + *it = 1; + } else if (s.size()%2==0) { + uint64_t n1 = std::stoi(s.substr(0, s.size()/2)); + uint64_t n2 = std::stoi(s.substr(s.size()/2, s.size())); + *it = n2; + it = stones.insert(it, n1); + it++; + } else { + *it *= 2024; + } + } +} + +void blink2(uint64_t start, int num, std::promise<uint64_t> &&result) { + Data stones; + stones.push_back(start); + for (int i=0; i<num; ++i) { + blink(stones); + } + result.set_value(stones.size()); +} + +void printStones(const Data &stones) { + for (auto n:stones) { + std::cout << "[" << n << "]"; + } + std::cout << std::endl; +} + +int64_t part1(Data input [[ maybe_unused ]]) { + uint64_t sum = 0; + std::vector<std::pair<std::jthread, std::future<uint64_t>>> workers; + for (uint64_t num:input) { + std::pair<std::jthread, std::future<uint64_t>> p; + std::promise<uint64_t> pr; + p.second = pr.get_future(); + p.first = std::jthread(blink2, num, 25, std::move(pr)); + workers.push_back(std::move(p)); + } + for (auto &p:workers) { + p.first.join(); + sum += p.second.get(); + } + return sum; +} + +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 11 " << fname << std::endl; + Data input = read_file<Data>(fname); + std::cout << "part1: " << part1(input) << std::endl; + std::cout << "part2: " << part2(input) << std::endl; + + return 0; +} diff --git a/day11/test1.txt b/day11/test1.txt @@ -0,0 +1 @@ +0 1 10 99 999 diff --git a/day11/test2.txt b/day11/test2.txt @@ -0,0 +1 @@ +125 17