diff --git a/c++/main.cpp b/c++/main.cpp index eee56c7..56ddf53 100644 --- a/c++/main.cpp +++ b/c++/main.cpp @@ -1,21 +1,55 @@ #include #include +#include #include - +#include +#include +#include #include "solutions.hpp" using namespace std; +typedef std::pair(*solution_t)(std::istream&); +template void init_solutions(std::array &solutions); + +template +pair solve(istream&) { + stringstream s; + s << "solution " << N << " not implemented"; + throw logic_error(s.str()); +} + +template +constexpr void do_init(solution_t *solutions) { + solutions[I-1] = solve; + do_init(solutions); +} + +template <> +constexpr void do_init<1>(solution_t *solutions) { + solutions[0] = solve<1>; +} + +template +void init_solutions(array &solutions) { + do_init(solutions.data()); +} + int main(int argc, char *argv[]) { + array(*)(istream&), 25> solutions; + init_solutions(solutions); if (argc < 2) { return 0; } int problem_number = stoi(string(argv[1])); - solution_fn solutions[] = {solve1, solve2, solve3, solve4}; - auto x = solutions[problem_number-1]; + try { + auto x = solutions.at(problem_number-1); auto answer = x(cin); cout << "Solution 1: " << answer.first << endl; cout << "Solution 2: " << answer.second << endl; + } catch (exception const &e) { + cerr << "error: " << e.what() << endl; + } return 0; } diff --git a/c++/problem1.cpp b/c++/problem1.cpp index 8dfd6f5..adad122 100644 --- a/c++/problem1.cpp +++ b/c++/problem1.cpp @@ -12,7 +12,8 @@ using namespace std; -pair solve1(istream &input) { +template <> +pair solve<1>(istream &input) { unordered_set seen; unordered_map> targets; string part1; diff --git a/c++/problem2.cpp b/c++/problem2.cpp index 0a354a4..05d5575 100644 --- a/c++/problem2.cpp +++ b/c++/problem2.cpp @@ -5,7 +5,8 @@ using namespace std; -pair solve2(istream& input) { +template <> +pair solve<2>(istream& input) { uint64_t part1 = 0, part2 = 0; for (string line; getline(input, line); ) { stringstream line_stream(line); diff --git a/c++/problem3.cpp b/c++/problem3.cpp index c57f972..dd23597 100644 --- a/c++/problem3.cpp +++ b/c++/problem3.cpp @@ -6,7 +6,8 @@ using namespace std; -pair solve3(istream& input) { +template<> +pair solve<3>(istream& input) { vector my_map; uint64_t width; bool width_set = false; diff --git a/c++/problem4.cpp b/c++/problem4.cpp index 0b58897..cbb6229 100644 --- a/c++/problem4.cpp +++ b/c++/problem4.cpp @@ -18,7 +18,8 @@ bool is_hex_digit(char c) { return isdigit((int) c) || (c >= 'a' && c <= 'f'); } -pair solve4(istream& input) { +template <> +pair solve<4>(istream& input) { cerr << boolalpha; stringstream current_passport{string()}; uint64_t part1 = 0, part2 = 0; diff --git a/c++/solutions.hpp b/c++/solutions.hpp index 26b03b8..bf7ad36 100644 --- a/c++/solutions.hpp +++ b/c++/solutions.hpp @@ -3,11 +3,6 @@ #include #include #include +#include -typedef std::pair (*solution_fn)(std::istream&); -#define DECL_SOLUTION(x) std::pair solve##x(std::istream&); - -DECL_SOLUTION(1) -DECL_SOLUTION(2) -DECL_SOLUTION(3) -DECL_SOLUTION(4) +template std::pair solve(std::istream&);