From 02087088f95e93b0689fb7fd077f59a87a48ea57 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 16 Dec 2021 11:50:49 +0000 Subject: [PATCH] Add 2017 day 15 puzzles. --- 2017/puzzle-15-01.cc | 48 ++++++++++++++++++++++++++++++++++ 2017/puzzle-15-02.cc | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 2017/puzzle-15-01.cc create mode 100644 2017/puzzle-15-02.cc diff --git a/2017/puzzle-15-01.cc b/2017/puzzle-15-01.cc new file mode 100644 index 0000000..cf9a3c2 --- /dev/null +++ b/2017/puzzle-15-01.cc @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +using UInt = std::uint32_t; + +auto lcg(UInt prev, UInt factor) -> UInt +{ + auto t{static_cast(prev) * static_cast(factor)}; + return static_cast(t % 2147483647ULL); +} + +auto lcgA(UInt prev) -> UInt { return lcg(prev, 16807); } +auto lcgB(UInt prev) -> UInt { return lcg(prev, 48271); } + +auto main() -> int +{ + static std::regex a_re{"Generator A starts with (\\d+)"}; + static std::regex b_re{"Generator B starts with (\\d+)"}; + UInt prevA{0}; + UInt prevB{0}; + std::string line; + while (std::getline(std::cin, line) && !line.empty()) { + std::smatch m; + if (std::regex_search(line, m, a_re)) { + prevA = std::stoul(m.str(1)); + } + else if (std::regex_search(line, m, b_re)) { + prevB = std::stoul(m.str(1)); + } + else { + std::cerr << "Unable to interpret: " << line << '\n'; + return 1; + } + } + + UInt judged{0}; + for (UInt i = 0; i < 40'000'000; ++i) { + prevA = lcgA(prevA); + prevB = lcgB(prevB); + if (((prevA ^ prevB) & 0xffffULL) == 0) { + ++judged; + } + } + + std::cout << "Matched " << judged << '\n'; +} diff --git a/2017/puzzle-15-02.cc b/2017/puzzle-15-02.cc new file mode 100644 index 0000000..42778dd --- /dev/null +++ b/2017/puzzle-15-02.cc @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +using UInt = std::uint32_t; + +auto lcg(UInt prev, UInt factor) -> UInt +{ + auto t{static_cast(prev) * static_cast(factor)}; + return static_cast(t % 2147483647ULL); +} + +auto lcgA(UInt prev) -> UInt +{ + do { + prev = lcg(prev, 16807); + } while (prev % 4 != 0); + return prev; +} + +auto lcgB(UInt prev) -> UInt +{ + do { + prev = lcg(prev, 48271); + } while (prev % 8 != 0); + return prev; +} + +auto main() -> int +{ + static std::regex a_re{"Generator A starts with (\\d+)"}; + static std::regex b_re{"Generator B starts with (\\d+)"}; + UInt prevA{0}; + UInt prevB{0}; + std::string line; + while (std::getline(std::cin, line) && !line.empty()) { + std::smatch m; + if (std::regex_search(line, m, a_re)) { + prevA = std::stoul(m.str(1)); + } + else if (std::regex_search(line, m, b_re)) { + prevB = std::stoul(m.str(1)); + } + else { + std::cerr << "Unable to interpret: " << line << '\n'; + return 1; + } + } + + UInt judged{0}; + for (UInt i = 0; i < 5'000'000; ++i) { + prevA = lcgA(prevA); + prevB = lcgB(prevB); + if (((prevA ^ prevB) & 0xffffULL) == 0) { + ++judged; + } + } + + std::cout << "Matched " << judged << '\n'; +}