2022-01
This commit is contained in:
parent
3982cb8a4c
commit
d3525c097b
83
2022/01.cpp
Normal file
83
2022/01.cpp
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
#include <numeric>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <doctest.h>
|
||||||
|
|
||||||
|
#include <aocpp/Startup.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using Input = std::vector<std::vector<std::int64_t>>;
|
||||||
|
|
||||||
|
/// @brief Parse the input stream as a newline-delimited list of lists of integers
|
||||||
|
/// @param in input file parsed until EOF
|
||||||
|
/// @return outer list of elves, inner lists of calories
|
||||||
|
auto Parse(std::istream & in) -> Input
|
||||||
|
{
|
||||||
|
Input result {{}};
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(in, line)) {
|
||||||
|
if (line.empty()) {
|
||||||
|
result.push_back({});
|
||||||
|
} else {
|
||||||
|
result.back().emplace_back(std::stoll(line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Compute the top calorie carrying elves
|
||||||
|
/// @param input List of elves each with a list of calories
|
||||||
|
/// @return top 1 and sum of top 3 calorie counts
|
||||||
|
auto Solve(Input const& input) -> std::pair<std::int64_t, std::int64_t>
|
||||||
|
{
|
||||||
|
if (input.size() < 3) { throw std::runtime_error{"bad input"}; }
|
||||||
|
|
||||||
|
std::vector<std::int64_t> elves;
|
||||||
|
for (auto const& elf : input) {
|
||||||
|
elves.push_back(std::reduce(elf.begin(), elf.end()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::partial_sort(elves.begin(), std::next(elves.begin(), 3), elves.end(), std::greater());
|
||||||
|
|
||||||
|
return {elves.front(), std::reduce(elves.begin(), std::next(elves.begin(), 3))};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_SUITE("2022-01 examples") {
|
||||||
|
TEST_CASE("example") {
|
||||||
|
std::istringstream in {
|
||||||
|
"1000\n"
|
||||||
|
"2000\n"
|
||||||
|
"3000\n"
|
||||||
|
"\n"
|
||||||
|
"4000\n"
|
||||||
|
"\n"
|
||||||
|
"5000\n"
|
||||||
|
"6000\n"
|
||||||
|
"\n"
|
||||||
|
"7000\n"
|
||||||
|
"8000\n"
|
||||||
|
"9000\n"
|
||||||
|
"\n"
|
||||||
|
"10000\n"};
|
||||||
|
auto entries = Parse(in);
|
||||||
|
auto [p1,p2] = Solve(entries);
|
||||||
|
CHECK(p1 == 24000);
|
||||||
|
CHECK(p2 == 45000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto main(int argc, char** argv) -> int {
|
||||||
|
auto [p1,p2] = Solve(Parse(*aocpp::Startup(argc, argv)));
|
||||||
|
std::cout << "Part 1: " << p1 << std::endl;
|
||||||
|
std::cout << "Part 2: " << p2 << std::endl;
|
||||||
|
}
|
2
2022/CMakeLists.txt
Normal file
2
2022/CMakeLists.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
add_executable(2022_01 01.cpp)
|
||||||
|
target_link_libraries(2022_01 aocpp)
|
|
@ -28,3 +28,4 @@ add_subdirectory(2017)
|
||||||
add_subdirectory(2018)
|
add_subdirectory(2018)
|
||||||
add_subdirectory(2019)
|
add_subdirectory(2019)
|
||||||
add_subdirectory(2020)
|
add_subdirectory(2020)
|
||||||
|
add_subdirectory(2022)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user