diff --git a/2019/16.cpp b/2019/16.cpp new file mode 100644 index 0000000..30634a5 --- /dev/null +++ b/2019/16.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#include +using namespace aocpp; + +namespace { + +auto FFT(std::size_t start, std::vector const& prev) +{ + std::vector result; + std::vector partial_sums; + auto n = prev.size(); + result.resize(n); + 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); + } + + for (std::size_t i = start; i < n+start; i++) { + std::int32_t acc = 0; + for (std::size_t offset = i; offset < n+start; offset += 4*(i+1)) { + acc += partial_sums[std::min(n,offset-start + i + 1)] - partial_sums[std::min(n, offset-start)]; + acc -= partial_sums[std::min(n, offset-start + 3*(i + 1))] - partial_sums[std::min(n, offset-start + 2*(i + 1))]; + } + + result[i-start] = std::abs(acc % 10); + } + return result; +} + +} // namespace + +auto main(int argc, char** argv) -> int { + auto & in = aocpp::Startup(argc, argv); + std::string input; + in >> input; + + std::vector 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; +} \ No newline at end of file diff --git a/2019/CMakeLists.txt b/2019/CMakeLists.txt index d453df4..22f112d 100644 --- a/2019/CMakeLists.txt +++ b/2019/CMakeLists.txt @@ -43,6 +43,9 @@ target_link_libraries(14 aocpp) add_executable(15 15.cpp) target_link_libraries(15 aocpp intcode) +add_executable(16 16.cpp) +target_link_libraries(16 aocpp) + add_executable(17 17.cpp) target_link_libraries(17 aocpp intcode)