aocpp/2017/13.cpp
2023-01-31 08:59:09 -08:00

81 lines
1.6 KiB
C++

#include <cstdint>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <tuple>
#include <doctest.h>
#include <aocpp/Startup.hpp>
using namespace aocpp;
namespace {
struct Scanner {
std::uint64_t depth, range, position, length;
};
auto Parse(std::istream & in)
-> std::vector<Scanner>
{
std::uint64_t depth, range;
char skip;
std::vector<Scanner> result;
while (in >> depth >> skip >> range) {
auto len = range*2-2;
result.push_back(Scanner{depth, range, depth%len, len});
}
return result;
}
auto Caught(std::vector<Scanner> const& scanners) -> std::size_t {
std::uint64_t severity = 0;
for (auto const& scanner : scanners) {
if (scanner.position == 0) {
severity += scanner.depth * scanner.range;
}
}
return severity;
}
auto Delay(std::vector<Scanner> scanners) {
std::uint64_t delay = 0;
for (;;) {
bool success = true;
for (auto & scanner : scanners) {
if (scanner.position++ == 0) { success = false; }
if (scanner.position == scanner.length) scanner.position = 0;
}
if (success) break;
delay++;
}
return delay;
}
} // namespace
TEST_SUITE("2017-12 examples") {
TEST_CASE("example") {
std::istringstream in {
"0: 3\n"
"1: 2\n"
"4: 4\n"
"6: 4\n"
};
auto input = Parse(in);
CHECK(Caught(input) == 24);
CHECK(Delay(input) == 10);
}
}
auto Main(std::istream & in) -> void
{
auto input {Parse(in)};
std::cout << "Part 1: " << Caught(input) << std::endl;
std::cout << "Part 2: " << Delay(std::move(input)) << std::endl;
}