67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
#include <cstdint>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
#include <doctest.h>
|
|
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
namespace {
|
|
|
|
auto Step(std::vector<std::uint32_t> & spin, int steps) -> void {
|
|
std::uint32_t cursor = spin.size() - 1;
|
|
for (int i = 0; i < steps; i++) {
|
|
cursor = spin[cursor];
|
|
}
|
|
spin.push_back(spin[cursor]);
|
|
spin[cursor] = spin.size() - 1;
|
|
cursor = spin.size() - 1;
|
|
}
|
|
|
|
auto Part1(std::uint64_t jump, std::uint64_t iterations) -> std::uint64_t {
|
|
std::vector<std::uint32_t> spin {0};
|
|
for (int j = 0; j < iterations; j++) {
|
|
Step(spin, jump);
|
|
}
|
|
return spin.back();
|
|
}
|
|
|
|
auto Part2(std::uint64_t jump, std::uint64_t iterations) -> std::uint64_t {
|
|
std::uint64_t cursor = 0;
|
|
std::uint64_t after_zero = -1;
|
|
for (std::uint64_t i = 1; i <= iterations; ++i) {
|
|
cursor += jump;
|
|
cursor %= i;
|
|
if (cursor == 0) {
|
|
after_zero = i;
|
|
}
|
|
cursor++;
|
|
}
|
|
|
|
return after_zero;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_SUITE("2017-17 examples") {
|
|
TEST_CASE("example") {
|
|
std::vector<std::uint32_t> spin {0};
|
|
Step(spin, 3);
|
|
REQUIRE(spin == std::vector<std::uint32_t>{1,0});
|
|
Step(spin, 3);
|
|
REQUIRE(spin == std::vector<std::uint32_t>{2,0,1});
|
|
Step(spin, 3);
|
|
REQUIRE(spin == std::vector<std::uint32_t>{2,0,3,1});
|
|
|
|
CHECK(Part1(3, 2017) == 638);
|
|
}
|
|
}
|
|
|
|
auto Main(std::istream & in, std::ostream & out) -> void
|
|
{
|
|
int steps;
|
|
in >> steps;
|
|
out << "Part 1: " << Part1(steps, 2017) << std::endl;
|
|
out << "Part 2: " << Part2(steps, 50'000'000) << std::endl;
|
|
}
|