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

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "2022/2022/cpp/doctest"]
path = 2022/cpp/doctest
url = git@github.com:doctest/doctest

35
2022/cpp/01/Makefile Normal file
View File

@ -0,0 +1,35 @@
# VERBOSE := 0
CXXFLAGS := -Wall -O3 -DPART=2
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
verbose: clean
$(eval CXXFLAGS += -DVERBOSE)
test: $(OBJ_DIR)/$(OBJ)
sample1 sample2:
$(eval CXXFLAGS += -DSAMPLE$(@:sample%=%))
@-rm test
$(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)/

68
2022/cpp/01/answer.cpp Normal file
View File

@ -0,0 +1,68 @@
// AOC - 2022 - 01
#include "answer.hpp"
#include <cstdlib>
#include <iostream>
#include <fstream>
std::vector<Input> get_input(const char * filename){
std::ifstream stream(filename);
std::string str;
std::vector<Input> ret;
ret.push_back(*new Input());
while (getline(stream, str)){
if (str.length() == 0)
ret.push_back(*new Input());
else
ret.back().calories.push_back(stoi(str));
}
return ret;
}
std::string get_result(std::vector<Input> inputs){
std::string ret;
#if PART==1
uint max_sum = 0;
for (auto& inp: inputs){
uint local_sum = 0;
for (auto& n : inp.calories)
local_sum += n;
std::cout << local_sum << std::endl;
if (local_sum > max_sum)
max_sum = local_sum;
}
ret = std::to_string(max_sum);
std::cout << ret << std::endl;
#else
std::vector<uint> top3(3);
for (int i = 0; i < 3; i++){
uint max_sum = 0;
size_t max_idx = -1;
size_t idx = 0;
for (auto& inp: inputs){
uint local_sum = 0;
for (auto& n : inp.calories)
local_sum += n;
std::cout << local_sum << std::endl;
if (local_sum > max_sum) {
max_sum = local_sum;
max_idx = idx;
}
idx++;
}
top3.push_back(max_sum);
inputs.erase(inputs.begin() + max_idx);
}
uint sum = 0;
for (auto& n : top3)
sum += n;
ret = std::to_string(sum);
std::cout << ret << std::endl;
#endif
return ret;
}

41
2022/cpp/01/answer.hpp Normal file
View File

@ -0,0 +1,41 @@
#include <string>
#include <vector>
#include <ostream>
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;
}
struct Input{
std::vector<uint> calories;
bool operator ==(const Input & rhs) const{
return this->calories == rhs.calories;
};
};
inline std::ostream& operator<<(std::ostream &os, Input & inp){
os << inp.calories;
return os;
}
#ifndef VERBOSE
#define VERBOSE 0
#endif
std::vector<Input> get_input(const char *);
std::string get_result(std::vector<Input>);

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

Binary file not shown.

2251
2022/cpp/01/input Normal file

File diff suppressed because it is too large Load Diff

BIN
2022/cpp/01/result Executable file

Binary file not shown.

28
2022/cpp/01/result.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "answer.hpp"
#include <iostream>
void error(const char * msg){
std::cerr << msg << std::endl;
exit(1);
}
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;
}

14
2022/cpp/01/sample1 Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

14
2022/cpp/01/sample2 Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

BIN
2022/cpp/01/test Executable file

Binary file not shown.

65
2022/cpp/01/test.cpp Normal file
View File

@ -0,0 +1,65 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "answer.hpp"
#include "../doctest/doctest/doctest.h"
#ifdef SAMPLE1
TEST_CASE("Part 1"){
std::vector<Input> expected_input;
Input elf1;
elf1.calories.insert(elf1.calories.end(), {
1000,
2000,
3000,
});
Input elf2;
elf2.calories.insert(elf2.calories.end(), {
4000,
});
Input elf3;
elf3.calories.insert(elf3.calories.end(), {
5000,
6000,
});
Input elf4;
elf4.calories.insert(elf4.calories.end(), {
7000,
8000,
9000,
});
Input elf5;
elf5.calories.insert(elf5.calories.end(), {
10000
});
expected_input.insert(expected_input.end(), {
elf1, elf2, elf3, elf4, elf5
});
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) == "24000");
}
}
#endif
#ifdef SAMPLE2
TEST_CASE("Part 2"){
std::vector<Input> actual_input = get_input("sample2");
SUBCASE("Testing output is the one expected from AOC"){
CHECK(get_result(actual_input) == "45000");
}
}
#endif

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

33
2022/cpp/03/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)/

77
2022/cpp/03/answer.cpp Normal file
View File

@ -0,0 +1,77 @@
// AOC - 2022 - 03
#include "answer.hpp"
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
std::vector<Input> get_input(const char *filename) {
std::ifstream stream(filename);
std::string str;
std::vector<Input> ret;
while (getline(stream, str))
ret.push_back({.content = str});
return ret;
}
int get_priority(const char &c) {
return std::islower(c) ? 1 + (c - 'a') : 27 + (c - 'A');
}
std::string get_result(std::vector<Input> input) {
std::string ret;
int score = 0;
#if PART == 1
// filter through the first half to find common elements
for (auto &sacks : input) {
size_t size = sacks.content.size();
if (size % 2 == 1)
error("sack does not have even number of elements");
std::string sack1 = sacks.content.substr(0, size / 2);
std::string sack2 = sacks.content.substr(size / 2);
std::unordered_set<char> common_items;
std::copy_if(
sack1.begin(), sack1.end(),
std::inserter(common_items, common_items.end()),
[sack2](char c) { return sack2.find(c) != std::string::npos; });
std::cout << sacks << std::endl;
std::cout << common_items << std::endl;
// get priority of items found
for (auto &item : common_items)
score += get_priority(item);
std::cout << "score: " << score << std::endl;
}
#else
std::vector<std::vector<std::string>> groups;
for (size_t i = 0; i < input.size(); i += 3) {
groups.insert(groups.end(), {input[i + 0].content, input[i + 1].content,
input[i + 2].content});
}
for (auto &group : groups) {
std::cout << group << std::endl;
std::unordered_set<char> common_items;
std::copy_if(group[0].begin(), group[0].end(),
std::inserter(common_items, common_items.end()),
[group](char c) {
return group[1].find(c) != std::string::npos &&
group[2].find(c) != std::string::npos;
});
// get priority of items found
for (auto &item : common_items)
score += get_priority(item);
std::cout << "score: " << score << std::endl;
}
#endif
ret = std::to_string(score);
return ret;
}

33
2022/cpp/03/answer.hpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <string>
#include "lib.hpp"
#ifndef PART
#define PART 2
#endif
#ifndef VERBOSE
#define VERBOSE 0
#endif
struct Input{
std::string content;
bool operator ==(const Input & rhs) const{
return this->content == rhs.content;
}
};
inline std::ostream& operator<<(std::ostream &os, Input & inp){
os << inp.content;
return os;
}
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/03/build/answer.o Normal file

Binary file not shown.

300
2022/cpp/03/input Normal file
View File

@ -0,0 +1,300 @@
wgqJtbJMqZVTwWPZZT
LHcTGHQhzrTzBsZFPHFZWFFs
RnLRClzGzRGLGLGCNRjTMjJfgmffSffMqNgp
WPLgsfLmLgqZvZgSRR
RbwHdbDdQFFFMvvMjbhqhZZS
lzTdldBDszfGcRsr
ZjnhJjMjnbdnbHdFLmmfFLmnCCWFFl
PpNwtRsNsZSsRwCfzQQBfQszCBsC
PpwcqqVZRtbggggjcgJJ
ntczBcVcgnHzgBHnVntcBBFhgsmmmssqWNWNWqLvNhsqTN
bSSGdSDZbGSGdDmLmGTvTGmLFFhm
PlFbDpJDPbP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
zTJpqFzbTzsWsVbbfLGfSSCDNSBCHfMLHG
rZcvtmhctrvmlPPmmmrhhmBLCHDCCLLDlqMlGMNDMwDC
rRhRhnnQPZhtZcZtdttZgqFWWVjssqQpppWpFpJW
NWPhdWJPWVzVqQrqmSsPbrPP
cZDRjGsffGsCDfffgjGgRQSrTcTmSlTrbnqmSSrlln
fFGCjGCjLDLFRgfDHZvzLVWtvsWWBtzJNWMB
qMVbtnmMMTpCppsR
NffHGrWzWWgDBfTRhChCnSCWcnjT
QrlQBPBrlPHrrQlrHFLqPnLvVvbVmVVJtq
MVMpHMZLVCpMrfWjvWnfrJ
hlblzDDzwlSlGtRhRlSdrfGGWnWWfFPjJjnfqWGF
lmlhBRlDhhhDRRhwDmBpHJmsNCHmCgNHJCJLsc
jvsLgmqLgHvbPPVbNjSCjC
pwTcRpRWLRMLJJFwBBGWcFWNVlDDCSTVttNPblZZCVVDlP
GcdhccpcpRpGRhGmfsHHzLQQHrmsnh
FMmSRgtMltMnVgnmNvlrsJrsZWjspvsZJp
QbdhqwqbNqdHbTdcbcpsrpvjfWfLJLfwJrWp
DQBBQqQGccdTPGqqBNtFGRSMRSFGtnVSnnmM
fPjGrfFrrprprdrbQPZwlcZwZmlJwH
qvNnvWnvWDvSvqNtWSLWStqbcJBQwQJwQZHLBZbcmJbblb
DMtvqSvvDtntCRfwzGCgdzzFjG
TfdZgtmfDgqgvlLFFsFHvcvZ
pphWQMVjQVVBWWjRlHlHnlcLDDhcnF
JQwwWVPBwMJpJwpWwGBWNzrDzSSzfgTPqTSTTtSPgt

35
2022/cpp/03/lib.hpp Normal file
View File

@ -0,0 +1,35 @@
#include <ostream>
#include <unordered_set>
#include <vector>
template<class T>
std::ostream &operator<<(std::ostream &os, const std::unordered_set<T> &list) {
unsigned long i = 0;
os << "[";
for (auto el : list) {
os << el;
if (i < list.size() - 1) {
os << ", ";
}
i++;
}
os << "]";
return os;
}
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/03/result Executable file

Binary file not shown.

23
2022/cpp/03/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/03/sample1 Normal file
View File

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

6
2022/cpp/03/sample2 Normal file
View File

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

BIN
2022/cpp/03/test Executable file

Binary file not shown.

34
2022/cpp/03/test.cpp Normal file
View File

@ -0,0 +1,34 @@
#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(), {
{.content = "vJrwpWtwJgWrhcsFMMfFFhFp"},
{.content = "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"},
{.content = "PmmdzqPrVvPwwTWBwg"},
{.content = "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn"},
{.content = "ttgJtRGJQctTZtZT"},
{.content = "CrZsJsPPZsGzwwsLwLmpwMDw"},
});
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) == "157");
}
}
#endif
#if PART == 2
TEST_CASE("Part 2") {
std::vector<Input> actual_input = get_input("sample2");
SUBCASE("Testing output is the one expected from AOC") {
CHECK(get_result(actual_input) == "70");
}
}
#endif

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

33
2022/cpp/05/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)/

91
2022/cpp/05/answer.cpp Normal file
View File

@ -0,0 +1,91 @@
// AOC - 2022 - 05
#include "answer.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#define STACK_WIDTH 4
stacks_t get_stacks(std::vector<std::string> stack_lines, uint n_stacks) {
// reverse so that stack_lines[0] is the bottom of the stacks
std::reverse(stack_lines.begin(), stack_lines.end());
std::vector<std::vector<char>> stacks(n_stacks);
uint max_height = stack_lines.size();
for (uint h = 0; h < max_height; h++) {
for (uint i = 0; i < n_stacks; i++) {
char item = stack_lines[h][i * STACK_WIDTH + 1];
if (item != ' ')
stacks[i].push_back(item);
}
}
return stacks;
}
Move get_move(std::string line) {
std::stringstream ss(line);
std::string _;
uint item, from, to;
ss >> _; // move
ss >> item;
ss >> _; // from
ss >> from;
ss >> _; // to
ss >> to;
return {--item, --from, --to};
}
Input get_input(const char * filename) {
std::ifstream stream(filename);
std::string str;
Input ret;
std::vector<std::string> stack_lines;
stacks_t stacks;
while (getline(stream, str) && !std::isdigit(str[1])) {
stack_lines.push_back(str);
}
std::reverse(str.begin(), str.end() - 1);
std::stringstream ss(str);
uint n_stacks;
ss >> n_stacks;
stacks = get_stacks(stack_lines, n_stacks);
getline(stream, str); // get rid of the empty line between the stacks and
// the move instructions
std::vector<Move> moves;
while (getline(stream, str))
moves.push_back(get_move(str));
return {stacks, moves};
}
template <class T> std::vector<T> pop(std::vector<T> & v, uint i) {
std::vector<T> ret(v.begin() + i, v.end());
v.erase(v.begin() + i, v.end());
return ret;
}
std::string get_result(Input input) {
std::string ret;
stacks_t stacks = input.stacks;
std::cout << stacks << std::endl;
for (auto & move : input.moves) {
std::cout << move << std::endl;
std::vector<char> tbi = pop(stacks[move.from], stacks[move.from].size() - 1 - move.item);
#if PART == 1
std::reverse(tbi.begin(), tbi.end());
#endif
stacks[move.to].insert(stacks[move.to].end(),
tbi.begin(), tbi.end());
std::cout << stacks << std::endl << std::endl;
}
for (auto & stack : stacks)
ret += stack.back();
return ret;
}

61
2022/cpp/05/answer.hpp Normal file
View File

@ -0,0 +1,61 @@
#include <stack>
#include <string>
#include "lib.hpp"
#ifndef VERBOSE
#define VERBOSE 0
#endif
#ifndef PART
#define PART 2
#endif
struct Move{
uint item, from, to;
bool operator ==(const Move & rhs) const{
return this->item == rhs.item && this->from == rhs.from && this->to == rhs.to;
}
};
typedef std::vector<std::vector<char>> stacks_t;
struct Input{
stacks_t stacks;
std::vector<Move> moves;
bool operator ==(const Input & rhs) const{
return this->moves == rhs.moves && this->stacks == rhs.stacks;
}
};
inline std::ostream& operator<<(std::ostream &os, stacks_t & stacks){
uint i = 0;
for (auto & stack: stacks){
os << i++ << " ";
for (auto & item : stack)
os << item << " ";
os << std::endl;
}
return os;
}
inline std::ostream& operator<<(std::ostream &os, Move & move){
os << "move " << move.item << " from " << move.from << " to " << move.to;
return os;
}
inline std::ostream& operator<<(std::ostream &os, Input & inp){
os << "---------------------------------" << std::endl;
os << inp.stacks << std::endl;
for (auto & move: inp.moves)
os << move << std::endl;
os << "---------------------------------" << std::endl;
return os;
}
Input get_input(const char *);
std::string get_result(Input);

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

Binary file not shown.

512
2022/cpp/05/input Normal file
View File

@ -0,0 +1,512 @@
[F] [L] [M]
[T] [H] [V] [G] [V]
[N] [T] [D] [R] [N] [D]
[Z] [B] [C] [P] [B] [R] [Z]
[M] [J] [N] [M] [F] [M] [V] [H]
[G] [J] [L] [J] [S] [C] [G] [M] [F]
[H] [W] [V] [P] [W] [H] [H] [N] [N]
[J] [V] [G] [B] [F] [G] [D] [H] [G]
1 2 3 4 5 6 7 8 9
move 6 from 4 to 3
move 5 from 8 to 9
move 1 from 4 to 5
move 1 from 4 to 5
move 2 from 2 to 7
move 2 from 1 to 6
move 9 from 6 to 1
move 12 from 3 to 5
move 1 from 8 to 4
move 3 from 1 to 5
move 1 from 6 to 7
move 10 from 5 to 2
move 14 from 5 to 1
move 8 from 7 to 9
move 11 from 2 to 9
move 1 from 3 to 9
move 11 from 1 to 5
move 2 from 1 to 9
move 1 from 4 to 8
move 6 from 1 to 5
move 1 from 8 to 3
move 16 from 5 to 1
move 4 from 1 to 3
move 1 from 5 to 6
move 4 from 3 to 4
move 1 from 6 to 7
move 21 from 9 to 6
move 2 from 1 to 9
move 2 from 4 to 9
move 5 from 9 to 4
move 9 from 1 to 6
move 6 from 4 to 6
move 1 from 6 to 2
move 1 from 7 to 6
move 1 from 3 to 2
move 8 from 6 to 9
move 3 from 1 to 8
move 1 from 2 to 1
move 13 from 6 to 3
move 1 from 1 to 9
move 2 from 1 to 6
move 3 from 8 to 4
move 4 from 4 to 9
move 3 from 1 to 3
move 22 from 9 to 8
move 1 from 2 to 9
move 6 from 8 to 9
move 15 from 6 to 5
move 5 from 8 to 9
move 11 from 9 to 8
move 13 from 5 to 1
move 1 from 6 to 5
move 1 from 9 to 3
move 21 from 8 to 3
move 3 from 5 to 3
move 11 from 1 to 2
move 25 from 3 to 1
move 5 from 1 to 7
move 20 from 1 to 7
move 1 from 6 to 7
move 16 from 3 to 9
move 8 from 9 to 6
move 1 from 1 to 5
move 5 from 9 to 4
move 2 from 2 to 1
move 2 from 9 to 4
move 1 from 9 to 4
move 1 from 8 to 4
move 1 from 5 to 2
move 3 from 4 to 6
move 1 from 4 to 7
move 9 from 7 to 6
move 5 from 4 to 6
move 7 from 7 to 2
move 1 from 1 to 6
move 11 from 2 to 5
move 10 from 5 to 1
move 1 from 6 to 8
move 1 from 5 to 7
move 24 from 6 to 1
move 12 from 1 to 4
move 12 from 4 to 8
move 2 from 2 to 7
move 3 from 7 to 2
move 5 from 2 to 8
move 9 from 8 to 9
move 9 from 8 to 5
move 1 from 9 to 1
move 14 from 1 to 8
move 11 from 7 to 9
move 4 from 1 to 3
move 7 from 1 to 2
move 3 from 3 to 7
move 12 from 9 to 7
move 8 from 7 to 2
move 4 from 9 to 2
move 1 from 3 to 6
move 5 from 5 to 9
move 14 from 2 to 1
move 8 from 9 to 4
move 6 from 4 to 5
move 5 from 5 to 7
move 1 from 8 to 2
move 2 from 4 to 6
move 4 from 7 to 3
move 10 from 8 to 4
move 2 from 3 to 6
move 7 from 7 to 6
move 10 from 4 to 8
move 5 from 1 to 6
move 8 from 2 to 1
move 7 from 6 to 8
move 9 from 6 to 5
move 16 from 1 to 6
move 2 from 3 to 9
move 1 from 7 to 4
move 2 from 9 to 1
move 14 from 6 to 7
move 1 from 6 to 3
move 2 from 6 to 3
move 9 from 5 to 7
move 3 from 1 to 6
move 3 from 3 to 7
move 5 from 5 to 9
move 3 from 6 to 2
move 1 from 6 to 2
move 12 from 8 to 2
move 5 from 2 to 1
move 2 from 1 to 3
move 25 from 7 to 1
move 1 from 4 to 6
move 2 from 3 to 9
move 26 from 1 to 9
move 2 from 1 to 8
move 1 from 6 to 8
move 1 from 7 to 1
move 7 from 8 to 1
move 7 from 1 to 5
move 1 from 1 to 2
move 2 from 8 to 6
move 32 from 9 to 8
move 1 from 6 to 5
move 5 from 2 to 9
move 1 from 9 to 7
move 24 from 8 to 3
move 1 from 6 to 9
move 3 from 2 to 5
move 1 from 7 to 9
move 4 from 9 to 3
move 8 from 8 to 7
move 18 from 3 to 7
move 20 from 7 to 8
move 6 from 8 to 9
move 6 from 5 to 1
move 8 from 9 to 4
move 3 from 5 to 4
move 8 from 8 to 4
move 2 from 5 to 2
move 3 from 1 to 5
move 4 from 3 to 7
move 6 from 2 to 9
move 3 from 3 to 6
move 6 from 4 to 5
move 2 from 6 to 3
move 1 from 3 to 1
move 4 from 3 to 8
move 8 from 4 to 3
move 4 from 3 to 7
move 4 from 4 to 5
move 4 from 9 to 5
move 3 from 3 to 4
move 3 from 4 to 9
move 1 from 1 to 4
move 2 from 1 to 5
move 7 from 7 to 8
move 4 from 7 to 4
move 1 from 6 to 7
move 1 from 1 to 5
move 1 from 3 to 8
move 11 from 5 to 9
move 17 from 9 to 8
move 13 from 8 to 4
move 1 from 4 to 8
move 4 from 7 to 1
move 4 from 8 to 3
move 6 from 5 to 4
move 3 from 3 to 6
move 2 from 1 to 9
move 1 from 9 to 5
move 1 from 3 to 5
move 5 from 5 to 9
move 2 from 1 to 8
move 21 from 8 to 6
move 2 from 8 to 4
move 4 from 9 to 6
move 1 from 9 to 7
move 19 from 4 to 1
move 28 from 6 to 5
move 7 from 4 to 2
move 28 from 5 to 3
move 1 from 9 to 4
move 1 from 4 to 2
move 1 from 7 to 8
move 1 from 8 to 9
move 13 from 1 to 3
move 8 from 2 to 8
move 3 from 1 to 2
move 5 from 8 to 5
move 1 from 2 to 7
move 1 from 9 to 7
move 1 from 2 to 3
move 2 from 7 to 9
move 1 from 2 to 6
move 1 from 9 to 1
move 9 from 3 to 9
move 3 from 9 to 1
move 1 from 6 to 8
move 21 from 3 to 7
move 7 from 9 to 4
move 2 from 4 to 2
move 1 from 8 to 6
move 7 from 1 to 4
move 7 from 7 to 8
move 4 from 5 to 9
move 10 from 7 to 1
move 7 from 3 to 9
move 1 from 7 to 9
move 1 from 5 to 3
move 3 from 3 to 5
move 10 from 4 to 2
move 1 from 3 to 7
move 2 from 4 to 9
move 3 from 9 to 1
move 3 from 7 to 1
move 1 from 6 to 4
move 1 from 1 to 2
move 1 from 3 to 4
move 2 from 4 to 3
move 1 from 7 to 4
move 4 from 8 to 9
move 1 from 4 to 9
move 3 from 1 to 9
move 12 from 1 to 7
move 2 from 9 to 5
move 12 from 9 to 7
move 5 from 5 to 1
move 1 from 8 to 5
move 4 from 1 to 4
move 1 from 9 to 6
move 1 from 3 to 4
move 3 from 8 to 3
move 1 from 1 to 7
move 8 from 2 to 5
move 2 from 8 to 1
move 10 from 7 to 1
move 4 from 9 to 5
move 2 from 5 to 8
move 11 from 5 to 4
move 6 from 7 to 2
move 2 from 2 to 1
move 1 from 7 to 5
move 1 from 5 to 1
move 2 from 4 to 8
move 1 from 6 to 9
move 8 from 4 to 3
move 8 from 1 to 7
move 7 from 1 to 2
move 4 from 3 to 9
move 1 from 9 to 6
move 7 from 2 to 1
move 5 from 2 to 3
move 2 from 7 to 8
move 5 from 8 to 4
move 2 from 9 to 3
move 1 from 8 to 1
move 6 from 3 to 5
move 10 from 3 to 1
move 3 from 5 to 3
move 3 from 2 to 1
move 1 from 5 to 4
move 6 from 4 to 5
move 1 from 6 to 2
move 3 from 4 to 7
move 1 from 9 to 4
move 2 from 3 to 1
move 1 from 9 to 8
move 1 from 3 to 7
move 4 from 4 to 8
move 2 from 7 to 4
move 8 from 5 to 9
move 2 from 8 to 6
move 2 from 4 to 3
move 2 from 3 to 4
move 4 from 9 to 7
move 1 from 8 to 7
move 2 from 6 to 9
move 2 from 8 to 9
move 1 from 2 to 9
move 1 from 7 to 8
move 1 from 2 to 7
move 19 from 7 to 6
move 1 from 8 to 1
move 2 from 4 to 8
move 5 from 6 to 1
move 2 from 7 to 2
move 2 from 2 to 8
move 2 from 1 to 8
move 4 from 8 to 2
move 3 from 2 to 8
move 6 from 9 to 5
move 8 from 6 to 3
move 26 from 1 to 6
move 1 from 5 to 3
move 1 from 1 to 5
move 8 from 3 to 1
move 1 from 3 to 7
move 3 from 9 to 2
move 4 from 2 to 6
move 26 from 6 to 1
move 1 from 7 to 5
move 3 from 8 to 4
move 2 from 8 to 2
move 7 from 1 to 2
move 1 from 5 to 9
move 2 from 4 to 6
move 9 from 6 to 2
move 18 from 1 to 7
move 6 from 7 to 1
move 6 from 5 to 6
move 1 from 1 to 2
move 19 from 2 to 7
move 1 from 4 to 2
move 9 from 7 to 1
move 3 from 6 to 7
move 1 from 9 to 4
move 1 from 2 to 3
move 8 from 7 to 8
move 4 from 6 to 5
move 2 from 6 to 3
move 1 from 4 to 2
move 4 from 5 to 1
move 8 from 8 to 7
move 17 from 7 to 8
move 3 from 3 to 1
move 1 from 2 to 8
move 8 from 8 to 4
move 8 from 8 to 7
move 1 from 8 to 2
move 7 from 7 to 6
move 1 from 2 to 7
move 5 from 7 to 8
move 7 from 1 to 6
move 10 from 6 to 1
move 4 from 7 to 9
move 3 from 9 to 7
move 1 from 7 to 2
move 6 from 4 to 2
move 7 from 1 to 5
move 4 from 2 to 5
move 16 from 1 to 9
move 3 from 2 to 7
move 2 from 4 to 9
move 4 from 1 to 6
move 5 from 7 to 4
move 4 from 6 to 3
move 1 from 7 to 4
move 1 from 6 to 9
move 1 from 8 to 5
move 4 from 3 to 2
move 2 from 5 to 3
move 3 from 6 to 2
move 3 from 2 to 1
move 9 from 5 to 8
move 1 from 3 to 1
move 10 from 8 to 1
move 1 from 8 to 5
move 16 from 9 to 2
move 1 from 3 to 2
move 12 from 1 to 9
move 1 from 9 to 2
move 3 from 1 to 6
move 2 from 1 to 9
move 3 from 6 to 8
move 20 from 2 to 7
move 16 from 9 to 7
move 1 from 7 to 5
move 2 from 5 to 9
move 2 from 2 to 3
move 2 from 8 to 5
move 3 from 9 to 7
move 2 from 5 to 2
move 1 from 4 to 6
move 2 from 1 to 4
move 23 from 7 to 5
move 4 from 8 to 5
move 7 from 7 to 1
move 16 from 5 to 7
move 1 from 6 to 5
move 1 from 2 to 4
move 2 from 3 to 9
move 1 from 2 to 3
move 13 from 5 to 1
move 1 from 3 to 8
move 1 from 9 to 4
move 19 from 1 to 9
move 2 from 1 to 9
move 22 from 9 to 8
move 14 from 8 to 5
move 12 from 5 to 3
move 21 from 7 to 9
move 14 from 9 to 7
move 1 from 8 to 6
move 9 from 3 to 7
move 1 from 3 to 2
move 4 from 4 to 1
move 1 from 2 to 4
move 1 from 3 to 9
move 6 from 8 to 9
move 4 from 1 to 7
move 2 from 5 to 9
move 6 from 4 to 5
move 4 from 7 to 4
move 1 from 5 to 3
move 5 from 9 to 7
move 2 from 3 to 1
move 6 from 9 to 6
move 1 from 1 to 6
move 2 from 4 to 2
move 8 from 7 to 5
move 20 from 7 to 5
move 2 from 5 to 6
move 4 from 9 to 5
move 1 from 1 to 3
move 1 from 3 to 4
move 1 from 2 to 7
move 1 from 4 to 9
move 9 from 6 to 3
move 2 from 4 to 3
move 28 from 5 to 3
move 1 from 8 to 3
move 1 from 8 to 1
move 1 from 2 to 8
move 1 from 6 to 2
move 1 from 8 to 1
move 6 from 5 to 7
move 1 from 5 to 1
move 1 from 9 to 2
move 1 from 1 to 3
move 1 from 9 to 7
move 2 from 1 to 2
move 11 from 3 to 8
move 3 from 8 to 6
move 3 from 6 to 9
move 25 from 3 to 7
move 4 from 3 to 8
move 4 from 2 to 3
move 9 from 8 to 9
move 2 from 3 to 7
move 3 from 8 to 2
move 11 from 9 to 7
move 1 from 9 to 1
move 4 from 7 to 3
move 1 from 1 to 5
move 23 from 7 to 2
move 12 from 2 to 3
move 2 from 3 to 9
move 12 from 2 to 1
move 2 from 3 to 9
move 1 from 5 to 4
move 1 from 2 to 5
move 1 from 9 to 4
move 1 from 5 to 9
move 2 from 4 to 2
move 3 from 1 to 4
move 1 from 2 to 1
move 10 from 3 to 2
move 7 from 7 to 3
move 11 from 7 to 9
move 5 from 3 to 1
move 1 from 4 to 5
move 11 from 2 to 3
move 9 from 9 to 3
move 3 from 9 to 4
move 2 from 4 to 8
move 1 from 5 to 6
move 13 from 1 to 5
move 3 from 3 to 8
move 3 from 7 to 2
move 1 from 7 to 4
move 3 from 8 to 3
move 8 from 3 to 8
move 4 from 4 to 5
move 2 from 8 to 2
move 8 from 8 to 3
move 1 from 6 to 3
move 2 from 2 to 8
move 6 from 5 to 2
move 3 from 2 to 8
move 1 from 1 to 7
move 2 from 9 to 3
move 3 from 5 to 4
move 2 from 8 to 6

24
2022/cpp/05/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/05/result Executable file

Binary file not shown.

23
2022/cpp/05/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
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;
}

9
2022/cpp/05/sample1 Normal file
View File

@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

9
2022/cpp/05/sample2 Normal file
View File

@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

BIN
2022/cpp/05/test Executable file

Binary file not shown.

40
2022/cpp/05/test.cpp Normal file
View File

@ -0,0 +1,40 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "../doctest/doctest/doctest.h"
#include "answer.hpp"
#if PART == 1
TEST_CASE("Part 1") {
Input expected_input{.stacks =
{
{'Z', 'N'},
{'M', 'C', 'D'},
{'P'},
},
.moves = {
{0, 1, 0},
{2, 0, 2},
{1, 1, 0},
{0, 0, 1},
}};
Input actual_input = get_input("sample1");
std::cout << expected_input << std::endl;
std::cout << actual_input << std::endl;
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) == "CMZ");
}
}
#endif
#if PART == 2
TEST_CASE("Part 2") {
Input actual_input = get_input("sample2");
SUBCASE("Testing output is the one expected from AOC") {
CHECK(get_result(actual_input) == "MCD");
}
}
#endif

33
2022/cpp/06/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)/

58
2022/cpp/06/answer.cpp Normal file
View File

@ -0,0 +1,58 @@
// AOC - 2022 - 06
#include "answer.hpp"
#include <algorithm>
#include <deque>
#include <fstream>
#include <iostream>
#include <queue>
#include <unordered_set>
Input get_input(const char* filename) {
std::ifstream stream(filename);
std::string str;
getline(stream, str);
return {str};
}
template <class T>
bool contains_duplicates(std::deque<T> q) {
std::unordered_set<T> set;
for (auto& elem : q) {
if (set.find(elem) != set.end())
return true;
set.insert(elem);
}
return false;
}
std::string get_result(Input input) {
std::string ret;
std::stringstream stream(input.str);
std::deque<char> q;
char c;
#if PART == 1
#define Q_SIZE 4
#else
#define Q_SIZE 14
#endif
for (int i = 0; i < Q_SIZE - 1; i++) {
stream >> c;
q.push_back(c);
}
int i = Q_SIZE - 1;
while (stream.rdbuf()->in_avail()) {
i++;
stream >> c;
q.push_back(c);
if (!contains_duplicates(q)) {
std::cout << q << "is duplicate free" << std::endl;
break;
}
std::cout << q << " DOES contain duplicates" << std::endl;
q.pop_front();
}
ret = std::to_string(i);
return ret;
}

29
2022/cpp/06/answer.hpp Normal file
View File

@ -0,0 +1,29 @@
#include "lib.hpp"
#include <sstream>
#include <string>
#ifndef VERBOSE
#define VERBOSE 0
#endif
#ifndef PART
#define PART 2
#endif
struct Input {
std::string str;
bool operator==(const Input& rhs) const {
return this->str == rhs.str;
}
};
inline std::ostream& operator<<(std::ostream& os, Input& inp) {
os << inp.str;
return os;
}
Input get_input(const char*);
std::string get_result(Input);

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

Binary file not shown.

1
2022/cpp/06/input Normal file
View File

@ -0,0 +1 @@
hlfhfzffqnnrlnnvnmmgbgwgttbppcrcnnmdmfdmmgwwrrqnrrscrctcbttvcvtvvhchjhccjgjttmddplplqplqlbqlblrrbrvvprpffpmmzpmpcczjzzbwwfssvrrvggncgncgcwczzswwqqjjflffpwfpwpbwpwpdpbpvvqffcfcjffjllncczfzzmhzzmddgdrgrwwjzzdjjsnjsjfsjsjhhcchlccchqchhzzpnngdgndnpnppsdsggbvgvgpprqrqmmlzmzllvrrcvclcwczcqqcdcfcqqmmzbzdzdjzdjdmjdjzdjjcvjvcjvcvssltstttfbtftrfrlrdllrqqfssslccjdcjdjfdjfjqjnqjnjnrnddtnndtnnztzqztqztqzzpmzmggzrgrwwdqwdwcdwdnnmlmgmtmtstwssbffcnclclnclcjjcjpcpqcpqcpqpmqqfccpcjppnspsnnzggnpntndtdqtthwhnhwnwllzhlzhlzzghzghhlhvhwhjhfjjcnjnvjnjvvqccdmmgddllnmnrrdtdnncggfhgfglfgfmfnnpvvggznnwvnwwfgghrrfwrwzwszzzldzdldhlhblhblhbbbgjgsjggmqqmrrzggrhhwpwdpwdpwplpgpbggtssqffbqfbqbnnsqnqfnngcnnmwnmnbmmmslsjllbtbbpllltzzhgzztllsdllrvvhvjvbbhcbhchmchcctbcttvccgwcwpcchrcrdrdggcrrntrrfllcffbdfflrrrgbgrbbbdqbqjbbgbgrrqwqtwqwhwghwhzwwcswsnwnqqjhjhwhfwhffdfgddgjgsjgjhgglhlwhlhssfqfhhdmdnmnppdcddfzzhmhqqntqnnjvnjvjddcvcgcbgbbpjbjtbjjfgftgffplljfjrrhqqpddlssrvsrvrpppsllsdsqqqzzfttqsqzssjbbrnbnnrbbsrshsrrshrhwhbwbrrsrfrttfqtfqqfddvrvjrjvjsjhjsjdjqdjjlqjjjgcjcmcncfcrcwrwsrsslffzszmsszrsssrnrjjvbvpvcppptbbhhrddbcbggbqbmmsqqwggfpfbblmldmmpmwpwfwjfjsjnjmmpllccjzcjcwwpswshhpthhzchctcbcrrrrmvrvrdvvjmmvgmgwglghllvmllzlzzsvzzrmmhnnsjnnpvpwvpwwmvwwdqqdffhhhmccfgfvggchcctrrmdrrhrhnhnzzgpzzgttnhthvhzzqvvvwpwqpqdppsnnrgnnhphphmhcmcrmrvvqlvqqsccqhchzhwwmvmzmczzgsgdsggthgglrlnrlllbdllhwlwltwwcswsgssbhbsbvsbsbwbhbnncrcllttbrbppjccfpfhhgshschsccmrcmmcrrrzvvrcrggmwgwjwnwjjbffjddjnjgngqgdgnndznndvvfqfgfvvrvqrvvpllnsszbsbdbbdzbdbzbqbzznrznzjzpptcptccvwccfscffrftrrsnsvvswvvhbhzzfbzffncchhcnngzzcpcmmfttsntnjjsccqbcqqmzzgppdhppdtppmffgtgvvlzlpptdtttdppqjqtqctcrrzsswwtnwtnwnqqvbbdgjhvmmzpnhfvsbddzhgdwcnfdstvhhbzlzcfjwhlptbhmbmblprtsdmrdhbbbwpplnzgdnrzjmgzgpqbggnqvwwtntzgfwqrztqtdrsnhpfzswptggnvbszdcrmrhhtlrrfnpqrnpwrbmhlfwmdqqdbqrwbzqjbzwrgmbgrtzrhdclqfgsrtsgfwqrnnqgwsncmpgffggssrqvwjlhpsghbqdtzwmvzzvcmzsjqvprvcqwqjbcqcqrhpwwcsrscgmfdppbgvmnrdfrppblznbstnjzwwgstjvtprjbhtpdfgrhdjnjmnlbfwggzhcngvcwvcfpcwdtdppwjrdzsnjlnrzbfqqshlnzvwsmscgpfwjzhtwgfwgzdhbdwwzbsmfwwbmvrlrpswnjlmfbfzhwvcmgwfzssmmtjlwtrpwpwgnspbgchdncbfcpjsvtzjqtwqwjwgbhrbwvhqbcstsgsnwsjmhrlrvzgqhqfrmnrjdrhdjwcwctpdrzctlvnfzmzwhsnfprlzgzjpqvzchlmvbhffhpfjtvsdbvbdmwgvmqpflhwwndbqthmmwshdtspsrvqdflmmzwbqbqmpfdwjmvpbzdnqzfmhzdgldqjjvgpfcqftvjzwnzmfqdggrwlfzdhjnhmtrjbnllgqpntwmhnwtglnqdwbqdblpwnnrdwzpsqzfwqcmhqhnpsdcwvdldphgnrtqzdbnnzdzfttldrqcztlvlrgpdqzrcthslmtqhfvbzrfgnlrprcpbsctqhspbhnjtzrzhqjzszbzdthttqmbznzssftztwlggmdqqdtfllqjzjtvpgjfhtbwtbmtjplqnbdmsvlnqcwtdbdvfjnzgsmpnhbvvwwfbrgffjqfsccdjdwvbsdhqwfzvcpjzjbdjgrdctjplhwbdhhnbnwstvndnnwtsgbhzbvwdshvmnbwsthlrggtmddvjbfzfrnrdrqfjpslrccctzpjbwpdbhlbzfmwbltcqfngdprvfhgcszdtpnrcpdmllfnlspgrdrpwqmqbmrglvlrsmrfqrtzzgjcvqtqzpmghjrvmdmvvqztrjzbzjwdqsmrwpqnswbzhjbzzhdvmnfdsztzdzrjssgnnfqvbtsqrrmcppjgrmnstrnrlwjvvcczqlcbmwqzdfpssfwdfrvwtstwchdgwtrhhcmppcqlmrqlnwqccfphsdhbsbmtjvpcwwjrmrllbpnrmpbvwgwbftpdpphccwqcblcnvvbbppscmnjqgddllbnbvmmqzdffrrjtqwllzgpqrmnlfqrptzqmdmnrfnjpvqvjbsqrhljslgqqcqqtmtbwjrpphtjgjbqpzmzrzjrfjwcdcnbsjfljclffjplnrrcfbmhphtcjrzlrvvjcznpgpnrdwwqvgnbnzqnlcghhgwvhqbvjzfbdvhrzlqfbtlqhpltfjlfpbnjbphmmpntzqgjmwjtchwmlwvfjmfflqzpqnvvrgnbddzlfpdpdjghfgbsfddjspnfdwvqppncmdgfrnvrpcrflhgjgbwdsbwblfcwbtlrrnjjdhbvmrzgsvjwgfnnhqfbvhprlmwwqgclzlbqbrdspcbhftmdscsmpwrggrmnsvjphjmzmmrlrhnmdhwjlbmjchtvsrcplfspsssjznmzcrqnsjjtwjzvlhshbptqwwvjhjvzrhphphsbphpnzpfbwcdnqrhrrvlrwrztlpqnrcfzrncsvpzqzgslrlrwhvtgjmfncldqmvshlmnlpqbgvnwqfcthgrgllmqrjqmfgznspgltpptglpdcvhtzsprprbldbzhbmjsqzwvjggwhsczltcvgwqhspzpzvljwqjgrgtwswjdswlzjzslrsslvqzncjwhbbjpbdthqpgmhfglggmlrgwdsplgscrwstntvrhjzjjlshtgmnnhvsjwfmcjbpzjcstmnpvtbgrfcfdwjljsrfhdphrdcslwhgvlnwltwchplvfzntfgcnlsvzrvnnczhhqdlwjvqprhmtjdtwmppffmszzzqtfrgnhnzqgqzhrjzgntcszstrfhhtptgvswvzvjcgcntmhzzmdgsmtgzhpfvqfnwmsjdhtfgmmbrrfsdlptchqqzqdqjncmtpznfssrcnmcdnthglmfzsfgltrndqsfmdftmfgchbwmzgrtjvgqtshlltthnnpqnzfrchzhdzrrnpzvfzblrmhwdwjnqdptlbvndmmlhzhvsfdlmlhqrgqqzsdqtpczwcrwcbsftvvphfbwjrvrnrcqbbcsqgnhltwzvllljcvpwjgslbmngcdmpdvjlgcnrzwqjdgrblncpqmrgjmpqjzvdmcmwfnwqlszdgwqdfznhsnpsjrfwrqpqmpvhstmzgqblfmcfvwljbhdfhdmqcvrwnqcstwtzgmng

39
2022/cpp/06/lib.hpp Normal file
View File

@ -0,0 +1,39 @@
#include <deque>
#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;
}
template <class T>
std::ostream& operator<<(std::ostream& os, const std::deque<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/06/result Executable file

Binary file not shown.

23
2022/cpp/06/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
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;
}

1
2022/cpp/06/sample1 Normal file
View File

@ -0,0 +1 @@
mjqjpqmgbljsphdztnvjfqwrcgsmlb

1
2022/cpp/06/sample2 Normal file
View File

@ -0,0 +1 @@
bvwbjplbgvbhsrlpgdmjqwftvncz

1
2022/cpp/06/sample3 Normal file
View File

@ -0,0 +1 @@
nppdvjthqldpwncqszvftbrmjlhg

1
2022/cpp/06/sample4 Normal file
View File

@ -0,0 +1 @@
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg

1
2022/cpp/06/sample5 Normal file
View File

@ -0,0 +1 @@
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw

BIN
2022/cpp/06/test Executable file

Binary file not shown.

67
2022/cpp/06/test.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <ostream>
#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_inputs;
expected_inputs.insert(expected_inputs.end(),
{
{"mjqjpqmgbljsphdztnvjfqwrcgsmlb"},
{"bvwbjplbgvbhsrlpgdmjqwftvncz"},
{"nppdvjthqldpwncqszvftbrmjlhg"},
{"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"},
{"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"},
});
std::vector<std::string> expected_outputs;
expected_outputs.insert(expected_outputs.end(),
{
"7",
"5",
"6",
"10",
"11",
});
for (int i : {0, 1, 2, 3, 4}) {
Input expected_input = expected_inputs[i];
std::string sample_name = "sample" + std::to_string(i + 1);
Input actual_input = get_input(sample_name.c_str());
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) == expected_outputs[i]);
}
}
}
#endif
#if PART == 2
TEST_CASE("Part 2") {
std::vector<std::string> expected_outputs;
expected_outputs.insert(expected_outputs.end(),
{
"19",
"23",
"23",
"29",
"26",
});
for (int i : {0, 1, 2, 3, 4}) {
std::string sample_name = "sample" + std::to_string(i + 1);
Input actual_input = get_input(sample_name.c_str());
SUBCASE("Testing output is the one expected from AOC") {
CHECK(get_result(actual_input) == expected_outputs[i]);
}
}
}
#endif

33
2022/cpp/07/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)/

145
2022/cpp/07/answer.cpp Normal file
View File

@ -0,0 +1,145 @@
// AOC - 2022 - 07
#include "answer.hpp"
#include <deque>
#include <fstream>
#include <iostream>
#include <sstream>
bool is_dir(std::string line) {
return line.substr(0, 3) == "dir";
}
bool is_command(std::string line) {
return line[0] == '$';
}
Dir* get_subdir(Dir* current_dir, const std::string& subdir_name) {
for (auto& dir : current_dir->dirs)
if (dir->name == subdir_name)
return dir;
std::cerr << "'" << subdir_name << "' not in " << current_dir->name
<< std::endl;
error("couldn't find subdir");
}
Dir* handle_command(
Dir* current_dir, std::string& cmd_line, std::ifstream& input) {
std::cout << "CWD: " << current_dir->name << std::endl;
std::cout << "Handling cmd: '" << cmd_line << "'" << std::endl;
std::stringstream cmd_stream(cmd_line);
std::string cmd, _;
cmd_stream >> _; // $
cmd_stream >> cmd;
std::string line;
Dir* reading_dir = current_dir;
if (cmd == "ls") {
if (!cmd_stream.eof()) {
std::string dir_name;
cmd_stream >> dir_name;
reading_dir = get_subdir(current_dir, dir_name);
}
while (std::getline(input, line) && !is_command(line)) {
if (is_dir(line)) {
std::string dir_name = line.substr(4);
std::cout << "Adding dir '" << dir_name << "' to "
<< current_dir->name << std::endl;
reading_dir->dirs.push_back(new Dir{reading_dir, dir_name});
} else {
uint size;
std::string file_name;
std::stringstream file_line_stream(line);
file_line_stream >> size;
file_line_stream >> file_name;
reading_dir->files.push_back({size, file_name});
}
}
std::cout << "After reading, the cwd looks like" << std::endl;
std::cout << *reading_dir << std::endl;
if (is_command(line))
return handle_command(current_dir, line, input);
else
return current_dir;
} else if (cmd == "cd") {
std::string new_dirname;
cmd_stream >> new_dirname;
std::cout << "cd-ing to " << new_dirname << std::endl;
if (new_dirname == "..") {
std::cout << "\t thus to " << current_dir->parent->name
<< std::endl;
return current_dir->parent;
} else
return get_subdir(current_dir, new_dirname);
} else {
std::cerr << "Invalid cmd: " << cmd << std::endl;
error("Invalid command");
}
}
Input get_input(const char* filename) {
std::ifstream stream(filename);
std::string str;
Dir* root = new Dir{nullptr, "/"};
getline(stream, str); // cd /
Dir* current_dir = root;
while (getline(stream, str))
current_dir = handle_command(current_dir, str, stream);
std::cout << "current" << std::endl;
std::cout << *current_dir << std::endl;
std::cout << "root" << std::endl;
std::cout << *root << std::endl;
return {*root};
}
#define CUTOFF_SIZE 100000
#define FILESYSTEM_SIZE 70000000
#define NEEDED_SIZE 30000000
std::string get_result(Input input) {
std::string ret;
#if PART == 1
std::vector<Dir*> queue;
queue.push_back(&input.root);
int i = 0;
uint res = 0;
while (i < queue.size()) {
std::cout << "ehhlo?" << std::endl;
Dir d = *queue[i++];
if (d.get_size() < CUTOFF_SIZE)
res += d.get_size();
for (auto& d : d.dirs)
queue.push_back(d);
for (auto& d : queue)
std::cout << *d << std::endl;
}
ret = std::to_string(res);
#else
uint space_to_free = input.root.get_size() - NEEDED_SIZE;
std::cout << space_to_free << std::endl;
std::vector<Dir*> queue;
queue.push_back(&input.root);
int i = 0;
uint min_above_needed = -1;
while (i < queue.size()) {
Dir d = *queue[i++];
if (d.get_size() < space_to_free)
continue;
else if (d.get_size() < min_above_needed)
min_above_needed = d.get_size();
for (auto& sub_dir : d.dirs)
queue.push_back(sub_dir);
for (auto& d : queue)
std::cout << d->name << std::endl;
}
ret = std::to_string(min_above_needed);
#endif
return ret;
}

80
2022/cpp/07/answer.hpp Normal file
View File

@ -0,0 +1,80 @@
#include "lib.hpp"
#include <string>
#ifndef VERBOSE
#define VERBOSE 0
#endif
#ifndef PART
#define PART 2
#endif
struct File {
uint size;
std::string name;
bool operator==(const File& rhs) const {
return this->name == rhs.name && this->size == rhs.size;
}
};
struct Dir {
Dir* parent;
std::string name;
std::vector<Dir*> dirs;
std::vector<File> files;
uint size = 0;
uint get_size() {
if (this->size > 0)
return this->size;
uint res = 0;
for (auto& f : this->files)
res += f.size;
for (auto& d : this->dirs)
res += d->get_size();
this->size = res;
return res;
}
bool operator==(const Dir& rhs) const {
return this->name == rhs.name && this->dirs == rhs.dirs &&
this->files == rhs.files;
}
};
struct Input {
Dir root;
bool operator==(const Input& rhs) const {
return this->root == rhs.root;
}
};
inline std::ostream& operator<<(std::ostream& os, File& file) {
os << "- " << file.name << " (file, size=" << file.size << ")" << std::endl;
return os;
}
inline std::ostream& operator<<(std::ostream& os, Dir& dir) {
os << "- " << dir.name << " (dir";
if (dir.parent)
os << ", parent=" << dir.parent->name;
os << ")" << std::endl;
for (auto& d : dir.dirs)
os << '\t' << *d;
for (auto& f : dir.files)
os << '\t' << f;
return os;
}
inline std::ostream& operator<<(std::ostream& os, Input& inp) {
os << inp.root;
return os;
}
Input get_input(const char*);
std::string get_result(Input);

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

Binary file not shown.

1101
2022/cpp/07/input Normal file

File diff suppressed because it is too large Load Diff

23
2022/cpp/07/lib.hpp Normal file
View 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);
}

BIN
2022/cpp/07/result Executable file

Binary file not shown.

23
2022/cpp/07/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
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;
}

23
2022/cpp/07/sample1 Normal file
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

23
2022/cpp/07/sample2 Normal file
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

BIN
2022/cpp/07/test Executable file

Binary file not shown.

25
2022/cpp/07/test.cpp Normal file
View File

@ -0,0 +1,25 @@
#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");
std::cout << "actual_input" << std::endl;
std::cout << actual_input << std::endl;
SUBCASE("Testing output is the one expected from AOC") {
CHECK(get_result(actual_input) == "95437");
}
}
#endif
#if PART == 2
TEST_CASE("Part 2") {
Input actual_input = get_input("sample2");
SUBCASE("Testing output is the one expected from AOC") {
CHECK(get_result(actual_input) == "24933642");
}
}
#endif

33
2022/cpp/08/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)/

130
2022/cpp/08/answer.cpp Normal file
View File

@ -0,0 +1,130 @@
// AOC - 2022 - 08
#include "answer.hpp"
#include <fstream>
#include <iostream>
#include <string>
Input get_input(const char* filename) {
std::ifstream stream(filename);
std::string str;
Input ret;
getline(stream, str);
ret.width = str.size();
for (auto c : str)
ret.trees.push_back(c);
while (getline(stream, str))
for (char c : str)
ret.trees.push_back(c);
ret.height = ret.trees.size() / ret.width;
return ret;
}
bool is_tree_seen(uint x, uint y, Forest& forest) {
Tree current_tree = forest.tree_at(x, y);
bool is_seen = true;
// north
for (uint i = 0; i < y; i++)
if (forest.tree_at(x, i) >= current_tree)
is_seen = false;
if (is_seen)
return true;
is_seen = true;
// east
for (uint i = x + 1; i < forest.width; i++)
if (forest.tree_at(i, y) >= current_tree)
is_seen = false;
if (is_seen)
return true;
is_seen = true;
// south
for (uint i = y + 1; i < forest.height; i++)
if (forest.tree_at(x, i) >= current_tree)
is_seen = false;
if (is_seen)
return true;
is_seen = true;
// west
for (uint i = 0; i < x; i++)
if (forest.tree_at(i, y) >= current_tree)
is_seen = false;
if (!is_seen)
std::cout << "couldn't see tree " << current_tree << " at " << x << ", "
<< y << std::endl;
return is_seen;
}
uint scenic_score(uint x, uint y, Forest& forest) {
Tree current_tree = forest.tree_at(x, y);
uint total_score = 1;
uint score = 0;
// north
for (int i = y - 1; i >= 0; i--) {
score++;
if (forest.tree_at(x, i) >= current_tree)
break;
}
total_score *= score;
score = 0;
// east
for (uint i = x + 1; i < forest.width; i++) {
score++;
if (forest.tree_at(i, y) >= current_tree)
break;
}
total_score *= score;
score = 0;
// south
for (uint i = y + 1; i < forest.height; i++) {
score++;
if (forest.tree_at(x, i) >= current_tree)
break;
}
total_score *= score;
score = 0;
// west
for (int i = x - 1; i >= 0; i--) {
score++;
if (forest.tree_at(i, y) >= current_tree)
break;
}
total_score *= score;
return total_score;
}
std::string get_result(Input& forest) {
std::string ret;
#if PART == 1
uint count = 2 * forest.width + 2 * forest.height - 4;
for (uint y = 1; y < forest.height - 1; y++) {
for (uint x = 1; x < forest.width - 1; x++)
if (is_tree_seen(x, y, forest))
count++;
}
ret = std::to_string(count);
#else
uint max_scenic_score = 0;
for (uint y = 1; y < forest.height - 1; y++) {
for (uint x = 1; x < forest.width - 1; x++) {
uint score = scenic_score(x, y, forest);
if (max_scenic_score < score)
max_scenic_score = score;
}
}
ret = std::to_string(max_scenic_score);
#endif
return ret;
}

48
2022/cpp/08/answer.hpp Normal file
View File

@ -0,0 +1,48 @@
#include "lib.hpp"
#include <cstdint>
#include <string>
#ifndef VERBOSE
#define VERBOSE 0
#endif
#ifndef PART
#define PART 2
#endif
typedef uint8_t Tree;
struct Forest {
uint width, height;
std::vector<Tree> trees;
bool operator==(const Forest& rhs) const {
return this->trees == rhs.trees;
}
uint get_idx(uint x, uint y) {
return y * width + x;
}
Tree tree_at(uint x, uint y) {
return trees[get_idx(x, y)];
}
};
typedef Forest Input;
inline std::ostream& operator<<(std::ostream& os, Input& inp) {
uint col = 0;
for (auto& tree : inp.trees) {
os << tree;
if (++col % inp.width == 0)
os << '\n';
}
return os;
}
Input get_input(const char*);
std::string get_result(Input&);

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

Binary file not shown.

99
2022/cpp/08/input Normal file
View File

@ -0,0 +1,99 @@
200120010031113332213034140102430141241124321111512323442304040044220141342121012210022220211111200
111021120230012003124233312003010303204555454332453445353125225214221214442214301113012111020220220
010000112330232223210123331341111021455213441355415452453433542411022110033014112420103200010200112
210220131023203033023421012411405531115123145115445215311422135252541100222231431331003220231121021
020112210133112114444340100410411231114131452544155151415233415312511222041221113442132231003203100
121210122332301142321211440441113455552434332511455325315545354524142541534411112013143013120333022
002022223332143323320030004111431434153352153321414154453442552321332341355132444030442110103203212
212130023213100232130230411132244242133251435525323326611211534441513532233340420321144212221332012
112201210213131241203033341411115513233426453426433653356253533133113511115222441140433214102332200
212012000101044144022321521321444445566546646625625665232323663631331412343532242340023204432003233
101233132121222100241254211542133266356326564552445466322626353633223221135151311240430411433022011
200132020222314334333551152431232424524623645555522366665322366466545513112135535223102441334310202
023113000020114221524122422222662645435544455234636242322656464245462233555511334411233402112203303
323212301014434412315252233452332225554646452654545433445553336343544664455435323243510303023142200
310022200424121333144123222262332322343322244646667564465544426245466232342143412141451122001041322
212301111322001335441132334525635254352425553354565445643472236236266635446421522132452121404230333
222212444300345211222512256444456624535343536756355746664436374344346652444335121144422222340114403
133103420101433344253425324366444265345745457464444766674564476573243345254266312555221132304022243
104024111323421413115464433455553477333456354575666765646757774776752425462423244113225443242031103
102001244212344453145353232652647556546465453563544776667356374453675332254663444121345213131024201
224444241243433331136646422564367344447534333336573546336576477746463642222264422255444411444213332
111102440221143134255523244546456346745346547453543465664363545634575433436342252431313443241200434
134231321331555233423635635366465473563535535586445888474374535544636375425246452265522242444124430
234112414314111344422243544643454775656784865866884848444877466476364476356322553554245224411411221
034400114351525345222546546744637535635657575787587675474664764764736443646462322222525151425423322
200214241324215322354446653764777674587466544685754678446567884767437556756324542252661541311422130
343144113253113264625454456777333344687566876677476786864558645875776364454562645333232242321313142
343024121143435322432236376444777668467756646886688564457885758774655565674476525265433235351133020
403402124545255442342535445764457654765745674556784877787445455686478773563733543423236235255444144
103322531152132266562676355566367676848756788886766967585484486646776553753344443262466623351341241
310355244513624552353465735465555764667556575665597778969594858667646774744555337236256442514154340
023311451112523462333545544537465688645559988688956997665697964748587858337347363225646321434554144
320135522136342345436443367666564847488558589757575579796688785767645678467555334323622425311134113
302355525243323546577736555686874886486985688686888898966978978668487864787645754566534465541511251
334125312126266532347576536548547584696886997768569758698995755875465674754333433642553345425533431
341111335132225635365534457665748575977867585959997886979957965558787465664637533766462255534332423
332515314534343636736373478687676678865555895657977886779567677978977887466835476754353523355412552
223554423243256435734467677444784886576856669787898778968956989875566754875777467667635224535114135
155522522343322534364735656868875656687568869667769697877887796866975856457447636733764232364431215
242345244424435673345476658556448689589759768689899966877768987857579874745884637474453634352133231
415255446645436567674735664645455855996887768879976678686967768687868985856885736776326622253351232
345511155426334655353457846578475998667966669787979667866969678557875668566464436456435236255355314
142554534636264637437346664558867877978687978689679869967766679569789677485678377367342526223134543
445152112563265336347774464854776998568899676687677797996796877777879786686845577654534235322155423
333311452256452445537757558677965658888896678979799998788899698785896978877575444547572432544335251
153342523322435374455644657476596955598987666997999878876686677879688676464687476363732553363413554
144335145664565566437454557675966795686769969779977988878679878778787789465465637576634655354532332
152415343652336664567566647557776598677998979889877998977867798897785759685544636646555455665423232
233452324356364643433768554555595977968766779897887998898666688775789556775758843355374525244243434
415514136342636535455644448567857957878867697988999887897878896698589669784466744746433345345355353
232342145445335734443368474877857768999979889789879797978786868766697957448687837436437246435352325
423142134652644455355368647749855698998886897798977997799867788779868756865564445675347463653623132
221215125542657466763755887857659858677696867997877899797779899996787779754784846775376254442211254
154531463524644747547747455647685566769977767978799879788898879798999996758665537564563322242224125
114543323332546776763457675777969696978788779877889799879998997975759878686666834757774555334611241
353441154256436766664748544445957885788777978878979877989798876799786999466647747557553324526153313
433233236244364537664567776764977668967888876787887977898978679686668778764647857553662664333221513
112335142543334473565677774456986769698869989679787999966877889796668798648474667745633463364123315
113555236654542657737447854474695576987788777767987976776779677896568655666568777374452626625515123
335535115334436455537766485588496998559679678997888767666867796988758985466776445675326242423551222
113534446446435634464566878474597579966586699697988866966997787776788976778774466655756463456235323
432234422656545375357656547448457656589888789768979867898878979959955468888683644466555336562315155
313344415452422533675445775845665999956589788887986877769867786689888565577764433373633363624515141
222432351346446453465663784584875977688789959977876966969658866777886544775874577535665532224311252
225114233145352533344653757488874695566977756698769768695668575699668757584555454357642544414312222
033415322244643242337557674678486867575857998965787859688788778777887745544746656765646262354135222
244444135424622245536477356767676647596876888859989597677576756885858547856577434533266366252341111
043422441154553466473376764757644888596669857975585975898957778585855575867574554436566563342243533
331142132342425642446677456357664865566895568659588999997598897847485545576757377726563624233332331
023252542543632422666654576368776445864568889675685656595757555564576478356355573264462564525315531
401013153355343645357335364463874854866687865999886679877556446867474565563474773455256232432344241
021145311524153334266573666344586785645787666877957988698886547444446737764473344255436645355133043
343022114235145422524664767447348848745548778464684486768568675765556575337775543626443412212441440
111402455441135334663624565477563468586767564584754776865658486876757446535673333342623334312243002
300312451335434322222245676775746358685854756547745674447587647487533473336654232544332431554454412
433111155454541556355524273356653577688858857767884847858548544877576457444762553325524214215402141
131233333423335563265232427676636343767667757678865557657665545337553674363435662333422331253432142
424110224332255444554444426563344537343587775787775766858448446366677553432226234426335534235001312
333412002215124441543424366273747763767633456777658554454464446357566564765333524264253113145112300
101110030515255434456252656567734365435643756543777575374763565634563366532624443621453215143234300
303322412424455315323623364345353365376676743654344676735433644466637676342646445331232331222232310
323112211415542144152522545435246756534467755554567676637543357465766366552324534423444455001240332
104010010231221552145233456226456635564775656444567465773637433733444632663325561451234351230241312
121411232340543435321316435636242655564355535365574763633547465475523553325562122152444444042300041
131012210332331125512334254246234345354543573744464635755354544666345534262435114123351541032131413
320204021134412321515435566234224352423426577577364457356445435455454244243342521425351312003010221
323114214132034241313542544555435465334424654666346364554655422236635456436354122354410044244241323
013302120424301452153353441245652435266525344656664634425626224355262362255351231351513443132401213
320030122021334215313244242334523262333453263233364243362363226452453421344311524253311414201210001
232000301112410134252124412414346224626463252363536262245565624425334453135542241232142142113023001
023101113421202000305251552134255163452646652324334242436543224522235515444445314014323100240020120
320223110031202012323335553432231212252653563665545464422324656333443431535442453303001103202231020
102033222010100142203435155413125314311166645446465322244546532441244154143433143443342400201012012
103310002120123402241302221241151441435355114335344522434421453552354154135223134443030043002000021
112323110033212002113322411233332355223342424233213123445224135331545115152043042400020200022133330
022111301200101044211214231433231311322554214331413543414521424255241531111410022413003020200231110
012022111212202121003233141343155411314255551134343533133453252153433344441013011221420230021203220
221101122202110034040022441212441254112211113355531534335143125434435312222200311411322330203331100
210122210200133032213341213111433213432513121525124133544144433541201142230241123430213303001122221

23
2022/cpp/08/lib.hpp Normal file
View 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);
}

BIN
2022/cpp/08/result Executable file

Binary file not shown.

23
2022/cpp/08/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
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;
}

5
2022/cpp/08/sample1 Normal file
View File

@ -0,0 +1,5 @@
30373
25512
65332
33549
35390

5
2022/cpp/08/sample2 Normal file
View File

@ -0,0 +1,5 @@
30373
25512
65332
33549
35390

BIN
2022/cpp/08/test Executable file

Binary file not shown.

58
2022/cpp/08/test.cpp Normal file
View File

@ -0,0 +1,58 @@
#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;
expected_input.width = 5;
for (auto t : {
'3',
'0',
'3',
'7',
'3',
'2',
'5',
'5',
'1',
'2',
'6',
'5',
'3',
'3',
'2',
'3',
'3',
'5',
'4',
'9',
'3',
'5',
'3',
'9',
'0',
}) {
expected_input.trees.push_back(t);
};
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) == "21");
}
}
#endif
#if PART == 2
TEST_CASE("Part 2") {
Input actual_input = get_input("sample2");
SUBCASE("Testing output is the one expected from AOC") {
CHECK(get_result(actual_input) == "8");
}
}
#endif

16
2022/cpp/Makefile Normal file
View File

@ -0,0 +1,16 @@
COOKIE = 53616c7465645f5f49dff2a6091a00e67a9678384d5d9867c24d2c2024c8524adb2391b31b45118c978505c7ea27ddecf0cc047ce8d51dc32203dffde652c690
.PHONY = new
NUMBER := $$(($(shell ls -D | grep "^[0-2][0-9]$$" | wc -l) + 1))
NUMBER_DIR := $(shell printf "%02d" $(NUMBER))
# NUMBER := $(shell ls -D | wc -l)
# https://adventofcode.com/2022/day/1/input
new:
@echo "Creating $(NUMBER)..."
cp -r template/ $(NUMBER_DIR)/
@echo "Getting input"
@wget -q -O $(NUMBER_DIR)/input --header "Cookie: session=$(COOKIE)" https://adventofcode.com/2022/day/$(NUMBER)/input
sed -i "s/DATE/$(NUMBER_DIR)/" $(NUMBER_DIR)/answer.cpp
xclip -o > $(NUMBER_DIR)/sample1

1
2022/cpp/doctest Submodule

@ -0,0 +1 @@
Subproject commit 86892fc480f80fb57d9a3926cb506c0e974489d8

Some files were not shown because too many files have changed in this diff Show More