From cd4c32c2a3187f06acf88c2cd85d058c99f86b36 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 12 Sep 2019 11:16:03 +0200 Subject: [PATCH] Initial set-up for 2019 AoC. --- 2019/.gitignore | 1 + 2019/CMakeLists.txt | 10 ++++++++++ 2019/src/day01.cpp | 35 +++++++++++++++++++++++++++++++++++ 2019/src/runner.cpp | 34 ++++++++++++++++++++++++++++++++++ 2019/src/solutions.cpp | 10 ++++++++++ 2019/src/solutions.hpp | 13 +++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 2019/.gitignore create mode 100644 2019/CMakeLists.txt create mode 100644 2019/src/day01.cpp create mode 100644 2019/src/runner.cpp create mode 100644 2019/src/solutions.cpp create mode 100644 2019/src/solutions.hpp diff --git a/2019/.gitignore b/2019/.gitignore new file mode 100644 index 0000000..8eee68f --- /dev/null +++ b/2019/.gitignore @@ -0,0 +1 @@ +cmake-build-* diff --git a/2019/CMakeLists.txt b/2019/CMakeLists.txt new file mode 100644 index 0000000..d321718 --- /dev/null +++ b/2019/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.15) + +project(aoc2019) + +add_library(AoCSolutions src/solutions.hpp src/solutions.cpp src/day01.cpp) +target_compile_features(AoCSolutions PUBLIC cxx_std_17) + +add_executable(runner src/runner.cpp) +target_compile_features(runner PUBLIC cxx_std_17) +target_link_libraries(runner AoCSolutions) diff --git a/2019/src/day01.cpp b/2019/src/day01.cpp new file mode 100644 index 0000000..af57e58 --- /dev/null +++ b/2019/src/day01.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include "solutions.hpp" + +// Currently an implementation of 2018 day 1 +void aoc2019::day01_part1(std::istream &input, std::ostream &output) { + int sum = std::accumulate(std::istream_iterator(input), + std::istream_iterator(), + 0); + + output << sum << std::endl; +} + +void aoc2019::day01_part2(std::istream &input, std::ostream &output) { + std::vector drifts; + std::copy(std::istream_iterator(input), + std::istream_iterator(), + std::back_inserter(drifts)); + + int cur = 0; + std::unordered_set seen{cur}; + while (true) { + for (auto drift : drifts) { + cur += drift; + if (seen.count(cur) == 1) { + output << drift << std::endl; + return; + } else { + seen.insert(cur); + } + } + } +} diff --git a/2019/src/runner.cpp b/2019/src/runner.cpp new file mode 100644 index 0000000..30543fd --- /dev/null +++ b/2019/src/runner.cpp @@ -0,0 +1,34 @@ +#include "solutions.hpp" +#include +#include +#include + +template +Int int_from_cstr(Int &value, const char *begin) { + const char *end = begin + std::strlen(begin); + auto result = std::from_chars(begin, end, value, Radix); + if (result.ec != std::errc()) { + throw std::invalid_argument("Unparseable integer"); + } + + return value; +} + +int main(int argc, const char *argv[]) { + if (argc < 2) { + std::cerr << "Specify a day to run.\n"; + return 1; + } + + int day; + int_from_cstr(day, argv[1]); + + const aoc2019::solution_t solution = aoc2019::get_implementation(day); + if (solution != nullptr) { + solution(std::cin, std::cout); + return 0; + } else { + std::cerr << "Unimplemented.\n"; + return 1; + } +} diff --git a/2019/src/solutions.cpp b/2019/src/solutions.cpp new file mode 100644 index 0000000..bb8fd0d --- /dev/null +++ b/2019/src/solutions.cpp @@ -0,0 +1,10 @@ +#include +#include "solutions.hpp" + +constexpr const std::array, 25> SOLUTIONS = { + {aoc2019::day01_part1, aoc2019::day01_part2} +}; + +aoc2019::solution_t aoc2019::get_implementation(int day, bool part2) { + return SOLUTIONS.at(day - 1).at((int) part2); +} diff --git a/2019/src/solutions.hpp b/2019/src/solutions.hpp new file mode 100644 index 0000000..7ac9747 --- /dev/null +++ b/2019/src/solutions.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace aoc2019 { + typedef void (*solution_t)(std::istream &, std::ostream &); + + solution_t get_implementation(int day, bool part2 = false); + + // Declarations of all implemented days. + void day01_part1(std::istream &input, std::ostream &output); + void day01_part2(std::istream &input, std::ostream &output); +}