wierd problem. idk how counting works
This commit is contained in:
79
src/day-5-solution.cpp
Normal file
79
src/day-5-solution.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
uint_fast64_t part_one_solution(const std::vector<std::pair<uint_fast64_t, uint_fast64_t>>& ranges, const std::vector<uint_fast64_t>& ids){
|
||||
uint_fast64_t runnign_sum = uint_fast64_t {0};
|
||||
for (uint_fast64_t id : ids) {
|
||||
for (std::pair<uint_fast64_t, uint_fast64_t> range : ranges) {
|
||||
if(!(id>=range.first && id <= range.second)) continue;
|
||||
runnign_sum++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return runnign_sum;
|
||||
}
|
||||
|
||||
bool range_ascending_compare(const std::pair<uint_fast64_t, uint_fast64_t>&a , const std::pair<uint_fast64_t, uint_fast64_t>& b){
|
||||
return a.first<b.first;
|
||||
}
|
||||
|
||||
uint_fast64_t part_two_solution(std::vector<std::pair<uint_fast64_t,uint_fast64_t>> 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<std::pair<uint_fast64_t, uint_fast64_t>> merged_ranges = std::vector<std::pair<uint_fast64_t, uint_fast64_t>> {};
|
||||
|
||||
for (std::vector<std::pair<uint_fast64_t, uint_fast64_t>>::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<std::pair<uint_fast64_t, uint_fast64_t>>::const_iterator j = merged_ranges.begin(); j<merged_ranges.end(); j++) {
|
||||
total_fresh_ids += j->second - 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<std::pair<uint_fast64_t, uint_fast64_t>> ranges = std::vector<std::pair<uint_fast64_t, uint_fast64_t>> {};
|
||||
std::vector<uint_fast64_t> ids = std::vector<uint_fast64_t> {};
|
||||
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<uint_fast64_t>( std::stoull(string_matches[1].str()) ),static_cast<uint_fast64_t>( 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<uint_fast64_t>(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;
|
||||
}
|
||||
Reference in New Issue
Block a user