Problem 1 part 1 is OK, part 2 in progress
This commit is contained in:
parent
37dd1322cb
commit
a0d69feddb
@ -1,6 +1,58 @@
|
|||||||
cmake_minimum_required(VERSION 3.27)
|
cmake_minimum_required(VERSION 3.27)
|
||||||
project(AdventOfCode2023)
|
project(AdventOfCode2023 CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
find_package(PkgConfig REQUIRED)
|
||||||
|
|
||||||
|
pkg_check_modules(Jemalloc REQUIRED jemalloc)
|
||||||
|
|
||||||
|
set(LINKER_OPTIONS
|
||||||
|
PRIVATE -Wl,--sort-common,--as-needed,--gc-sections,--strip-all)
|
||||||
|
|
||||||
|
set(COMPILE_FLAGS
|
||||||
|
-pipe
|
||||||
|
-march=native
|
||||||
|
-mtune=native
|
||||||
|
-mrdrnd
|
||||||
|
-mrdseed
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
-ffunction-sections
|
||||||
|
-fdata-sections
|
||||||
|
-funroll-loops
|
||||||
|
-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free
|
||||||
|
)
|
||||||
|
set(LINKER_OPTIONS
|
||||||
|
${LINKER_OPTIONS}
|
||||||
|
-fuse-ld=gold
|
||||||
|
)
|
||||||
|
|
||||||
|
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
|
||||||
|
set(COMPILE_FLAGS -stdlib=libc++ -Wmove ${COMPILE_FLAGS} -fwhole-program-vtables)
|
||||||
|
set(LINKER_OPTIONS ${LINKER_OPTIONS} -fwhole-program-vtables)
|
||||||
|
set(LINKER_FLAGS ${LINKER_FLAGS} c++)
|
||||||
|
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
|
||||||
|
set(COMPILE_FLAGS ${COMPILE_FLAGS} -fuse-ld=gold -fdevirtualize-at-ltrans)
|
||||||
|
set(LINKER_OPTIONS ${LINKER_OPTIONS} -fdevirtualize-at-ltrans)
|
||||||
|
set(LINKER_FLAGS ${LINKER_FLAGS} stdc++)
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
|
||||||
add_executable(AdventOfCode2023 main.cpp)
|
add_executable(AdventOfCode2023 main.cpp)
|
||||||
|
|
||||||
|
add_library(pb_1 STATIC pb_1/problem_1.cpp pb_1/problem_1.hpp)
|
||||||
|
|
||||||
|
|
||||||
|
set_target_properties(AdventOfCode2023 pb_1 PROPERTIES
|
||||||
|
CXX_STANDARD 20
|
||||||
|
CXX_STANDARD_REQUIRED ON
|
||||||
|
CXX_EXTENSIONS OFF
|
||||||
|
INTERPROCEDURAL_OPTIMIZATION ON
|
||||||
|
UNITY_BUILD ON
|
||||||
|
)
|
||||||
|
target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_OPTIONS})
|
||||||
|
target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS})
|
||||||
|
target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${Jemalloc_LIBRARIES})
|
||||||
|
target_link_libraries(pb_1 ${Jemalloc_LIBRARIES})
|
||||||
|
1000
inputs/pb1.txt
Normal file
1000
inputs/pb1.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
inputs/pb1_p2_sample.txt
Normal file
7
inputs/pb1_p2_sample.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
1000
inputs/pb1_part2.txt
Normal file
1000
inputs/pb1_part2.txt
Normal file
File diff suppressed because it is too large
Load Diff
4
inputs/pb1_sample.txt
Normal file
4
inputs/pb1_sample.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
14
main.cpp
14
main.cpp
@ -1,6 +1,18 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "pb_1/problem_1.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!\n";
|
{
|
||||||
|
std::cout << "Begin Problem 1 part 1\n";
|
||||||
|
if (const auto result = pb1::solve_problem_part1("inputs/pb1.txt"); result != 0) {
|
||||||
|
std::cout << "Problem 1 part1: " << result << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::cout << "Begin Problem 1 part 2\n";
|
||||||
|
if (const auto result = pb1::solve_problem_part2("inputs/pb1_p2_sample.txt"); result != 0) {
|
||||||
|
std::cout << "Problem 1 part2: " << result << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
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…
Reference in New Issue
Block a user