From 3418444a8da97e5a05f87f9737bed94edd43ec60 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Wed, 11 Dec 2024 07:03:12 +0100 Subject: Day 11 2024 --- 2024/11/Makefile | 24 +++++++++++++++++++++ 2024/11/day11a.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 2024/11/day11b.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2024/learned.txt | 1 + 4 files changed, 144 insertions(+) create mode 100644 2024/11/Makefile create mode 100644 2024/11/day11a.cpp create mode 100644 2024/11/day11b.cpp (limited to '2024') diff --git a/2024/11/Makefile b/2024/11/Makefile new file mode 100644 index 0000000..a5d27bb --- /dev/null +++ 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 new file mode 100644 index 0000000..50c3ebf --- /dev/null +++ b/2024/11/day11a.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +pair 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 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 new file mode 100644 index 0000000..7f78e8e --- /dev/null +++ b/2024/11/day11b.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +map, uint64_t> t; + +pair 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 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/learned.txt b/2024/learned.txt index 56fa515..440571a 100644 --- 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) -- cgit v1.3