this was so annoying

This commit is contained in:
2025-12-06 17:10:16 -06:00
parent 109a964cb5
commit 190f9d59ff

View File

@@ -1,3 +1,5 @@
#include "spdlog/common.h"
#include "spdlog/spdlog.h"
#include <algorithm>
#include <charconv>
@@ -70,12 +72,115 @@ std::string_view current_line_string_view = std::string_view{current_line};
return worksheet;
}
std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> part_two_parse(std::ifstream& input_file) {
std::vector<std::string> lines;
std::string current_line;
while (std::getline(input_file, current_line))
lines.push_back(current_line);
if (lines.empty()) return {};
size_t num_rows = lines.size();
size_t num_cols = 0;
for (std::string& l : lines) num_cols = std::max(num_cols, l.size());
for (std::string& l : lines) {
if (l.size() < num_cols) l.resize(num_cols, ' ');
}
const std::string &operator_row = lines.back();
std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> worksheet;
int col = static_cast<int>(num_cols) - 1;
while (col >= 0) {
bool is_separator = true;
for (size_t row = 0; row < num_rows - 1; ++row) {
if (!std::isspace(lines[row][col])) {
is_separator = false;
break;
}
}
if (is_separator) {
--col;
continue;
}
int right_col = col;
int left_col = col;
while (left_col > 0) {
bool has_content = false;
for (size_t row = 0; row < num_rows - 1; ++row) {
if (!std::isspace(lines[row][left_col - 1])) {
has_content = true;
break;
}
}
if (!has_content) break;
--left_col;
}
spdlog::debug("Problem block from col {} to {}", left_col, right_col);
std::vector<uint_fast64_t> numbers;
for (int c = left_col; c <= right_col; ++c) {
std::string num_str;
for (size_t row = 0; row < num_rows - 1; ++row) {
if (std::isdigit(lines[row][c])) {
num_str += lines[row][c];
}
}
if (!num_str.empty()) {
uint_fast64_t value;
std::from_chars(num_str.data(), num_str.data() + num_str.size(), value);
numbers.push_back(value);
spdlog::debug(" Column {} -> number {}", c, value);
}
}
Operation op = Operation::ADD;
if (left_col < static_cast<int>(operator_row.size())) {
char op_char = operator_row[left_col];
spdlog::debug(" Operator at [row={}, col={}]: '{}' (ASCII {})",
num_rows - 1, left_col, op_char, static_cast<int>(op_char));
if (op_char == '+') {
op = Operation::ADD;
} else if (op_char == '*') {
op = Operation::MULTIPLY;
}
}
spdlog::debug(" Final operator: {}", (op == Operation::ADD ? '+' : '*'));
spdlog::debug(" Final numbers order: ");
for (auto n : numbers) {
spdlog::debug(" {}", n);
}
worksheet.push_back({numbers, op});
col = left_col - 1;
}
input_file.clear();
input_file.seekg(0);
spdlog::debug("Total problems captured: {}", worksheet.size());
return worksheet;
}
int main (int argc, char *argv[]) {
spdlog::set_level(spdlog::level::info);
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::debug("test");
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);
std::ifstream input_part_two = std::ifstream{"testcases/day-6.in"};
std::vector<std::pair<std::vector<uint_fast64_t>, Operation>> part_two_worksheet = part_two_parse(input_part_two);
spdlog::info("part two problems captured: {}", part_two_worksheet.size());
uint_fast64_t part_two_result = part_one_solve_worksheet(part_two_worksheet);
spdlog::info("Part Two solution: {}", part_two_result);
return 0;
}