63 lines
1.1 KiB
C++
63 lines
1.1 KiB
C++
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <vector>
|
|
|
|
#include <doctest.h>
|
|
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
namespace {
|
|
|
|
auto fuel1(std::int64_t weight) -> std::int64_t {
|
|
return weight / 3 - 2;
|
|
}
|
|
|
|
auto fuel2(std::int64_t weight) -> std::int64_t {
|
|
std::int64_t total = 0;
|
|
for(;;) {
|
|
weight = fuel1(weight);
|
|
if (weight <= 0) break;
|
|
total += weight;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
}
|
|
|
|
TEST_SUITE("documented examples") {
|
|
|
|
TEST_CASE("part 1") {
|
|
REQUIRE(fuel1(12) == 2);
|
|
REQUIRE(fuel1(14) == 2);
|
|
REQUIRE(fuel1(1969) == 654);
|
|
REQUIRE(fuel1(100756) == 33583);
|
|
}
|
|
|
|
TEST_CASE("part 2") {
|
|
REQUIRE(fuel2(14) == 2);
|
|
REQUIRE(fuel2(1969) == 966);
|
|
REQUIRE(fuel2(100756) == 50346);
|
|
}
|
|
|
|
}
|
|
|
|
auto main(int argc, char** argv) -> int {
|
|
auto in_ptr = aocpp::Startup(argc, argv);
|
|
auto & in = *in_ptr;
|
|
|
|
std::int64_t weight;
|
|
std::int64_t part1 = 0;
|
|
std::int64_t part2 = 0;
|
|
|
|
while (in >> weight) {
|
|
part1 += fuel1(weight);
|
|
part2 += fuel2(weight);
|
|
}
|
|
|
|
std::cout << "Part 1: " << part1 << std::endl;
|
|
std::cout << "Part 2: " << part2 << std::endl;
|
|
}
|