66 lines
1.2 KiB
C++
66 lines
1.2 KiB
C++
#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(std::istream & in, std::ostream & out) -> void
|
|
{
|
|
auto input {Parse(in)};
|
|
out << "Part 1: " << Run1(input) << std::endl;
|
|
out << "Part 2: " << Run2(std::move(input)) << std::endl;
|
|
}
|