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

33
2022/cpp/04/Makefile Normal file
View 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)/

52
2022/cpp/04/answer.cpp Normal file
View File

@ -0,0 +1,52 @@
// AOC - 2022 - 04
#include "answer.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
Range get_range(std::stringstream & ss) {
uint start, end;
char _;
ss >> start;
ss >> _;
ss >> end;
ss >> _;
return {start, end};
}
std::vector<Input> get_input(const char * filename) {
std::ifstream stream(filename);
std::string str;
std::vector<Input> ret;
while (getline(stream, str)) {
std::stringstream ss(str);
ret.push_back({get_range(ss), get_range(ss)});
}
return ret;
}
bool Range::fully_contains(const Range & rhs) const {
return this->start <= rhs.start && this->end >= rhs.end;
}
bool Range::is_disjoint_from(const Range & rhs) const {
return this->start < rhs.start && this->end < rhs.start;
}
bool Range::overlaps(const Range & rhs) const {
return !this->is_disjoint_from(rhs) && !rhs.is_disjoint_from(*this);
}
std::string get_result(std::vector<Input> input) {
std::string ret;
uint count = 0;
for (auto & ranges : input)
#if PART == 1
count += ranges.r1.fully_contains(ranges.r2) || ranges.r2.fully_contains(ranges.r1);
#else
count += ranges.r1.overlaps(ranges.r2);
#endif
ret = std::to_string(count);
return ret;
}

45
2022/cpp/04/answer.hpp Normal file
View File

@ -0,0 +1,45 @@
#include <string>
#include "lib.hpp"
#ifndef VERBOSE
#define VERBOSE 0
#endif
#ifndef PART
#define PART 1
#endif
struct Range{
uint start, end;
bool operator ==(const Range & rhs) const{
return this->start == rhs.start && this->end == rhs.end;
}
bool fully_contains(const Range & rhs) const;
bool overlaps(const Range & rhs) const;
bool is_disjoint_from(const Range & rhs) const;
};
struct Input{
Range r1, r2;
bool operator ==(const Input & rhs) const{
return this->r1 == rhs.r1 && this->r2 == rhs.r2;
}
};
inline std::ostream& operator<<(std::ostream &os, Range & range){
os << "[" << range.start << ", " << range.end << "]";
return os;
}
inline std::ostream& operator<<(std::ostream &os, Input & inp){
os << inp.r1 << " & " << inp.r2;
return os;
}
std::vector<Input> get_input(const char *);
std::string get_result(std::vector<Input>);

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

Binary file not shown.

1000
2022/cpp/04/input Normal file

File diff suppressed because it is too large Load Diff

24
2022/cpp/04/lib.hpp Normal file
View File

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

BIN
2022/cpp/04/result Executable file

Binary file not shown.

23
2022/cpp/04/result.cpp Normal file
View 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
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;
}

6
2022/cpp/04/sample1 Normal file
View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

BIN
2022/cpp/04/test Executable file

Binary file not shown.

35
2022/cpp/04/test.cpp Normal file
View File

@ -0,0 +1,35 @@
#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.insert(expected_input.end(), {
{{2, 4}, {6, 8}},
{{2, 3}, {4, 5}},
{{5, 7}, {7, 9}},
{{2, 8}, {3, 7}},
{{6, 6}, {4, 6}},
{{2, 6}, {4, 8}},
});
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) == "2");
}
}
#endif
#if PART == 2
TEST_CASE("Part 2"){
std::vector<Input> actual_input = get_input("sample1");
SUBCASE("Testing output is the one expected from AOC"){
CHECK(get_result(actual_input) == "4");
}
}
#endif