This commit is contained in:
Eric Mertens 2022-11-16 09:18:17 -08:00
parent 91d9f6e340
commit dc817bbc9e

View File

@ -1,7 +1,10 @@
#include <cstdint>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <sstream>
#include <vector> #include <vector>
#include <numeric>
#include <doctest.h>
#include <aocpp/Startup.hpp> #include <aocpp/Startup.hpp>
using namespace aocpp; using namespace aocpp;
@ -34,44 +37,92 @@ auto FFT(std::size_t const start, std::vector<std::int8_t> const& prev)
return result; return result;
} }
} // namespace auto Parse(std::istream & in) -> std::vector<std::int8_t>
{
auto main(int argc, char** argv) -> int { std::string line;
auto & in = aocpp::Startup(argc, argv); std::getline(in, line);
std::string input; std::vector<std::int8_t> result;
in >> input; result.reserve(line.size());
for (auto const c : line) {
std::vector<std::int8_t> signal; result.push_back(c - '0');
for (char const c : input) {
signal.push_back(c - '0');
} }
return result;
}
for (int i = 0; i < 100; i++) { auto Compute(
signal = FFT(0, signal); std::vector<std::int8_t> signal,
} std::size_t iterations,
for (int i = 0; i < 8; i++) { std::size_t offset = 0
std::cout << int(signal[i]); ) -> std::string
} {
std::cout << std::endl; for (std::size_t i = 0; i < iterations; i++) {
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); signal = FFT(offset, signal);
} }
signal.resize(8);
for (auto i = 0; i < 8; i++) { std::string result;
std::cout << int(signal[i]); for (auto const x : signal) {
result += char(x + '0');
} }
std::cout << std::endl; return result;
} }
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];
}
// 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);
// 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");
}
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");
}
}
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;
}