0
0
Fork 0
AdventOfCode2023/pb_1/problem_1.cpp

159 lines
5.1 KiB
C++

//
// Created by postaron on 06/12/23.
//
#include "problem_1.hpp"
#include <array>
#include <string>
#include <string_view>
#include <optional>
#include <unordered_set>
#include <vector>
#include <fstream>
#include <iostream>
#include <format>
namespace pb1 {
using namespace std::string_view_literals;
constexpr std::string_view digits = "0123456789";
constexpr std::array digitLetters{
"one"sv,
"two"sv,
"three"sv,
"four"sv,
"five"sv,
"six"sv,
"seven"sv,
"eight"sv,
"nine"sv,
};
const std::unordered_set numbersSet{
"one"sv,
"two"sv,
"three"sv,
"four"sv,
"five"sv,
"six"sv,
"seven"sv,
"eight"sv,
"nine"sv,
};
std::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept {
std::ifstream file(problemFile);
if (!file.is_open()) {
const auto error_state = file.rdstate();
switch (error_state) {
case std::ios::badbit:
std::cerr << "Fatal I/O error occurred.\n";
break;
case std::ios::eofbit:
std::cerr << "End of file reached.\n";
break;
case std::ios::failbit:
std::cerr << "Non-fatal I/O error occurred.\n";
break;
default:
std::cerr << "impossible to reach.\n";
break;
}
const auto path_string = problemFile.string();
const auto msg = std::format("Failed to open file {}: ", path_string);
std::perror(msg.c_str());
return std::nullopt;
}
return file;
}
void part1(const std::string_view &toSearch, std::size_t &result) noexcept {
const auto first_digit_pos = toSearch.find_first_of(digits);
if (first_digit_pos == std::string::npos) {
return;
}
const auto second_part = toSearch.substr(first_digit_pos);
const auto second_digit_pos = second_part.find_last_of(digits) + first_digit_pos;
result += (toSearch[first_digit_pos] - '0') * 10 + (toSearch[second_digit_pos] - '0');
}
std::size_t solve_problem_part1(const fs::path &problemFile) {
auto file_option = read_file(problemFile);
if (!file_option) {
return 0;
}
std::ifstream &file = *file_option;
std::size_t result = 0;
for (std::string line; std::getline(file, line);) {
part1(line, result);
}
return result;
}
void part2(const std::string_view &toSearch, std::vector<std::string> &splittedLine, std::size_t &result) noexcept {
std::string line_with_numbers, word;
line_with_numbers.reserve(toSearch.size());
word.reserve(sizeof("seven") - 1);
for (int i = 0; i < toSearch.size(); ++i) {
const int second_letter = i + 1, third = i + 2, fourth = i + 3, fifth = i + 4, sixth = i + 5;
switch (toSearch[i]) {
case 'o': {
if (second_letter >= toSearch.size() or third >= toSearch.size()) {
line_with_numbers.append(toSearch.substr(i));
continue;
}
word.append(toSearch.substr(i, digitLetters[0].size()));
if (!numbersSet.contains(word)) {
const auto pos = word.find_first_of(digits);
if (pos != std::string::npos) {
line_with_numbers.push_back(word[pos]);
}
if (word[1] == 'n') {
++i;
}
} else {
line_with_numbers.push_back('1');
++i;
}
word.clear();
}
break;
case 't':
break;
case 'f':
break;
case 's':
break;
case 'e':
break;
case 'n':
break;
default:
if ((toSearch[i] - '0') < 10) {
line_with_numbers.push_back(toSearch[i]);
}
continue;
}
}
if (!line_with_numbers.empty()) {
result += (line_with_numbers.front() - '0') * 10 + (line_with_numbers.back() - '0');
}
}
std::size_t solve_problem_part2(const fs::path &problemFile) {
auto file_option = read_file(problemFile);
if (!file_option) {
return 0;
}
std::ifstream &file = *file_option;
std::vector<std::string> splitted_line;
std::size_t result = 0;
for (std::string line; std::getline(file, line);) {
part2(line, splitted_line, result);
splitted_line.clear();
}
return result;
}
}