From 140dd1171538b1402d4918fc18a94995536ee9b4 Mon Sep 17 00:00:00 2001 From: Mars Ultor Date: Tue, 2 Dec 2025 00:35:58 -0600 Subject: [PATCH] part two tm --- src/day-2-solution.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/day-2-solution.cpp diff --git a/src/day-2-solution.cpp b/src/day-2-solution.cpp new file mode 100644 index 0000000..b95b51a --- /dev/null +++ b/src/day-2-solution.cpp @@ -0,0 +1,90 @@ + +#include "spdlog/common.h" +#include "spdlog/fmt/bundled/format.h" +#include "spdlog/spdlog.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +std::vector> parse_ranges(std::string_view input) { + std::vector> ranges; + + size_t pos = 0; + while (pos < input.size()) { + // Find next '-' for the start-end split + size_t dash = input.find('-', pos); + if (dash == std::string_view::npos) break; + + uint_fast64_t start; + auto [ptr1, ec1] = std::from_chars(input.data() + pos, input.data() + dash, start); + if (ec1 != std::errc()) break; + + pos = dash + 1; + + // Find next ',' for end of range + size_t comma = input.find(',', pos); + if (comma == std::string_view::npos) comma = input.size(); + + uint_fast64_t end; + auto [ptr2, ec2] = std::from_chars(input.data() + pos, input.data() + comma, end); + if (ec2 != std::errc()) break; + + ranges.emplace_back(start, end); + + pos = comma + 1; // move past comma + } + + return ranges; +} + +uint_fast64_t solution_part_one(std::vector> ranges ){ + uint_fast64_t running_sum = uint_fast64_t{0}; + for (std::vector>::const_iterator i =ranges.cbegin(); ifirst, i->second); + for (uint_fast64_t j = i->first; j<= i->second; j++) { + std::string number = fmt::format("{}",j); + if(number.size()%2 ==1) continue; + + std::string::const_iterator start = number.cbegin(); + std::string::const_iterator mid = number.cbegin()+number.size()/2; + std::string::const_iterator end = number.cend(); + + std::string_view first_half = std::string_view{&*start, static_cast(mid-start)}; + std::string_view second_half = std::string_view{&*mid, static_cast(end-mid)}; + + if(first_half==second_half) running_sum+=j; + } + } + return running_sum; +} + + +int main (int argc, char *argv[]) { + spdlog::set_level(spdlog::level::level_enum::debug); + + std::ifstream input = std::ifstream{"testcases/day-2.in", std::ios_base::binary | std::ios::ate}; + + std::streamsize size_of_buffer = input.tellg(); + input.seekg(0, std::ios::beg); + + std::string loaded_buffer = std::string(static_cast(size_of_buffer), '\0'); + + if(!input.read(loaded_buffer.data(),size_of_buffer)) { + spdlog::critical("Read error"); std::terminate(); + } + + std::vector> ranges = parse_ranges(loaded_buffer); + spdlog::debug("Size of ranges: {}", ranges.size()); + uint_fast64_t first_part_response = solution_part_one(ranges); + spdlog::info("Solution to part one: {}", first_part_response); + + + return 0; +}