aocpp/2017/13.cpp

79 lines
1.6 KiB
C++
Raw Permalink Normal View History

2022-11-25 12:07:41 -08:00
#include <cstdint>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <tuple>
#include <doctest.h>
#include <aocpp/Startup.hpp>
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;
}
2023-01-31 08:58:42 -08:00
auto Delay(std::vector<Scanner> scanners) {
2022-11-25 12:07:41 -08:00
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);
}
}
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
auto input {Parse(in)};
2023-01-31 09:15:15 -08:00
out << "Part 1: " << Caught(input) << std::endl;
out << "Part 2: " << Delay(std::move(input)) << std::endl;
2023-01-31 08:58:42 -08:00
}