Added 2022 (cpp)
This commit is contained in:
35
2022/cpp/01/Makefile
Normal file
35
2022/cpp/01/Makefile
Normal 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
68
2022/cpp/01/answer.cpp
Normal 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
41
2022/cpp/01/answer.hpp
Normal 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
BIN
2022/cpp/01/build/answer.o
Normal file
Binary file not shown.
2251
2022/cpp/01/input
Normal file
2251
2022/cpp/01/input
Normal file
File diff suppressed because it is too large
Load Diff
BIN
2022/cpp/01/result
Executable file
BIN
2022/cpp/01/result
Executable file
Binary file not shown.
28
2022/cpp/01/result.cpp
Normal file
28
2022/cpp/01/result.cpp
Normal 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
14
2022/cpp/01/sample1
Normal file
@ -0,0 +1,14 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
14
2022/cpp/01/sample2
Normal file
14
2022/cpp/01/sample2
Normal file
@ -0,0 +1,14 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
BIN
2022/cpp/01/test
Executable file
BIN
2022/cpp/01/test
Executable file
Binary file not shown.
65
2022/cpp/01/test.cpp
Normal file
65
2022/cpp/01/test.cpp
Normal 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
|
Reference in New Issue
Block a user