Browse Source

Templates for solving lol

trunk
Shanti Chellaram 4 years ago
parent
commit
ef5b1bb427
  1. 40
      c++/main.cpp
  2. 3
      c++/problem1.cpp
  3. 3
      c++/problem2.cpp
  4. 3
      c++/problem3.cpp
  5. 3
      c++/problem4.cpp
  6. 9
      c++/solutions.hpp

40
c++/main.cpp

@ -1,21 +1,55 @@
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <sstream>
#include <string> #include <string>
#include <array>
#include <stdexcept>
#include <functional>
#include "solutions.hpp" #include "solutions.hpp"
using namespace std; using namespace std;
typedef std::pair<std::string, std::string>(*solution_t)(std::istream&);
template <unsigned long N> void init_solutions(std::array<solution_t, N> &solutions);
template <unsigned int N>
pair<string, string> solve(istream&) {
stringstream s;
s << "solution " << N << " not implemented";
throw logic_error(s.str());
}
template <unsigned long I>
constexpr void do_init(solution_t *solutions) {
solutions[I-1] = solve<I>;
do_init<I-1>(solutions);
}
template <>
constexpr void do_init<1>(solution_t *solutions) {
solutions[0] = solve<1>;
}
template <unsigned long N>
void init_solutions(array<solution_t, N> &solutions) {
do_init<N>(solutions.data());
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
array<pair<string,string>(*)(istream&), 25> solutions;
init_solutions(solutions);
if (argc < 2) { if (argc < 2) {
return 0; return 0;
} }
int problem_number = stoi(string(argv[1])); 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); auto answer = x(cin);
cout << "Solution 1: " << answer.first << endl; cout << "Solution 1: " << answer.first << endl;
cout << "Solution 2: " << answer.second << endl; cout << "Solution 2: " << answer.second << endl;
} catch (exception const &e) {
cerr << "error: " << e.what() << endl;
}
return 0; return 0;
} }

3
c++/problem1.cpp

@ -12,7 +12,8 @@
using namespace std; using namespace std;
pair<string, string> solve1(istream &input) {
template <>
pair<string, string> solve<1>(istream &input) {
unordered_set<int64_t> seen; unordered_set<int64_t> seen;
unordered_map<int64_t, pair<int64_t, int64_t>> targets; unordered_map<int64_t, pair<int64_t, int64_t>> targets;
string part1; string part1;

3
c++/problem2.cpp

@ -5,7 +5,8 @@
using namespace std; using namespace std;
pair<string, string> solve2(istream& input) {
template <>
pair<string, string> solve<2>(istream& input) {
uint64_t part1 = 0, part2 = 0; uint64_t part1 = 0, part2 = 0;
for (string line; getline(input, line); ) { for (string line; getline(input, line); ) {
stringstream line_stream(line); stringstream line_stream(line);

3
c++/problem3.cpp

@ -6,7 +6,8 @@
using namespace std; using namespace std;
pair<string, string> solve3(istream& input) {
template<>
pair<string, string> solve<3>(istream& input) {
vector<string> my_map; vector<string> my_map;
uint64_t width; uint64_t width;
bool width_set = false; bool width_set = false;

3
c++/problem4.cpp

@ -18,7 +18,8 @@ bool is_hex_digit(char c) {
return isdigit((int) c) || (c >= 'a' && c <= 'f'); return isdigit((int) c) || (c >= 'a' && c <= 'f');
} }
pair<string, string> solve4(istream& input) {
template <>
pair<string, string> solve<4>(istream& input) {
cerr << boolalpha; cerr << boolalpha;
stringstream current_passport{string()}; stringstream current_passport{string()};
uint64_t part1 = 0, part2 = 0; uint64_t part1 = 0, part2 = 0;

9
c++/solutions.hpp

@ -3,11 +3,6 @@
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <string> #include <string>
#include <array>
typedef std::pair<std::string, std::string> (*solution_fn)(std::istream&);
#define DECL_SOLUTION(x) std::pair<std::string, std::string> solve##x(std::istream&);
DECL_SOLUTION(1)
DECL_SOLUTION(2)
DECL_SOLUTION(3)
DECL_SOLUTION(4)
template <unsigned int N> std::pair<std::string, std::string> solve(std::istream&);
Loading…
Cancel
Save