aocpp/2019/01.cpp

62 lines
1.1 KiB
C++
Raw Normal View History

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
2022-11-13 11:42:40 -08:00
#include <doctest.h>
#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);
}
}
auto main(int argc, char** argv) -> int {
2022-11-07 21:00:14 -08:00
auto& in = aocpp::Startup(argc, argv);
2022-11-13 11:42:40 -08:00
std::int64_t weight;
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-13 11:42:40 -08:00
std::cout << "Part 1: " << part1 << std::endl;
std::cout << "Part 2: " << part2 << std::endl;
}