aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit b6264be563583c188018d534a223d4108cce6d76
parent d92b565634adb2737af9b0d1f8b243da0c206335
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Wed, 11 Dec 2024 07:03:12 +0100

Day 11 2024

Diffstat:
A2024/11/Makefile | 24++++++++++++++++++++++++
A2024/11/day11a.cpp | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2024/11/day11b.cpp | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2024/11/input | 1+
M2024/learned.txt | 1+
5 files changed, 145 insertions(+), 0 deletions(-)

diff --git a/2024/11/Makefile b/2024/11/Makefile @@ -0,0 +1,24 @@ +CC=g++ -std=c++20 -g -Wall + +a: + ${CC} -o a.out day11a.cpp + +b: + ${CC} -o b.out day11b.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/11/day11a.cpp b/2024/11/day11a.cpp @@ -0,0 +1,57 @@ +#include <algorithm> +#include <cstdint> +#include <iostream> +#include <map> +#include <ranges> +#include <set> +#include <sstream> +#include <string> +#include <string_view> +#include <vector> +using namespace std; + +pair<uint64_t, uint64_t> split(uint64_t a) { + int digits = 0; + for (uint64_t b = a; b != 0; b /= 10) + digits++; + + if (digits % 2 == 1) + return make_pair(0, 0); + + uint64_t j = 1; + for (int k = 0; k < digits/2; k++) + j *= 10; + + return make_pair(a/j, a%j); +} + +uint64_t count(uint64_t a, int n) { + if (n == 0) + return 1; + + if (a == 0) + return count(1, n-1); + + if (auto [x, y] = split(a); x != 0) { + auto c1 = count(x, n-1); + auto c2 = count(y, n-1); + return c1+c2; + } + + return count(a*2024, n-1); +} + +int main() { + uint64_t x; + vector<uint64_t> old, v; + while (cin >> x) + v.push_back(x); + + uint64_t tot = 0; + for (auto a : v) + tot += count(a, 25); + + cout << tot << endl; + + return 0; +} diff --git a/2024/11/day11b.cpp b/2024/11/day11b.cpp @@ -0,0 +1,62 @@ +#include <algorithm> +#include <cstdint> +#include <iostream> +#include <map> +#include <ranges> +#include <set> +#include <sstream> +#include <string> +#include <string_view> +#include <vector> +using namespace std; + +map<pair<uint64_t, uint64_t>, uint64_t> t; + +pair<uint64_t, uint64_t> split(uint64_t a) { + int digits = 0; + for (uint64_t b = a; b != 0; b /= 10) + digits++; + + if (digits % 2 == 1) + return make_pair(0, 0); + + uint64_t j = 1; + for (int k = 0; k < digits/2; k++) + j *= 10; + + return make_pair(a/j, a%j); +} + +uint64_t count(uint64_t a, int n) { + if (n == 0) + return 1; + + if (auto it = t.find(make_pair(a, n)); it != t.end()) + return it->second; + + if (a == 0) + return t[make_pair(a, n)] = count(1, n-1); + + if (auto [x, y] = split(a); x != 0) { + auto c1 = count(x, n-1); + auto c2 = count(y, n-1); + return t[make_pair(a, n)] = c1+c2; + } + + return t[make_pair(a, n)] = count(a*2024, n-1); +} + +int main() { + uint64_t x; + vector<uint64_t> old, v; + while (cin >> x) + v.push_back(x); + + uint64_t tot = 0; + for (auto a : v) + tot += count(a, 75); + + cout << tot << endl; + + return 0; +} diff --git a/2024/11/input b/2024/11/input @@ -0,0 +1 @@ +92 0 286041 8034 34394 795 8 2051489 diff --git a/2024/learned.txt b/2024/learned.txt @@ -8,3 +8,4 @@ List of things I learned (or refreshed) with this year's AoC. * Day 5: std::find and std::replace * Day 8: set::insert_range(), but it is from C++23 only * Day 9: std::views +* Day 11: if(init; cond)