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_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 <iostream>
#include <format> #include <format>
#include "../common/common_functions.hpp"
namespace pb1 { namespace pb1 {
using namespace std::string_view_literals; using namespace std::string_view_literals;
@ -42,32 +44,6 @@ namespace pb1 {
{ "nine"sv, '9' }, { "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 { void part1(const std::string_view &toSearch, std::size_t &result) noexcept {
const auto first_digit_pos = toSearch.find_first_of(digits); const auto first_digit_pos = toSearch.find_first_of(digits);
if (first_digit_pos == std::string::npos) { if (first_digit_pos == std::string::npos) {
@ -79,7 +55,7 @@ namespace pb1 {
} }
std::size_t solve_problem_part1(const fs::path &problemFile) { 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) { if (!file_option) {
return 0; return 0;
} }
@ -223,7 +199,7 @@ namespace pb1 {
} }
std::size_t solve_problem_part2(const fs::path &problemFile) { 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) { if (!file_option) {
return 0; 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_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 "problem_2.hpp"
#include <optional> #include "../common/common_functions.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <format>
#include <list> #include <list>
#include <algorithm> #include <algorithm>
#include <numeric> #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) { Cube parse_line(const std::string &line) {
std::list<std::string_view> cubes_str; std::list<std::string_view> cubes_str;
std::list<Cube> cubes; std::list<Cube> cubes;
@ -76,53 +49,53 @@ namespace pb2 {
line_view = line_view.substr(line.find_first_of(':') + 1); line_view = line_view.substr(line.find_first_of(':') + 1);
boost::split(cubes_str, line_view.cbegin(), boost::is_any_of(",")); boost::split(cubes_str, line_view.cbegin(), boost::is_any_of(","));
return std::transform_reduce(cubes_str.begin(), return std::transform_reduce(cubes_str.begin(),
cubes_str.end(), cubes_str.end(),
Cube{}, Cube{},
std::plus<>{}, std::plus<>{},
[&cube_str](std::string_view &str) -> Cube { [&cube_str](std::string_view &str) -> Cube {
str = str.substr(1); str = str.substr(1);
const std::string tmp(str.cbegin(), str.cend()); const std::string tmp(str.cbegin(), str.cend());
boost::split(cube_str, tmp, boost::is_any_of(" ")); boost::split(cube_str, tmp, boost::is_any_of(" "));
Cube cube; Cube cube;
const auto &chars = cube_str.front(); const auto &chars = cube_str.front();
switch (cube_str.back().size()) { switch (cube_str.back().size()) {
case 3: case 3:
// means it's red // means it's red
if (const auto result = std::from_chars(chars.data(), if (const auto result = std::from_chars(chars.data(),
chars.data() + chars.size(), chars.data() + chars.size(),
cube.red); cube.red);
result.ec != std::errc{}) { result.ec != std::errc{}) {
std::cerr << "Error: " std::cerr << "Error: "
<< std::quoted(std::make_error_code(result.ec).message()) << std::quoted(std::make_error_code(result.ec).message())
<< '\n'; << '\n';
} }
break; break;
case 4: case 4:
// means it's blue // means it's blue
if (const auto result = std::from_chars(chars.data(), if (const auto result = std::from_chars(chars.data(),
chars.data() + chars.size(), chars.data() + chars.size(),
cube.blue); cube.blue);
result.ec != std::errc{}) { result.ec != std::errc{}) {
std::cerr << "Error: " std::cerr << "Error: "
<< std::quoted(std::make_error_code(result.ec).message()) << std::quoted(std::make_error_code(result.ec).message())
<< '\n'; << '\n';
} }
break; break;
default: default:
// means it's green // means it's green
if (const auto result = std::from_chars(chars.data(), if (const auto result = std::from_chars(chars.data(),
chars.data() + chars.size(), chars.data() + chars.size(),
cube.green); cube.green);
result.ec != std::errc{}) { result.ec != std::errc{}) {
std::cerr << "Error: " std::cerr << "Error: "
<< std::quoted(std::make_error_code(result.ec).message()) << std::quoted(std::make_error_code(result.ec).message())
<< '\n'; << '\n';
} }
break; break;
} }
cube_str.clear(); cube_str.clear();
return cube; return cube;
}); });
} }
constexpr int maxRed = 12; constexpr int maxRed = 12;
@ -132,7 +105,7 @@ namespace pb2 {
constexpr int maxBlue = 14; constexpr int maxBlue = 14;
std::size_t problem_part1(const fs::path &problemFile) noexcept { 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) { if (!file_optional) {
return 0; return 0;
} }