done with day 6 part 1
This commit is contained in:
81
src/day-6-solution.cpp
Normal file
81
src/day-6-solution.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
enum class Operation {
|
||||
ADD='+', MULTIPLY='*'
|
||||
};
|
||||
|
||||
|
||||
uint_fast64_t part_one_solve_worksheet(const std::vector<std::pair<std::vector<uint_fast64_t>, Operation>>& worksheet){
|
||||
uint_fast64_t running_sum = uint_fast64_t {0};
|
||||
for (std::vector<std::pair<std::vector<uint_fast64_t>,Operation>>::const_iterator i = worksheet.cbegin(); i<worksheet.cend(); i++) {
|
||||
uint_fast64_t current_calc = * i->first.begin();
|
||||
switch (i->second) {
|
||||
case Operation::ADD :
|
||||
for (std::vector<uint_fast64_t>::const_iterator j = i->first.cbegin()+1; j< i->first.cend(); j++) {
|
||||
current_calc+= *j;
|
||||
}
|
||||
break;
|
||||
case Operation::MULTIPLY:
|
||||
for (std::vector<uint_fast64_t>::const_iterator j = i->first.cbegin()+1; j< i->first.cend() ; j++) {
|
||||
current_calc*= *j;
|
||||
}
|
||||
}
|
||||
running_sum+=current_calc;
|
||||
}
|
||||
return running_sum;
|
||||
}
|
||||
std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> part_one_parse(std::ifstream& input_file){
|
||||
|
||||
std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> worksheet = std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> {};
|
||||
std::string current_line = std::string{};
|
||||
while (std::getline(input_file, current_line)) {
|
||||
|
||||
std::string_view current_line_string_view = std::string_view{current_line};
|
||||
auto split_view = std::ranges::views::split(current_line_string_view, ' ');
|
||||
|
||||
std::vector<std::string_view> groups = std::vector<std::string_view>{};
|
||||
for (auto&& token : split_view) {
|
||||
if(token.empty()) continue;
|
||||
groups.push_back(std::string_view{token});
|
||||
}
|
||||
|
||||
if(worksheet.empty()) worksheet.assign(groups.size(), std::pair<std::vector<uint_fast64_t>, Operation> { std::vector<uint_fast64_t> {}, Operation::ADD });
|
||||
|
||||
if(input_file.peek()==EOF){
|
||||
|
||||
for (std::vector<std::string_view>::const_iterator i = groups.begin(); i<groups.end(); i++) {
|
||||
worksheet[static_cast<size_t>(i-groups.begin())].second = static_cast<Operation>(*(i->begin()));
|
||||
}
|
||||
|
||||
}else{
|
||||
for (std::vector<std::string_view>::const_iterator i = groups.begin(); i<groups.end(); i++) {
|
||||
uint_fast64_t value;
|
||||
std::from_chars(i->data(), i->data() + i->size(), value);
|
||||
worksheet[static_cast<size_t>(i-groups.begin())].first.push_back(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
input_file.seekg(0);
|
||||
return worksheet;
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
std::ifstream input_file = std::ifstream{"testcases/day-6.in"};
|
||||
std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> part_one_worksheet = part_one_parse(input_file);
|
||||
spdlog::info("Number of problems captured: {}", part_one_worksheet.size());
|
||||
uint_fast64_t part_one_result = part_one_solve_worksheet(part_one_worksheet);
|
||||
spdlog::info("Part one solution: {}", part_one_result);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user