2022-11-06 21:12:30 -08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-11-13 11:42:40 -08:00
|
|
|
#include <doctest.h>
|
|
|
|
|
2022-11-06 21:12:30 -08:00
|
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
|
2022-11-13 11:42:40 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-31 08:58:42 -08:00
|
|
|
auto Main(std::istream & in) -> void
|
|
|
|
{
|
2022-11-13 11:42:40 -08:00
|
|
|
std::int64_t weight;
|
2022-11-06 21:12:30 -08:00
|
|
|
std::int64_t part1 = 0;
|
|
|
|
std::int64_t part2 = 0;
|
|
|
|
|
2022-11-13 11:42:40 -08:00
|
|
|
while (in >> weight) {
|
|
|
|
part1 += fuel1(weight);
|
|
|
|
part2 += fuel2(weight);
|
2022-11-06 21:12:30 -08:00
|
|
|
}
|
2022-11-13 11:42:40 -08:00
|
|
|
|
2022-11-06 21:12:30 -08:00
|
|
|
std::cout << "Part 1: " << part1 << std::endl;
|
|
|
|
std::cout << "Part 2: " << part2 << std::endl;
|
|
|
|
}
|