2023-01-19 21:15:23 -08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <numeric>
|
|
|
|
#include <sstream>
|
2023-04-07 09:45:55 -07:00
|
|
|
#include <stdexcept>
|
2023-01-19 21:15:23 -08:00
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-01-25 13:48:54 -08:00
|
|
|
#include <boost/range/irange.hpp>
|
|
|
|
|
2023-01-19 21:15:23 -08:00
|
|
|
#include <doctest.h>
|
|
|
|
|
|
|
|
#include <aocpp/DivMod.hpp>
|
|
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using Input = std::vector<std::int64_t>;
|
|
|
|
|
|
|
|
/// @brief Parse the input stream as a newline-delimited list of lists of integers
|
|
|
|
/// @param in input file parsed until EOF
|
|
|
|
/// @return outer list of elves, inner lists of calories
|
|
|
|
auto Parse(std::istream & in) -> Input
|
|
|
|
{
|
2023-04-07 09:45:55 -07:00
|
|
|
auto result = Input{};
|
2023-01-19 21:15:23 -08:00
|
|
|
using It = std::istream_iterator<std::int64_t>;
|
|
|
|
std::copy<It>(in, {}, std::back_inserter(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Solve(Input const& input, std::size_t const rounds) -> std::int64_t
|
|
|
|
{
|
2023-01-25 13:48:54 -08:00
|
|
|
auto const len = input.size();
|
2023-04-07 09:45:55 -07:00
|
|
|
auto fwds = std::vector<std::size_t>(len);
|
|
|
|
auto bwds = std::vector<std::size_t>(len);
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
throw std::runtime_error{"Input too small"};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const zero_it = std::find(input.begin(), input.end(), 0);
|
|
|
|
if (zero_it == input.end()) {
|
|
|
|
throw std::runtime_error{"no zero element in input"};
|
|
|
|
}
|
2023-01-19 21:15:23 -08:00
|
|
|
|
2023-01-24 09:01:42 -08:00
|
|
|
std::iota(fwds.begin(), fwds.end()-1, 1); fwds.back() = 0;
|
|
|
|
std::iota(bwds.begin()+1, bwds.end(), 0); bwds.front() = len-1;
|
2023-01-19 21:15:23 -08:00
|
|
|
|
2023-01-25 13:48:54 -08:00
|
|
|
for (auto const _ : boost::irange(rounds)) {
|
|
|
|
for (auto const i : boost::irange(len)) {
|
2023-01-19 21:15:23 -08:00
|
|
|
|
|
|
|
// Remove i from the ring
|
|
|
|
auto next = fwds[i];
|
|
|
|
auto prev = bwds[i];
|
|
|
|
fwds[prev] = next;
|
|
|
|
bwds[next] = prev;
|
|
|
|
|
|
|
|
auto const steps = aocpp::Mod<std::int64_t>(input[i], len - 1);
|
|
|
|
if (steps < len/2) {
|
2023-01-25 13:48:54 -08:00
|
|
|
for (auto const _ : boost::irange(steps)) {
|
2023-01-19 21:15:23 -08:00
|
|
|
next = fwds[next];
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-25 13:48:54 -08:00
|
|
|
for (auto const _ : boost::irange(len - 1 - steps)) {
|
2023-01-19 21:15:23 -08:00
|
|
|
next = bwds[next];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev = bwds[next];
|
|
|
|
|
|
|
|
// insert i into the ring
|
|
|
|
fwds[i] = next;
|
|
|
|
bwds[i] = prev;
|
|
|
|
fwds[prev] = i;
|
|
|
|
bwds[next] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 09:45:55 -07:00
|
|
|
auto sum = std::int64_t{0};
|
|
|
|
auto cursor = std::distance(input.begin(), zero_it);
|
2023-01-25 13:48:54 -08:00
|
|
|
for (auto const _ : boost::irange(3)) {
|
|
|
|
for (auto const _ : boost::irange(1000)) {
|
2023-01-19 21:15:23 -08:00
|
|
|
cursor = fwds[cursor];
|
|
|
|
}
|
|
|
|
sum += input[cursor];
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Part1(Input const& input) -> std::int64_t
|
|
|
|
{
|
|
|
|
return Solve(input, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Part2(Input input) -> std::int64_t
|
|
|
|
{
|
2023-04-07 09:45:55 -07:00
|
|
|
for (auto& x : input) { x *= 811'589'153; }
|
2023-01-19 21:15:23 -08:00
|
|
|
return Solve(input, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_SUITE("2022-20 examples") {
|
|
|
|
TEST_CASE("example") {
|
|
|
|
std::istringstream in {
|
|
|
|
"1\n"
|
|
|
|
"2\n"
|
|
|
|
"-3\n"
|
|
|
|
"3\n"
|
|
|
|
"-2\n"
|
|
|
|
"0\n"
|
|
|
|
"4\n"};
|
|
|
|
auto input = Parse(in);
|
|
|
|
CHECK(3 == Part1(input));
|
2023-04-07 09:45:55 -07:00
|
|
|
CHECK(1623178306 == Part2(std::move(input)));
|
2023-01-19 21:15:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-04-07 09:45:55 -07:00
|
|
|
auto input = Parse(in);
|
2023-01-31 09:15:15 -08:00
|
|
|
out << "Part 1: " << Part1(input) << std::endl;
|
|
|
|
out << "Part 2: " << Part2(std::move(input)) << std::endl;
|
2023-01-19 21:15:23 -08:00
|
|
|
}
|