mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Initial set-up for 2019 AoC.
This commit is contained in:
1
2019/.gitignore
vendored
Normal file
1
2019/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
cmake-build-*
|
||||
10
2019/CMakeLists.txt
Normal file
10
2019/CMakeLists.txt
Normal file
@@ -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)
|
||||
35
2019/src/day01.cpp
Normal file
35
2019/src/day01.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <numeric>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#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<int>(input),
|
||||
std::istream_iterator<int>(),
|
||||
0);
|
||||
|
||||
output << sum << std::endl;
|
||||
}
|
||||
|
||||
void aoc2019::day01_part2(std::istream &input, std::ostream &output) {
|
||||
std::vector<int> drifts;
|
||||
std::copy(std::istream_iterator<int>(input),
|
||||
std::istream_iterator<int>(),
|
||||
std::back_inserter(drifts));
|
||||
|
||||
int cur = 0;
|
||||
std::unordered_set<int> seen{cur};
|
||||
while (true) {
|
||||
for (auto drift : drifts) {
|
||||
cur += drift;
|
||||
if (seen.count(cur) == 1) {
|
||||
output << drift << std::endl;
|
||||
return;
|
||||
} else {
|
||||
seen.insert(cur);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
2019/src/runner.cpp
Normal file
34
2019/src/runner.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "solutions.hpp"
|
||||
#include <iostream>
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
|
||||
template<typename Int, int Radix = 10>
|
||||
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;
|
||||
}
|
||||
}
|
||||
10
2019/src/solutions.cpp
Normal file
10
2019/src/solutions.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <array>
|
||||
#include "solutions.hpp"
|
||||
|
||||
constexpr const std::array<std::array<aoc2019::solution_t, 2>, 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);
|
||||
}
|
||||
13
2019/src/solutions.hpp
Normal file
13
2019/src/solutions.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user