Added 2022 (cpp)
This commit is contained in:
33
2022/cpp/template/Makefile
Normal file
33
2022/cpp/template/Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
PART := 1
|
||||
CXXFLAGS := -Wall -O3 -DPART=$(PART)
|
||||
OBJ_DIR = build
|
||||
OBJ = answer.o
|
||||
|
||||
.PHONY = clean asdf sample1 sample2
|
||||
|
||||
asdf: result
|
||||
@echo "------- RESULT -------"
|
||||
@./result "input" | xclip -selection c
|
||||
@xclip -selection c -o
|
||||
|
||||
check: test
|
||||
./test
|
||||
|
||||
test: $(OBJ_DIR)/$(OBJ)
|
||||
|
||||
verbose: clean
|
||||
$(eval CXXFLAGS += -DVERBOSE)
|
||||
|
||||
$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp
|
||||
|
||||
$(OBJ_DIR)/%.o: %.cpp
|
||||
@echo "Compiling $<..."
|
||||
@mkdir -p $(@D)
|
||||
gcc $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
result: $(OBJ_DIR)/$(OBJ)
|
||||
|
||||
clean:
|
||||
-rm -f *.o
|
||||
-rm -rf $(OBJ_DIR)/
|
||||
|
22
2022/cpp/template/answer.cpp
Normal file
22
2022/cpp/template/answer.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
// AOC - 2022 - DATE
|
||||
#include "answer.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
Input get_input(const char* filename) {
|
||||
std::ifstream stream(filename);
|
||||
std::string str;
|
||||
Input ret;
|
||||
|
||||
while (getline(stream, str)) {}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string get_result(Input& input) {
|
||||
std::string ret;
|
||||
#if PART == 1
|
||||
#else
|
||||
#endif
|
||||
return ret;
|
||||
}
|
28
2022/cpp/template/answer.hpp
Normal file
28
2022/cpp/template/answer.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "lib.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#ifndef VERBOSE
|
||||
#define VERBOSE 0
|
||||
#endif
|
||||
|
||||
#ifndef PART
|
||||
#define PART 1
|
||||
#endif
|
||||
|
||||
struct A {
|
||||
bool operator==(const A& rhs) const {
|
||||
return this->== rhs.;
|
||||
}
|
||||
};
|
||||
|
||||
typedef A Input;
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, Input& inp) {
|
||||
os << inp.;
|
||||
return os;
|
||||
}
|
||||
|
||||
Input get_input(const char*);
|
||||
|
||||
std::string get_result(Input&);
|
23
2022/cpp/template/lib.hpp
Normal file
23
2022/cpp/template/lib.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<T>& list) {
|
||||
unsigned long i = 0;
|
||||
os << "[";
|
||||
for (auto el : list) {
|
||||
os << el;
|
||||
if (i < list.size() - 1)
|
||||
os << ", ";
|
||||
i++;
|
||||
}
|
||||
os << "]";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
inline void error(const char* msg) {
|
||||
std::cerr << msg << std::endl;
|
||||
exit(1);
|
||||
}
|
23
2022/cpp/template/result.cpp
Normal file
23
2022/cpp/template/result.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "answer.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2)
|
||||
error("Please specify the input file as first argument.");
|
||||
|
||||
#if !VERBOSE
|
||||
std::cout.setstate(std::ios_base::failbit);
|
||||
#endif
|
||||
|
||||
Input input = get_input(argv[1]);
|
||||
std::string result = get_result(input);
|
||||
|
||||
#if !VERBOSE
|
||||
std::cout.clear();
|
||||
#endif
|
||||
|
||||
std::cout << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
33
2022/cpp/template/test.cpp
Normal file
33
2022/cpp/template/test.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "../doctest/doctest/doctest.h"
|
||||
#include "answer.hpp"
|
||||
|
||||
#if PART == 1
|
||||
TEST_CASE("Part 1") {
|
||||
Input actual_input = get_input("sample1");
|
||||
|
||||
Input expected_input;
|
||||
SUBCASE("Testing input is parsed correctly") {
|
||||
CHECK(actual_input == expected_input);
|
||||
}
|
||||
|
||||
SUBCASE("Testing output is the one expected from AOC") {
|
||||
CHECK(get_result(actual_input) == "SAMPLE1_OUT");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PART == 2
|
||||
TEST_CASE("Part 2") {
|
||||
Input actual_input = get_input("sample2");
|
||||
|
||||
Input expected_input;
|
||||
SUBCASE("Testing input is parsed correctly") {
|
||||
CHECK(actual_input == expected_input);
|
||||
}
|
||||
|
||||
SUBCASE("Testing output is the one expected from AOC") {
|
||||
CHECK(get_result(actual_input) == "SAMPLE2_OUT");
|
||||
}
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user