diff --git a/src/day-5-solution.cpp b/src/day-5-solution.cpp new file mode 100644 index 0000000..07637b0 --- /dev/null +++ b/src/day-5-solution.cpp @@ -0,0 +1,79 @@ +#include "spdlog/spdlog.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +uint_fast64_t part_one_solution(const std::vector>& ranges, const std::vector& ids){ + uint_fast64_t runnign_sum = uint_fast64_t {0}; + for (uint_fast64_t id : ids) { + for (std::pair range : ranges) { + if(!(id>=range.first && id <= range.second)) continue; + runnign_sum++; + break; + } + } + return runnign_sum; +} + +bool range_ascending_compare(const std::pair&a , const std::pair& b){ + return a.first> ranges_to_be_merged){ + uint_fast64_t total_fresh_ids = uint_fast64_t {0}; + std::sort(ranges_to_be_merged.begin(), ranges_to_be_merged.end(), range_ascending_compare); + std::vector> merged_ranges = std::vector> {}; + + for (std::vector>::const_iterator i = ranges_to_be_merged.begin(); i< ranges_to_be_merged.end(); i++) { + if(merged_ranges.empty() || merged_ranges.back().second < i->first) merged_ranges.push_back(*i); + else merged_ranges.back().second = std::max(merged_ranges.back().second, i->second); + + } + + for (std::vector>::const_iterator j = merged_ranges.begin(); jsecond - j->first +1; + } + + return total_fresh_ids; +} + +int main (int argc, char *argv[]) { + std::ifstream input_file = std::ifstream{"testcases/day-5.in"}; + std::vector> ranges = std::vector> {}; + std::vector ids = std::vector {}; + std::string current_line = std::string {}; + + const std::regex range_pattern = std::regex {"([0-9]+)-([0-9]+)"}; + while(std::getline(input_file, current_line)){ + if(std::all_of(current_line.begin(), current_line.end(), [](unsigned char c){return std::isspace(c);})) break; + std::smatch string_matches; + + if(std::regex_search(current_line, string_matches, range_pattern)){ + ranges.push_back(std::pair{static_cast( std::stoull(string_matches[1].str()) ),static_cast( std::stoull(string_matches[2].str()) )}); + } + + } + + while (std::getline(input_file, current_line)) { + // Skip blank/whitespace-only lines + if (std::all_of(current_line.begin(), current_line.end(), + [](unsigned char c) { return std::isspace(c); })) { + continue; + } + + ids.push_back(static_cast(std::stoull(current_line)));} + + + uint_fast64_t part_one_solution_result = part_one_solution(ranges , ids); + spdlog::info("Part one solution: {}",part_one_solution_result ); + uint_fast64_t part_two_solution_result = part_two_solution(ranges); + spdlog::info("Part two solution: {}", part_two_solution_result); + + return 0; +}