aocpp/2017/05.cpp

66 lines
1.2 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);
}
}
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: " << Run1(input) << std::endl;
out << "Part 2: " << Run2(std::move(input)) << std::endl;
2022-11-24 14:12:20 -08:00
}