Using common files
This commit is contained in:
		
					parent
					
						
							
								1681640aaa
							
						
					
				
			
			
				commit
				
					
						273384c8e7
					
				
			
		
					 4 changed files with 60 additions and 109 deletions
				
			
		| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue