aocpp/2019/16.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

2022-11-15 20:01:43 -08:00
#include <iostream>
#include <optional>
#include <vector>
#include <numeric>
#include <aocpp/Startup.hpp>
using namespace aocpp;
namespace {
2022-11-15 21:35:20 -08:00
auto FFT(std::size_t const start, std::vector<std::int8_t> const& prev)
2022-11-15 20:01:43 -08:00
{
std::vector<std::int8_t> result;
std::vector<std::int32_t> partial_sums;
auto n = prev.size();
2022-11-15 21:35:20 -08:00
result.reserve(n);
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 : prev) {
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 )];
acc -= partial_sums[std::min(n, offset + 3*step)] - partial_sums[std::min(n, offset + 2*step)];
2022-11-15 20:01:43 -08:00
}
2022-11-15 21:35:20 -08:00
result.push_back(std::abs(acc % 10));
2022-11-15 20:01:43 -08:00
}
return result;
}
} // namespace
auto main(int argc, char** argv) -> int {
auto & in = aocpp::Startup(argc, argv);
std::string input;
in >> input;
std::vector<std::int8_t> signal;
for (char const c : input) {
signal.push_back(c - '0');
}
for (int i = 0; i < 100; i++) {
signal = FFT(0, signal);
}
for (int i = 0; i < 8; i++) {
std::cout << int(signal[i]);
}
std::cout << std::endl;
signal.clear();
signal.reserve(10000 * input.size());
for (int i = 0; i < 10000; i++) {
for (char const c : input) {
signal.push_back(c - '0');
}
}
auto offset = std::stol(input.substr(0,7));
std::copy(signal.begin() + offset, signal.end(), signal.begin());
signal.resize(signal.size() - offset);
for (int i = 0; i < 100; i++) {
signal = FFT(offset, signal);
}
for (auto i = 0; i < 8; i++) {
std::cout << int(signal[i]);
}
std::cout << std::endl;
}