From a86449e97b0201af1aa71f5ba3a569975ff0c61c Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Thu, 4 Dec 2025 03:59:35 +0200 Subject: day02 --- day02/Makefile | 1 + day02/solution.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ day02/tests/test1.input | 3 ++ day02/tests/test1.output | 2 ++ 4 files changed, 91 insertions(+) create mode 100644 day02/Makefile create mode 100644 day02/solution.cpp create mode 100644 day02/tests/test1.input create mode 100644 day02/tests/test1.output (limited to 'day02') diff --git a/day02/Makefile b/day02/Makefile new file mode 100644 index 0000000..2fa98c0 --- /dev/null +++ b/day02/Makefile @@ -0,0 +1 @@ +include ../aoc.mk diff --git a/day02/solution.cpp b/day02/solution.cpp new file mode 100644 index 0000000..23304c1 --- /dev/null +++ b/day02/solution.cpp @@ -0,0 +1,85 @@ +#include + +namespace views = std::views; +namespace ranges = std::ranges; + +auto parse_input(std::istream& is) { + const std::string input = { + std::istreambuf_iterator(is), + std::istreambuf_iterator() + }; + + auto char_range_to_int = [](auto&& r) { + auto&& s = ranges::to(r); + return std::stoll(s); + }; + auto lines = views::split(input, ',') + | views::filter([](auto line) { return not line.empty(); }); + + std::vector> _ranges; + for (auto const line : lines) { + auto numbers = line + | views::split('-') + | views::transform(char_range_to_int); + _ranges.emplace_back(*numbers.begin(), *std::next(numbers.begin())); + } + return _ranges; +} + + +void part1(auto const& input) { + // Write first part solution here + long answer = 0; + + for (auto [begin, end] : input) { + for (auto i = begin; i <= end; ++i) { + std::string s{std::format("{}", i)}; + size_t half_size = s.size() / 2; + std::string_view sl{s.begin(), s.begin() + half_size}; + std::string_view sr{s.begin() + half_size, s.end()}; + if (sl == sr) { + answer += i; + } + } + } + + std::println("{}", answer); +} + +void part2(auto const& input) { + // Write second part solution here + long answer = 0; + + for (auto [begin, end] : input) { + for (auto i = begin; i <= end; ++i) { + std::stringstream ss; + ss << i; + std::string_view s{ss.str()}; + for (int chunk_size = 1; chunk_size <= s.size() / 2; ++chunk_size) { + if (s.size() % chunk_size != 0) { + continue; + } + auto chunks = s | views::chunk(chunk_size); + auto value = *chunks.begin(); + if (ranges::all_of(chunks, [value](auto&& chunk) { + return ranges::equal(chunk, value); + })) { + answer += i; + break; + } + } + } + } + + std::println("{}", answer); + +} + +int main() { + auto const input = parse_input(std::cin); + + part1(input); + part2(input); + + return 0; +} diff --git a/day02/tests/test1.input b/day02/tests/test1.input new file mode 100644 index 0000000..b3dc7c4 --- /dev/null +++ b/day02/tests/test1.input @@ -0,0 +1,3 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124 \ No newline at end of file diff --git a/day02/tests/test1.output b/day02/tests/test1.output new file mode 100644 index 0000000..0ab4cb7 --- /dev/null +++ b/day02/tests/test1.output @@ -0,0 +1,2 @@ +1227775554 +4174379265 -- cgit v1.2.3