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/solution.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 day02/solution.cpp (limited to 'day02/solution.cpp') 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; +} -- cgit v1.2.3