Add 2017 day 15 puzzles.

This commit is contained in:
2021-12-16 11:50:49 +00:00
parent 3631a4bb18
commit 02087088f9
2 changed files with 109 additions and 0 deletions

48
2017/puzzle-15-01.cc Normal file
View File

@@ -0,0 +1,48 @@
#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
using UInt = std::uint32_t;
auto lcg(UInt prev, UInt factor) -> UInt
{
auto t{static_cast<std::uint64_t>(prev) * static_cast<std::uint64_t>(factor)};
return static_cast<UInt>(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';
}

61
2017/puzzle-15-02.cc Normal file
View File

@@ -0,0 +1,61 @@
#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
using UInt = std::uint32_t;
auto lcg(UInt prev, UInt factor) -> UInt
{
auto t{static_cast<std::uint64_t>(prev) * static_cast<std::uint64_t>(factor)};
return static_cast<UInt>(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';
}