aocpp/2017/05.cpp

65 lines
1.3 KiB
C++
Raw Normal View History

2022-11-24 14:12:20 -08:00
#include <cstdint>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
#include <doctest.h>
#include <aocpp/Startup.hpp>
namespace {
auto Parse(std::istream & in) -> std::vector<std::int64_t> {
std::vector<std::int64_t> result;
std::int64_t x;
while (in >> x) {
result.push_back(x);
}
return result;
}
auto Run1(std::vector<std::int64_t> program) -> std::size_t {
std::int64_t cursor = 0;
std::size_t n = 0;
while (0 <= cursor && cursor < program.size()) {
cursor += program[cursor]++;
n++;
}
return n;
}
auto Run2(std::vector<std::int64_t> program) -> std::size_t {
std::int64_t cursor = 0;
std::size_t n = 0;
while (0 <= cursor && cursor < program.size()) {
auto & p = program[cursor];
cursor += p;
p += p >= 3 ? -1 : 1;
n++;
}
return n;
}
} // namespace
TEST_SUITE("2017-05 examples") {
TEST_CASE("example") {
std::istringstream in { "0\n3\n0\n1\n-3\n" };
auto input = Parse(in);
CHECK(Run1(input) == 5);
CHECK(Run2(input) == 10);
}
}
auto main(int argc, char** argv) -> int {
auto input = Parse(*aocpp::Startup(argc, argv));
std::cout << "Part 1: " << Run1(input) << std::endl;
std::cout << "Part 2: " << Run2(std::move(input)) << std::endl;
}