Skip to content

Commit

Permalink
Merge pull request #61 from EduardGomezEscandell/start-2023
Browse files Browse the repository at this point in the history
2023 🎅
  • Loading branch information
EduardGomezEscandell authored Nov 30, 2023
2 parents 540e27f + baa42cd commit 5e1a00f
Show file tree
Hide file tree
Showing 26 changed files with 532 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .github/workflows/2023.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "2023"

on:
push:
branches: [main]
pull_request:
branches: [main]
paths:
- .github/workflows/2023.yml
- "2023/**"
workflow_dispatch:

jobs:
run:
strategy:
fail-fast: false
matrix:
build_type: [Release, Debug]
sanitizer: ["no sanitizer", "sanitizer"]
runs-on: ubuntu-latest
# Need at least Ubuntu-22.10 to get GCC-13, and GitHub runners only go up to Ubuntu-22.04.
container: ubuntu:rolling
defaults:
run:
working-directory: ./2023
env:
BUILD_TYPE: ${{ matrix.build_type }}
ENABLE_SANITIZER: ${{ matrix.sanitizer == 'sanitizer' && '1' || '0' }}
BUILD_TESTS: "1"
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Install dependencies
run: ./dependencies
- name: Build
run: ./make
- name: Test
continue-on-error: true
run: ./build/${BUILD_TYPE}/test/test
- name: Run
run: ./build/${BUILD_TYPE}/cmd/aoc2023 --all
2 changes: 2 additions & 0 deletions 2023/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/**
.cache/**
21 changes: 21 additions & 0 deletions 2023/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_FLAGS "-Wall -Werror -Wextra -Wpedantic -Wconversion")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

project(synacor_challenge LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

if(ENABLE_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined")
endif()

message("Compiling with: ${CMAKE_CXX_FLAGS}")

add_subdirectory(external_libraries)
add_subdirectory(solvelib)
add_subdirectory(xmaslib)
add_subdirectory(cmd)
add_subdirectory(test)

41 changes: 41 additions & 0 deletions 2023/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 2023

## How to build
Install CMake, Git, GCC (≥13), and G++(≥13). If you're using the latest version of Ubuntu, you can use
```bash
./dependecies
```

Once the dependencies are installed, build with:
```bash
./make
```

The following environment variables control the build:
- BUILD_TYPE (default: Release): The build type
- ENABLE_SANITIZER (default: false): Wether to install sanitizers or not.
- BUILD_TESTS (default: false): Whether to build the tests or not.
- C (default: gcc-13): The executable for your C compiler
- CXX (default: g++-13): The executable for your C++ compiler

## How to run
To see options, use:

```
$ ./build/Release/cmd/aoc2023 --help
Usage:
aoc2023 -h | -help
Prints this message and exits
aoc2023 DAYS...
Runs the solution for the specified days
aoc2023 --all
Runs all solutions
```

To run the tests, use:
```
./build/Release/test/test
```
4 changes: 4 additions & 0 deletions 2023/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(aoc2023 main.cpp cmd.cpp)
set_target_properties(aoc2023 PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(aoc2023 INTERFACE ..)
target_link_libraries(aoc2023 PUBLIC solvelib xmaslib)
122 changes: 122 additions & 0 deletions 2023/cmd/cmd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "cmd.hpp"
#include "solvelib/alldays.hpp"
#include "xmaslib/registry/registry.hpp"

#include <algorithm>
#include <cstdlib>
#include <format>
#include <functional>
#include <iostream>
#include <numeric>
#include <ranges>
#include <stdexcept>
#include <string_view>
#include <vector>

template <typename... Args>
constexpr void log_error(std::format_string<Args...> msg, Args &&...args) {
std::cerr << "\x1b[31mERROR \x1b[0m "
<< std::format(msg, std::forward<Args>(args)...) << std::endl;
}

template <typename... Args>
constexpr void log_warning(std::format_string<Args...> msg, Args &&...args) {
std::cerr << "\x1b[33mWARNING\x1b[0m "
<< std::format(msg, std::forward<Args>(args)...) << std::endl;
}

constexpr int exit_success = EXIT_SUCCESS;
constexpr int exit_bad_args = 2;
constexpr int exit_error = 1;

int run(std::vector<std::string_view> &args) {
if (args.size() == 0) {
usage(std::cerr);
return exit_bad_args;
}

if (args.size() == 1 && (args[0] == "-h" || args[0] == "--help")) {
usage(std::cout);
return exit_success;
}

try {
populate_registry();
} catch (std::runtime_error &e) {
log_error("could not populate the registry fully: {}", e.what());
}
const auto &solutions = xmas::registered_solutions();

// Run all solutions
if (args.size() == 1 && (args[0] == "-a" || args[0] == "--all")) {
std::for_each(solutions.begin(), solutions.end(), solve_day);
return exit_success;
}

if(args[0].starts_with("-")) {
log_error("Unknown argument {}. Use --help to see possible inputs.", args[0]);
return exit_bad_args;
}

// Parse days requested
std::vector<std::map<const int, std::unique_ptr<xmas::solution>>::const_iterator>
days;
days.reserve(args.size());
for (auto &arg : args) {
if (arg.starts_with("-")) {
log_error("invalid mixed argument types, use --help to see valid inputs");
return exit_bad_args;
}

const int day = std::atoi(arg.data());
if (day == 0) {
log_warning("{} is not a vaid day", day);
continue;
}

auto it = solutions.find(day);
if (it == solutions.end()) {
log_warning("no solution registered for day {}", day);
continue;
}

days.push_back(it);
}

// No critical error: execute
const auto results = days | std::ranges::views::transform(
[](auto it) { return solve_day(*it); });
const bool all_successful =
std::reduce(results.begin(), results.end(), true, std::logical_and{});

if (!all_successful) {
return exit_error;
}

return exit_success;
}

void usage(std::ostream &s) {
s << "Usage:\n\n"
<< "aoc2023 -h\n"
<< "aoc2023 --help\n"
<< " Prints this message and exits\n\n"
<< "aoc2023 DAYS...\n"
<< " Runs the solution for the specified days\n\n"
<< "aoc2023 -a\n"
<< "aoc2023 --all\n"
<< " Runs all solutions\n\n";
}

bool solve_day(
std::map<const int, std::unique_ptr<xmas::solution>>::value_type const
&solution) {
try {
solution.second.get()->run();
return true;
} catch (std::runtime_error &e) {
log_error("day {} returned error: {}\n", solution.second.get()->day(),
e.what());
return false;
}
}
17 changes: 17 additions & 0 deletions 2023/cmd/cmd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <vector>
#include <string_view>
#include <map>
#include <memory>

#include "xmaslib/solvebase/solvebase.hpp"


int run(std::vector<std::string_view> &args);

void usage(std::ostream &s);

bool solve_day(
std::map<const int, std::unique_ptr<xmas::solution>>::value_type const
&solution);
18 changes: 18 additions & 0 deletions 2023/cmd/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// #include "xmaslib/solvebase/solvebase.hpp"
#include "cmd.hpp"

#include <cstdlib>
#include <string_view>
#include <vector>



int main(int argc, char **argv) {
std::vector<std::string_view> args;
args.reserve(static_cast<std::size_t>(argc));
for (auto i = 1; i < argc; ++i) {
args.emplace_back(argv[i]);
}

return run(args);
}
38 changes: 38 additions & 0 deletions 2023/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"directory": "/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-build",
"command": "/usr/bin/c++ -DDOCTEST_CONFIG_IMPLEMENT_WITH_MAIN -DENABLE_DOCTEST_IN_LIBRARY -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++11 -o CMakeFiles/doctest_with_main.dir/doctest/parts/doctest.cpp.o -c /home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src/doctest/parts/doctest.cpp",
"file": "/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src/doctest/parts/doctest.cpp",
"output": "_deps/doctest-build/CMakeFiles/doctest_with_main.dir/doctest/parts/doctest.cpp.o"
},
{
"directory": "/home/edu/AdventOfCode/2023/build/Release/solvelib",
"command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/xmaslib/.. -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/solvelib.dir/01/day01.cpp.o -c /home/edu/AdventOfCode/2023/solvelib/01/day01.cpp",
"file": "/home/edu/AdventOfCode/2023/solvelib/01/day01.cpp",
"output": "solvelib/CMakeFiles/solvelib.dir/01/day01.cpp.o"
},
{
"directory": "/home/edu/AdventOfCode/2023/build/Release/xmaslib",
"command": "/usr/bin/c++ -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/xmaslib.dir/registry/registry.cpp.o -c /home/edu/AdventOfCode/2023/xmaslib/registry/registry.cpp",
"file": "/home/edu/AdventOfCode/2023/xmaslib/registry/registry.cpp",
"output": "xmaslib/CMakeFiles/xmaslib.dir/registry/registry.cpp.o"
},
{
"directory": "/home/edu/AdventOfCode/2023/build/Release/cmd",
"command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/aoc2023.dir/main.cpp.o -c /home/edu/AdventOfCode/2023/cmd/main.cpp",
"file": "/home/edu/AdventOfCode/2023/cmd/main.cpp",
"output": "cmd/CMakeFiles/aoc2023.dir/main.cpp.o"
},
{
"directory": "/home/edu/AdventOfCode/2023/build/Release/cmd",
"command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/aoc2023.dir/cmd.cpp.o -c /home/edu/AdventOfCode/2023/cmd/cmd.cpp",
"file": "/home/edu/AdventOfCode/2023/cmd/cmd.cpp",
"output": "cmd/CMakeFiles/aoc2023.dir/cmd.cpp.o"
},
{
"directory": "/home/edu/AdventOfCode/2023/build/Release/test",
"command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/test.dir/test.cpp.o -c /home/edu/AdventOfCode/2023/test/test.cpp",
"file": "/home/edu/AdventOfCode/2023/test/test.cpp",
"output": "test/CMakeFiles/test.dir/test.cpp.o"
}
]
9 changes: 9 additions & 0 deletions 2023/dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -eu

export DEBIAN_NONINTERACTIVE=1

SUDO=$(which sudo) || true

$SUDO apt update -y
$SUDO apt install -y cmake g++-13 gcc-13 git
5 changes: 5 additions & 0 deletions 2023/external_libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
option(ENABLE_DOCTESTS "Include tests in the library. Setting this to OFF will remove all doctest related code.
Tests in tests/*.cpp will still be enabled." ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
include(Doctest.cmake)
9 changes: 9 additions & 0 deletions 2023/external_libraries/CxxOpts.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include(FetchContent)
FetchContent_Declare(
CxxOpts
GIT_REPOSITORY "https://github.com/jarro2783/cxxopts"
GIT_TAG "v3.1.1"
)

FetchContent_MakeAvailable(CxxOpts)
include_directories(${CXXOPTS_INCLUDE_DIR})
12 changes: 12 additions & 0 deletions 2023/external_libraries/Doctest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(ENABLE_DOCTESTS)
add_definitions(-DENABLE_DOCTEST_IN_LIBRARY)
include(FetchContent)
FetchContent_Declare(
DocTest
GIT_REPOSITORY "https://github.com/onqtam/doctest"
GIT_TAG "v2.4.11"
)

FetchContent_MakeAvailable(DocTest)
include_directories(${DOCTEST_INCLUDE_DIR})
endif()
33 changes: 33 additions & 0 deletions 2023/make
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
set -eu

export PROJECT_DIR=$(pwd)

export BUILD_TYPE="${BUILD_TYPE:-Release}"
export ENABLE_SANITIZER="${ENABLE_SANITIZER:-""}"
export BUILD_TESTS="${BUILD_TESTS:-""}"

export C="${C:-"gcc-13"}"
export CXX="${CXX:-"g++-13"}"

echo "Build type: ${BUILD_TYPE}"
echo

export BUILD_DIR="${PROJECT_DIR}/build/"
export SOURCE_DIR="${PROJECT_DIR}"

mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"

mkdir -p "${BUILD_TYPE}"
cd "${BUILD_TYPE}"

cmake "${SOURCE_DIR}" \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DENABLE_SANITIZER="${ENABLE_SANITIZER}" \
-DBUILD_TESTS="${BUILD_TESTS}"

cmake --build . -- -j $(nproc)

cd "${PROJECT_DIR}"
mv "${BUILD_DIR}/${BUILD_TYPE}/compile_commands.json" .
Loading

0 comments on commit 5e1a00f

Please sign in to comment.