aocpp/2019/16.cpp

127 lines
3.4 KiB
C++
Raw Normal View History

2022-11-16 09:18:17 -08:00
#include <cstdint>
2022-11-15 20:01:43 -08:00
#include <iostream>
#include <optional>
2022-11-16 09:18:17 -08:00
#include <sstream>
2022-11-15 20:01:43 -08:00
#include <vector>
2022-11-16 09:18:17 -08:00
#include <doctest.h>
2022-11-15 20:01:43 -08:00
#include <aocpp/Startup.hpp>
using namespace aocpp;
namespace {
auto FFT(std::size_t const start, std::vector<std::int32_t> & partial_sums, std::vector<std::int8_t> & signal)
2022-11-15 20:01:43 -08:00
{
auto n = signal.size();
partial_sums.clear();
2022-11-15 20:01:43 -08:00
partial_sums.reserve(n+1);
partial_sums.push_back(0);
std::int32_t acc = 0;
for (auto const x : signal) {
2022-11-15 20:01:43 -08:00
partial_sums.push_back(acc += x);
}
2022-11-15 21:35:20 -08:00
for (std::size_t i = 0; i < n; i++) {
2022-11-15 20:01:43 -08:00
std::int32_t acc = 0;
2022-11-15 21:35:20 -08:00
auto step = i+start+1;
for (std::size_t offset = i; offset < n; offset += 4*step) {
acc += partial_sums[std::min(n, offset + 1*step)] - partial_sums[std::min(n, offset + 0*step)]
+ partial_sums[std::min(n, offset + 2*step)] - partial_sums[std::min(n, offset + 3*step)];
2022-11-15 20:01:43 -08:00
}
signal[i] = std::abs(acc % 10);
2022-11-15 20:01:43 -08:00
}
}
2022-11-16 09:18:17 -08:00
auto Parse(std::istream & in) -> std::vector<std::int8_t>
{
std::string line;
std::getline(in, line);
std::vector<std::int8_t> result;
result.reserve(line.size());
for (auto const c : line) {
result.push_back(c - '0');
2022-11-15 20:01:43 -08:00
}
2022-11-16 09:18:17 -08:00
return result;
}
2022-11-15 20:01:43 -08:00
2022-11-16 09:18:17 -08:00
auto Compute(
std::vector<std::int8_t> signal,
std::size_t iterations,
std::size_t offset = 0
) -> std::string
{
std::vector<std::int32_t> partial_sums;
2022-11-16 09:18:17 -08:00
for (std::size_t i = 0; i < iterations; i++) {
FFT(offset, partial_sums, signal);
2022-11-15 20:01:43 -08:00
}
2022-11-16 09:18:17 -08:00
signal.resize(8);
std::string result;
for (auto const x : signal) {
result += char(x + '0');
2022-11-15 20:01:43 -08:00
}
2022-11-16 09:18:17 -08:00
return result;
}
2022-11-15 20:01:43 -08:00
2022-11-16 09:18:17 -08:00
auto Part2(std::vector<int8_t> const& input) -> std::string {
// Compute offset from first 7 digits of the input
std::size_t offset = 0;
for (int i = 0; i < 7; i++) {
offset = offset * 10 + input[i];
2022-11-15 20:01:43 -08:00
}
2022-11-16 09:18:17 -08:00
// Allocate the storage for 10,000 copies of the input starting at the given offset
std::vector<std::int8_t> signal;
std::size_t size2 = 10000 * input.size() - offset;
signal.reserve(size2);
2022-11-15 20:01:43 -08:00
2022-11-16 09:18:17 -08:00
// Populate the massively copied signal vector
for (std::size_t i = 0, j = offset % input.size(); i < size2; i++) {
signal.push_back(input[j++]);
if (j == input.size()) j = 0;
}
return Compute(signal, 100, offset);
}
} // namespace
TEST_SUITE("documented examples") {
std::istringstream in;
TEST_CASE("part 1") {
in = std::istringstream{"12345678"};
auto signal = Parse(in);
REQUIRE(Compute(signal, 1) == "48226158");
REQUIRE(Compute(signal, 2) == "34040438");
REQUIRE(Compute(signal, 3) == "03415518");
REQUIRE(Compute(signal, 4) == "01029498");
in = std::istringstream{"80871224585914546619083218645595"};
REQUIRE(Compute(Parse(in), 100) == "24176176");
in = std::istringstream{"19617804207202209144916044189917"};
REQUIRE(Compute(Parse(in), 100) == "73745418");
in = std::istringstream{"69317163492948606335995924319873"};
REQUIRE(Compute(Parse(in), 100) == "52432133");
2022-11-15 20:01:43 -08:00
}
2022-11-16 09:18:17 -08:00
TEST_CASE("part 2") {
in = std::istringstream{"03036732577212944063491565474664"};
REQUIRE(Part2(Parse(in)) == "84462026");
in = std::istringstream{"02935109699940807407585447034323"};
REQUIRE(Part2(Parse(in)) == "78725270");
in = std::istringstream{"03081770884921959731165446850517"};
REQUIRE(Part2(Parse(in)) == "53553731");
2022-11-15 20:01:43 -08:00
}
2022-11-16 09:18:17 -08:00
}
auto main(int argc, char** argv) -> int {
auto input = Parse(aocpp::Startup(argc, argv));
std::cout << "Part 1: " << Compute(input, 100, 0) << std::endl;
std::cout << "Part 2: " << Part2(input) << std::endl;
}