This commit is contained in:
Eric Mertens 2022-11-25 12:07:41 -08:00
parent d041919d91
commit f021fddf35
2 changed files with 82 additions and 0 deletions

79
2017/13.cpp Normal file
View File

@ -0,0 +1,79 @@
#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(int argc, char** argv) -> int {
auto input = Parse(*Startup(argc, argv));
std::cout << "Part 1: " << Caught(input) << std::endl;
std::cout << "Part 2: " << Delay(input) << std::endl;
}

View File

@ -13,6 +13,9 @@ target_link_libraries(2017_11 aocpp)
add_executable(2017_12 12.cpp) add_executable(2017_12 12.cpp)
target_link_libraries(2017_12 aocpp) target_link_libraries(2017_12 aocpp)
add_executable(2017_13 13.cpp)
target_link_libraries(2017_13 aocpp)
add_executable(2017_14 14.cpp) add_executable(2017_14 14.cpp)
target_link_libraries(2017_14 aocpp knothash) target_link_libraries(2017_14 aocpp knothash)