Compare commits
No commits in common. "109d3333419ad2266db58aa71b4a856da2a336b3" and "37dd1322cb1dae05c310a0a38b57a6951cc0861e" have entirely different histories.
109d333341
...
37dd1322cb
9 changed files with 2 additions and 2253 deletions
0
.gitmodules
vendored
0
.gitmodules
vendored
|
@ -1,59 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.27)
|
cmake_minimum_required(VERSION 3.27)
|
||||||
project(AdventOfCode2023 CXX)
|
project(AdventOfCode2023)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
find_package(PkgConfig REQUIRED)
|
|
||||||
|
|
||||||
pkg_check_modules(Jemalloc REQUIRED jemalloc)
|
|
||||||
|
|
||||||
set(LINKER_OPTIONS -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_definitions(AdventOfCode2023 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(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 PUBLIC ${Jemalloc_LIBRARIES})
|
|
||||||
|
|
1000
inputs/pb1.txt
1000
inputs/pb1.txt
File diff suppressed because it is too large
Load diff
|
@ -1,7 +0,0 @@
|
||||||
abcone2threexyz
|
|
||||||
two1nine
|
|
||||||
eightwothree
|
|
||||||
xtwone3four
|
|
||||||
4nineeightseven2
|
|
||||||
zoneight234
|
|
||||||
7pqrstsixteen
|
|
1000
inputs/pb1_part2.txt
1000
inputs/pb1_part2.txt
File diff suppressed because it is too large
Load diff
|
@ -1,4 +0,0 @@
|
||||||
1abc2
|
|
||||||
pqr3stu8vwx
|
|
||||||
a1b2c3d4e5f
|
|
||||||
treb7uchet
|
|
14
main.cpp
14
main.cpp
|
@ -1,18 +1,6 @@
|
||||||
#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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,158 +0,0 @@
|
||||||
//
|
|
||||||
// Created by postaron on 06/12/23.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "problem_1.hpp"
|
|
||||||
#include <array>
|
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
|
||||||
#include <optional>
|
|
||||||
#include <unordered_set>
|
|
||||||
#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,
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::unordered_set numbersSet{
|
|
||||||
"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, word;
|
|
||||||
line_with_numbers.reserve(toSearch.size());
|
|
||||||
word.reserve(sizeof("seven") - 1);
|
|
||||||
for (int i = 0; i < toSearch.size(); ++i) {
|
|
||||||
const int second_letter = i + 1, third = i + 2, fourth = i + 3, fifth = i + 4, sixth = i + 5;
|
|
||||||
switch (toSearch[i]) {
|
|
||||||
case 'o': {
|
|
||||||
if (second_letter >= toSearch.size() or third >= toSearch.size()) {
|
|
||||||
line_with_numbers.append(toSearch.substr(i));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
word.append(toSearch.substr(i, digitLetters[0].size()));
|
|
||||||
if (!numbersSet.contains(word)) {
|
|
||||||
const auto pos = word.find_first_of(digits);
|
|
||||||
if (pos != std::string::npos) {
|
|
||||||
line_with_numbers.push_back(word[pos]);
|
|
||||||
}
|
|
||||||
if (word[1] == 'n') {
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
line_with_numbers.push_back('1');
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
word.clear();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if ((toSearch[i] - '0') < 10) {
|
|
||||||
line_with_numbers.push_back(toSearch[i]);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!line_with_numbers.empty()) {
|
|
||||||
result += (line_with_numbers.front() - '0') * 10 + (line_with_numbers.back() - '0');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
//
|
|
||||||
// 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