This commit is contained in:
Eric Mertens 2022-11-15 21:35:20 -08:00
parent ede4232338
commit 91d9f6e340

View File

@ -8,12 +8,12 @@ using namespace aocpp;
namespace { namespace {
auto FFT(std::size_t start, std::vector<std::int8_t> const& prev) auto FFT(std::size_t const start, std::vector<std::int8_t> const& prev)
{ {
std::vector<std::int8_t> result; std::vector<std::int8_t> result;
std::vector<std::int32_t> partial_sums; std::vector<std::int32_t> partial_sums;
auto n = prev.size(); auto n = prev.size();
result.resize(n); result.reserve(n);
partial_sums.reserve(n+1); partial_sums.reserve(n+1);
partial_sums.push_back(0); partial_sums.push_back(0);
@ -22,14 +22,14 @@ auto FFT(std::size_t start, std::vector<std::int8_t> const& prev)
partial_sums.push_back(acc += x); partial_sums.push_back(acc += x);
} }
for (std::size_t i = start; i < n+start; i++) { for (std::size_t i = 0; i < n; i++) {
std::int32_t acc = 0; std::int32_t acc = 0;
for (std::size_t offset = i; offset < n+start; offset += 4*(i+1)) { auto step = i+start+1;
acc += partial_sums[std::min(n,offset-start + i + 1)] - partial_sums[std::min(n, offset-start)]; for (std::size_t offset = i; offset < n; offset += 4*step) {
acc -= partial_sums[std::min(n, offset-start + 3*(i + 1))] - partial_sums[std::min(n, offset-start + 2*(i + 1))]; 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)];
} }
result.push_back(std::abs(acc % 10));
result[i-start] = std::abs(acc % 10);
} }
return result; return result;
} }