This commit is contained in:
zv0n 2022-12-01 06:11:51 +01:00
parent b372144171
commit 6a437989f4
3 changed files with 2317 additions and 0 deletions

16
2022/01/Makefile Normal file
View File

@ -0,0 +1,16 @@
CXX ?= c++
CXXFLAGS ?= -std=c++11 -Wall -Wextra -pedantic -O2
PROJECT = calories
all: ${PROJECT}
${PROJECT}: main.cpp
${CXX} ${CXXFLAGS} -o $@ $^
test: ${PROJECT}
./${PROJECT}
clean:
${RM} *.o ${PROJECT}
.PHONY: all clean test

2255
2022/01/input Normal file

File diff suppressed because it is too large Load Diff

46
2022/01/main.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
std::vector<uint64_t> getCalories( std::ifstream &file ) {
std::vector<uint64_t> ret{};
ret.push_back({});
uint64_t tmp = 0;
std::string str;
uint64_t calories = 0;
while ( std::getline( file, str ) ) {
if(str.empty()) {
ret.push_back(calories);
calories = 0;
continue;
}
std::stringstream ss( str );
ss >> tmp;
calories += tmp;
}
ret.push_back(calories);
std::sort(ret.begin(), ret.end());
std::reverse(ret.begin(), ret.end());
return ret;
}
uint64_t getHighestCaloriesElf(const std::vector<uint64_t> &elves) {
return elves[0];
}
uint64_t get3HighestCaloriesElf(const std::vector<uint64_t> &elves) {
return elves[0] + elves[1] + elves[2];
}
int main() {
std::ifstream input_file( "input" );
auto elves = getCalories( input_file );
int part1 = getHighestCaloriesElf(elves);
std::cout << "The elf carrying the most calories is carrying \033[91;1m" << part1
<< "\033[0m calories." << std::endl;
int part2 = get3HighestCaloriesElf(elves);
std::cout << "The 3 elves carrying the most calories are carrying \033[91;1m" << part2
<< "\033[0m calories in total." << std::endl;
}