0
0

Using common files

This commit is contained in:
Pcornat 2024-07-31 22:20:57 +02:00
parent 1681640aaa
commit 273384c8e7
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
4 changed files with 60 additions and 109 deletions

View File

@ -4,8 +4,8 @@ add_library(pb_1 STATIC problem_1.cpp problem_1.hpp)
target_compile_definitions(pb_1 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
#target_compile_options(pb_1 PUBLIC ${COMPILE_FLAGS})
target_compile_options(pb_1 PUBLIC ${COMPILE_FLAGS})
#target_link_options(pb_1 PUBLIC ${LINKER_OPTIONS})
target_link_options(pb_1 PUBLIC ${LINKER_OPTIONS})
target_link_libraries(pb_1 PUBLIC ${LINKER_FLAGS})
target_link_libraries(pb_1 PUBLIC ${LINKER_FLAGS} common)

View File

@ -13,6 +13,8 @@
#include <iostream>
#include <format>
#include "../common/common_functions.hpp"
namespace pb1 {
using namespace std::string_view_literals;
@ -42,32 +44,6 @@ namespace pb1 {
{ "nine"sv, '9' },
};
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) {
@ -79,7 +55,7 @@ namespace pb1 {
}
std::size_t solve_problem_part1(const fs::path &problemFile) {
auto file_option = read_file(problemFile);
auto file_option = common::read_file(problemFile);
if (!file_option) {
return 0;
}
@ -223,7 +199,7 @@ namespace pb1 {
}
std::size_t solve_problem_part2(const fs::path &problemFile) {
auto file_option = read_file(problemFile);
auto file_option = common::read_file(problemFile);
if (!file_option) {
return 0;
}

View File

@ -8,6 +8,8 @@ target_include_directories(pb_2 PRIVATE ${Boost_INCLUDE_DIR})
target_compile_definitions(pb_2 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
#target_compile_options(pb_2 PUBLIC ${COMPILE_FLAGS})
target_compile_options(pb_2 PUBLIC ${COMPILE_FLAGS})
#target_link_options(pb_2 PUBLIC ${LINKER_OPTIONS})
target_link_options(pb_2 PUBLIC ${LINKER_OPTIONS})
target_link_libraries(pb_2 PUBLIC ${LINKER_FLAGS} common)

View File

@ -3,10 +3,9 @@
//
#include "problem_2.hpp"
#include <optional>
#include "../common/common_functions.hpp"
#include <iostream>
#include <fstream>
#include <format>
#include <list>
#include <algorithm>
#include <numeric>
@ -41,32 +40,6 @@ namespace pb2 {
}
};
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;
}
Cube parse_line(const std::string &line) {
std::list<std::string_view> cubes_str;
std::list<Cube> cubes;
@ -132,7 +105,7 @@ namespace pb2 {
constexpr int maxBlue = 14;
std::size_t problem_part1(const fs::path &problemFile) noexcept {
auto file_optional = read_file(problemFile);
auto file_optional = common::read_file(problemFile);
if (!file_optional) {
return 0;
}