tests16
This commit is contained in:
parent
91d9f6e340
commit
dc817bbc9e
125
2019/16.cpp
125
2019/16.cpp
|
@ -1,7 +1,10 @@
|
|||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
using namespace aocpp;
|
||||
|
@ -34,44 +37,92 @@ auto FFT(std::size_t const start, std::vector<std::int8_t> const& prev)
|
|||
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');
|
||||
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');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
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++) {
|
||||
auto Compute(
|
||||
std::vector<std::int8_t> signal,
|
||||
std::size_t iterations,
|
||||
std::size_t offset = 0
|
||||
) -> std::string
|
||||
{
|
||||
for (std::size_t i = 0; i < iterations; i++) {
|
||||
signal = FFT(offset, signal);
|
||||
}
|
||||
|
||||
for (auto i = 0; i < 8; i++) {
|
||||
std::cout << int(signal[i]);
|
||||
signal.resize(8);
|
||||
std::string result;
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user