From 42feac3994af485546825f43a1e467a441bda510 Mon Sep 17 00:00:00 2001 From: ajet Date: Sat, 13 Dec 2025 19:45:42 -1000 Subject: [PATCH] do day 3 --- input/2025-3.sample.txt | 4 +++ src/day3.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 input/2025-3.sample.txt create mode 100644 src/day3.cpp diff --git a/input/2025-3.sample.txt b/input/2025-3.sample.txt new file mode 100644 index 0000000..7255fca --- /dev/null +++ b/input/2025-3.sample.txt @@ -0,0 +1,4 @@ +987654321111111 +811111111111119 +234234234234278 +818181911112111 diff --git a/src/day3.cpp b/src/day3.cpp new file mode 100644 index 0000000..41d8c77 --- /dev/null +++ b/src/day3.cpp @@ -0,0 +1,59 @@ +// :autocmd BufWritePost *.cpp :silent exec "!./build 3" +#include +#include +#include +#include +#include +#include +#include + +using Data = std::string; + +int64_t part_1(const std::vector &); +int64_t part_2(const std::vector &); + +int main() { + std::ifstream file{"input/2025-3.txt"}; + Data line; + std::vector v; + while (std::getline(file, line)) { + v.push_back(line); + } + std::cout << "Part 1: " << part_1(v) << std::endl; + std::cout << "Part 2: " << part_2(v) << std::endl; +} + +char get_max_char(std::string_view s) { + return std::accumulate(s.begin(), s.end(), '0', + [](const char maximium_char, const char c) { + return std::max(maximium_char, c); + }); +} + +int64_t solve(const std::string_view line, size_t cnt_to_activate) { + const auto search{line.substr(0, line.size() - cnt_to_activate + 1)}; + const char max_char{get_max_char(search)}, null_terminator{'\0'}; + const auto max_char_idx{search.find(&max_char, 0)}; + const int64_t max_char_as_int = max_char - '0'; + if (cnt_to_activate == 1) { + return max_char_as_int; + } + const auto recur{solve(line.substr(max_char_idx + 1), cnt_to_activate - 1)}; + return std::stoll(std::string(&max_char) + std::to_string(recur)); +} + +int64_t part_1(const std::vector &v) { + int64_t ans{}; + for (auto d : v) { + ans += solve(d, 2); + } + return ans; +} + +int64_t part_2(const std::vector &v) { + int64_t ans{}; + for (auto d : v) { + ans += solve(d, 12); + } + return ans; +}