Problem 1 part 1 is OK, part 2 in progress
This commit is contained in:
parent
37dd1322cb
commit
a0d69feddb
8 changed files with 2194 additions and 2 deletions
100
pb_1/problem_1.cpp
Normal file
100
pb_1/problem_1.cpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
//
|
||||
// Created by postaron on 06/12/23.
|
||||
//
|
||||
|
||||
#include "problem_1.hpp"
|
||||
#include <array>
|
||||
#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,
|
||||
};
|
||||
|
||||
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;
|
||||
line_with_numbers.reserve(toSearch.size());
|
||||
for (int i = 0; i < toSearch.size(); ++i) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
17
pb_1/problem_1.hpp
Normal file
17
pb_1/problem_1.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Created by postaron on 06/12/23.
|
||||
//
|
||||
|
||||
#ifndef ADVENTOFCODE2023_PROBLEM_1_HPP
|
||||
#define ADVENTOFCODE2023_PROBLEM_1_HPP
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace pb1 {
|
||||
namespace fs = std::filesystem;
|
||||
std::size_t solve_problem_part1(const fs::path& problemFile);
|
||||
|
||||
std::size_t solve_problem_part2(const fs::path& problemFile);
|
||||
}
|
||||
|
||||
#endif //ADVENTOFCODE2023_PROBLEM_1_HPP
|
Loading…
Add table
Add a link
Reference in a new issue