Added 2022 (cpp)

This commit is contained in:
Karma Riuk
2023-08-02 11:50:10 +02:00
parent 11600f7ba9
commit 567c8487b5
106 changed files with 10153 additions and 0 deletions

36
2022/cpp/02/Makefile Normal file
View File

@ -0,0 +1,36 @@
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)
# sample1 sample2: $(OBJ_DIR)/$(OBJ)
# $(eval CXXFLAGS += -DSAMPLE$(@:sample%=%))
$(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)/

74
2022/cpp/02/answer.cpp Normal file
View File

@ -0,0 +1,74 @@
// AOC - 2022 - 02
#include "answer.hpp"
#include <iostream>
#include <fstream>
Move get_move(const char & move){
if (move == 'A' || move == 'X')
return Move::ROCK;
if (move == 'B' || move == 'Y')
return Move::PAPER;
if (move == 'C' || move == 'Z')
return Move::SCISSORS;
error("Move not valid");
}
std::vector<Input> get_input(const char * filename){
std::ifstream stream(filename);
std::string str;
std::vector<Input> ret;
while (getline(stream, str)){
#if PART == 1
ret.push_back({
.my = get_move(str[2]),
.opponent = get_move(str[0])
});
#else
Move opponent = get_move(str[0]);
Move my;
switch (str[2]) {
case 'X': // lose
my = static_cast<Move>((opponent - 1 + 3) % 3);
break;
case 'Y': // draw
my = opponent;
break;
case 'Z': // win
my = static_cast<Move>((opponent + 1) % 3);
break;
default:
error("Part 2 move not valid");
}
ret.push_back({
.my = my,
.opponent = opponent
});
#endif
}
return ret;
}
std::string get_result(std::vector<Input> input){
std::string ret;
uint score = 0;
for (auto round : input){
std::cout << "before: " << score << std::endl;
score += round.my + 1;
if (round.my == round.opponent){ // draw
score += 3;
continue;
}
std::cout << round.my << " " << round.opponent << std::endl;
if (round.my == (round.opponent + 1) % 3){ // win
std::cout << "win" << std::endl;
score += 6;
}
std::cout << "after: " << score << std::endl;
}
ret = std::to_string(score);
return ret;
}

36
2022/cpp/02/answer.hpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <string>
#include "lib.hpp"
enum Move {
ROCK,
PAPER,
SCISSORS
};
struct Input{
Move my, opponent;
bool operator ==(const Input & rhs) const{
return this->my == rhs.my && this->opponent == rhs.opponent;
}
};
inline std::ostream& operator<<(std::ostream &os, Input & inp){
os << "my: " << inp.my << ", opponent: " << inp.opponent;
return os;
}
#ifndef VERBOSE
#define VERBOSE 0
#endif
std::vector<Input> get_input(const char *);
std::string get_result(std::vector<Input>);
inline void error(const char * msg){
std::cerr << msg << std::endl;
exit(1);
}

BIN
2022/cpp/02/build/answer.o Normal file

Binary file not shown.

2500
2022/cpp/02/input Normal file

File diff suppressed because it is too large Load Diff

18
2022/cpp/02/lib.hpp Normal file
View File

@ -0,0 +1,18 @@
#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;
}

BIN
2022/cpp/02/result Executable file

Binary file not shown.

22
2022/cpp/02/result.cpp Normal file
View File

@ -0,0 +1,22 @@
#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
std::vector<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;
}

3
2022/cpp/02/sample1 Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

3
2022/cpp/02/sample2 Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

BIN
2022/cpp/02/test Executable file

Binary file not shown.

41
2022/cpp/02/test.cpp Normal file
View File

@ -0,0 +1,41 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "../doctest/doctest/doctest.h"
#include "answer.hpp"
#if PART == 1
TEST_CASE("Part 1"){
std::vector<Input> expected_input;
expected_input.push_back({.my = Move::PAPER, .opponent = Move::ROCK});
expected_input.push_back({.my = Move::ROCK, .opponent = Move::PAPER});
expected_input.push_back({.my = Move::SCISSORS, .opponent = Move::SCISSORS});
std::vector<Input> actual_input = get_input("sample1");
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) == "15");
}
}
#endif
#if PART == 2
TEST_CASE("Part 2"){
std::vector<Input> expected_input;
expected_input.push_back({.my = Move::ROCK, .opponent = Move::ROCK});
expected_input.push_back({.my = Move::ROCK, .opponent = Move::PAPER});
expected_input.push_back({.my = Move::ROCK, .opponent = Move::SCISSORS});
std::vector<Input> actual_input = get_input("sample2");
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) == "12");
}
}
#endif