part two tm
This commit is contained in:
90
src/day-2-solution.cpp
Normal file
90
src/day-2-solution.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/fmt/bundled/format.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::pair<uint_fast64_t,uint_fast64_t>> parse_ranges(std::string_view input) {
|
||||
std::vector<std::pair<uint_fast64_t,uint_fast64_t>> 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<std::pair<uint_fast64_t, uint_fast64_t>> ranges ){
|
||||
uint_fast64_t running_sum = uint_fast64_t{0};
|
||||
for (std::vector<std::pair<uint_fast64_t, uint_fast64_t>>::const_iterator i =ranges.cbegin(); i<ranges.cend(); i++) {
|
||||
spdlog::debug("range: {} to {}", i->first, 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<size_t>(mid-start)};
|
||||
std::string_view second_half = std::string_view{&*mid, static_cast<size_t>(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_t>(size_of_buffer), '\0');
|
||||
|
||||
if(!input.read(loaded_buffer.data(),size_of_buffer)) {
|
||||
spdlog::critical("Read error"); std::terminate();
|
||||
}
|
||||
|
||||
std::vector<std::pair<uint_fast64_t, uint_fast64_t>> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user