Compare commits
2 Commits
5111d633b7
...
7b2567a71e
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b2567a71e | |||
| c669dedbb7 |
@@ -0,0 +1 @@
|
|||||||
|
10000
|
||||||
|
|||||||
+37
-10
@@ -2,30 +2,57 @@
|
|||||||
|
|
||||||
namespace y2016::day16
|
namespace y2016::day16
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2016, Day16, std::vector<int>, int);
|
REGISTER_DAY(2016, Day16, std::string, std::string);
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day16, ExampleInput, 1, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day16, ExampleInput, 1, "01100");
|
||||||
REGISTER_TEST(2016, Day16, Input, 1, 0);
|
REGISTER_TEST(2016, Day16, Input, 1, "10100011010101011");
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day16, ExampleInput, 2, 0);
|
REGISTER_TEST(2016, Day16, Input, 2, "01010001101011001");
|
||||||
REGISTER_TEST(2016, Day16, Input, 2, 0);
|
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
|
||||||
std::string str;
|
std::string str;
|
||||||
while (getline(input, str))
|
getline(input, str);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ReverseFlip(const std::string& str)
|
||||||
|
{
|
||||||
|
std::string reverse;
|
||||||
|
for (int i = str.size() - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
|
reverse.push_back(str[i] == '1' ? '0' : '1');
|
||||||
}
|
}
|
||||||
return vec;
|
return reverse;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Solve(std::string str, int length)
|
||||||
|
{
|
||||||
|
while (str.size() < length)
|
||||||
|
{
|
||||||
|
str = str + "0" + ReverseFlip(str);
|
||||||
|
}
|
||||||
|
str = str.substr(0, length);
|
||||||
|
|
||||||
|
std::string checksum;
|
||||||
|
while (str.size() % 2 == 0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < str.size(); i += 2)
|
||||||
|
{
|
||||||
|
checksum.push_back(str[i] == str[i + 1] ? '1' : '0');
|
||||||
|
}
|
||||||
|
str = checksum;
|
||||||
|
checksum.clear();
|
||||||
|
}
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
return Solve(input, isExample ? 20 : 272);
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
return Solve(input, 35651584);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,249 @@
|
|||||||
|
#include "Helper.h"
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
inline bool Helper::traceErrors = true;
|
||||||
|
|
||||||
|
Helper::ScopedTraceErrorDisabler::ScopedTraceErrorDisabler()
|
||||||
|
{
|
||||||
|
DisableTraceErrors();
|
||||||
|
}
|
||||||
|
|
||||||
|
Helper::ScopedTraceErrorDisabler::~ScopedTraceErrorDisabler()
|
||||||
|
{
|
||||||
|
ResetTraceErrors();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string_view> Helper::Split(const std::string& str, const std::string& delim)
|
||||||
|
{
|
||||||
|
std::vector<std::string_view> split;
|
||||||
|
size_t pos = str.find(delim);
|
||||||
|
size_t lastPos = 0;
|
||||||
|
while (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
split.emplace_back(std::string_view{str.c_str() + lastPos, pos - lastPos});
|
||||||
|
lastPos = pos + delim.size();
|
||||||
|
pos = str.find(delim, pos + delim.size());
|
||||||
|
}
|
||||||
|
split.emplace_back(std::string_view{str.c_str() + lastPos, str.size() - lastPos});
|
||||||
|
return split;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Helper::StartsWith(const std::string_view& str, const std::string& prefix, size_t offset)
|
||||||
|
{
|
||||||
|
return str.substr(offset, prefix.size()) == prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Helper::EndsWith(const std::string_view& str, const std::string& prefix)
|
||||||
|
{
|
||||||
|
return str.substr(str.size() - prefix.size(), prefix.size()) == prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Helper::Replace(std::string& str, size_t size, const std::string& other, size_t pos)
|
||||||
|
{
|
||||||
|
str.replace(pos, size, other);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Helper::IsDigit(char c)
|
||||||
|
{
|
||||||
|
return c >= '0' && c <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
int Helper::GetNumberOfDigits(int n)
|
||||||
|
{
|
||||||
|
assert(n > 0);
|
||||||
|
// clang-format off
|
||||||
|
if (n < 10) return 1;
|
||||||
|
if (n < 100) return 2;
|
||||||
|
if (n < 1'000) return 3;
|
||||||
|
if (n < 10'000) return 4;
|
||||||
|
if (n < 100'000) return 5;
|
||||||
|
if (n < 1'000'000) return 6;
|
||||||
|
if (n < 10'000'000) return 7;
|
||||||
|
if (n < 100'000'000) return 8;
|
||||||
|
if (n < 1'000'000'000) return 9;
|
||||||
|
// clang-format on
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Helper::GetNumberOfDigits(int64_t n)
|
||||||
|
{
|
||||||
|
assert(n > 0);
|
||||||
|
// clang-format off
|
||||||
|
if (n < 10) return 1;
|
||||||
|
if (n < 100) return 2;
|
||||||
|
if (n < 1'000) return 3;
|
||||||
|
if (n < 10'000) return 4;
|
||||||
|
if (n < 100'000) return 5;
|
||||||
|
if (n < 1'000'000) return 6;
|
||||||
|
if (n < 10'000'000) return 7;
|
||||||
|
if (n < 100'000'000) return 8;
|
||||||
|
if (n < 1'000'000'000) return 9;
|
||||||
|
if (n < 10'000'000'000) return 10;
|
||||||
|
if (n < 100'000'000'000) return 11;
|
||||||
|
if (n < 1'000'000'000'000) return 12;
|
||||||
|
if (n < 10'000'000'000'000) return 13;
|
||||||
|
if (n < 100'000'000'000'000) return 14;
|
||||||
|
if (n < 1'000'000'000'000'000) return 15;
|
||||||
|
if (n < 10'000'000'000'000'000) return 16;
|
||||||
|
if (n < 100'000'000'000'000'000) return 17;
|
||||||
|
if (n < 1'000'000'000'000'000'000) return 18;
|
||||||
|
// clang-format on
|
||||||
|
return 19;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t Helper::Pow10(int pow)
|
||||||
|
{
|
||||||
|
int64_t p = 10;
|
||||||
|
for (int i = 1; i < pow; i++)
|
||||||
|
{
|
||||||
|
p *= 10;
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Helper::BinStrToInt(const std::string& str)
|
||||||
|
{
|
||||||
|
if (str.size() >= 32)
|
||||||
|
{
|
||||||
|
std::cout << "BinStrToInt: Too big string, use BinStrToInt64 instead" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int val = 0;
|
||||||
|
for (size_t i = 0; i < str.size(); i++)
|
||||||
|
{
|
||||||
|
if (str[i] == '1')
|
||||||
|
val |= (1 << (str.size() - i - 1));
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t Helper::BinStrToInt64(const std::string& str)
|
||||||
|
{
|
||||||
|
if (str.size() >= 64)
|
||||||
|
{
|
||||||
|
std::cout << "BinStrToInt64: Too big string" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t val = 0;
|
||||||
|
for (size_t i = 0; i < str.size(); i++)
|
||||||
|
{
|
||||||
|
if (str[i] == '1')
|
||||||
|
val |= (1ll << (str.size() - i - 1));
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Helper::Repeat(const std::string& str, int count)
|
||||||
|
{
|
||||||
|
std::string s;
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
s += str;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> Helper::GetAllRegexMatches(const std::string& str, const std::string& regex)
|
||||||
|
{
|
||||||
|
std::vector<std::string> matches;
|
||||||
|
std::regex reg{regex};
|
||||||
|
auto it = std::sregex_iterator(str.begin(), str.end(), reg);
|
||||||
|
auto end = std::sregex_iterator();
|
||||||
|
for (; it != end; it++)
|
||||||
|
{
|
||||||
|
matches.emplace_back(it->str());
|
||||||
|
}
|
||||||
|
return matches;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Index2D> Helper::GetNeighborDirections()
|
||||||
|
{
|
||||||
|
return std::vector<Index2D>{Index2D{1, 0}, Index2D{0, 1}, Index2D{-1, 0}, Index2D{0, -1}};
|
||||||
|
}
|
||||||
|
|
||||||
|
Index2D Helper::GetDirection(char c)
|
||||||
|
{
|
||||||
|
if (c == 'v' || c == 'V')
|
||||||
|
return Index2D{0, 1};
|
||||||
|
if (c == '^')
|
||||||
|
return Index2D{0, -1};
|
||||||
|
if (c == '>')
|
||||||
|
return Index2D{1, 0};
|
||||||
|
if (c == '<')
|
||||||
|
return Index2D{-1, 0};
|
||||||
|
std::cerr << "GetDirection: Invalid char: " << c << std::endl;
|
||||||
|
return Index2D{-1, -1};
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t Helper::ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t remainder1, int64_t remainder2)
|
||||||
|
{
|
||||||
|
int64_t i = remainder1;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (i % mod2 == remainder2)
|
||||||
|
return i;
|
||||||
|
|
||||||
|
i += mod1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t Helper::ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& remainders)
|
||||||
|
{
|
||||||
|
int64_t currentMod = mods.front();
|
||||||
|
int64_t currentRem = remainders.front();
|
||||||
|
for (int i = 1; i < mods.size(); i++)
|
||||||
|
{
|
||||||
|
int64_t result = ChineseRemainderTheoremTwo(currentMod, mods[i], currentRem, remainders[i]);
|
||||||
|
currentRem = result;
|
||||||
|
currentMod = currentMod * mods[i];
|
||||||
|
}
|
||||||
|
return currentRem;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t Helper::ChineseRemainderTheorem(const std::vector<int64_t>& mods,
|
||||||
|
std::vector<int64_t> remainders,
|
||||||
|
const std::vector<int64_t>& starts)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < mods.size(); i++)
|
||||||
|
{
|
||||||
|
remainders[i] = ((remainders[i] - starts[i]) % mods[i] + mods[i]) % mods[i];
|
||||||
|
}
|
||||||
|
return ChineseRemainderTheorem(mods, remainders);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t Helper::FastExponentiation(uint64_t start, uint64_t multiplication, uint64_t power, uint64_t mod)
|
||||||
|
{
|
||||||
|
while (power > 0)
|
||||||
|
{
|
||||||
|
if (power % 2 == 0)
|
||||||
|
{
|
||||||
|
multiplication = (multiplication * multiplication) % mod;
|
||||||
|
power /= 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
start = (start * multiplication) % mod;
|
||||||
|
power--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t Helper::ManhattanDistance(const Index2D& from, const Index2D& to)
|
||||||
|
{
|
||||||
|
return std::abs(from.x - to.x) + std::abs(from.y - to.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Helper::DisableTraceErrors()
|
||||||
|
{
|
||||||
|
traceErrors = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Helper::ResetTraceErrors()
|
||||||
|
{
|
||||||
|
traceErrors = true;
|
||||||
|
}
|
||||||
+23
-230
@@ -9,7 +9,6 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <regex>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -34,15 +33,8 @@ struct Helper
|
|||||||
{
|
{
|
||||||
struct ScopedTraceErrorDisabler
|
struct ScopedTraceErrorDisabler
|
||||||
{
|
{
|
||||||
ScopedTraceErrorDisabler()
|
ScopedTraceErrorDisabler();
|
||||||
{
|
~ScopedTraceErrorDisabler();
|
||||||
DisableTraceErrors();
|
|
||||||
}
|
|
||||||
|
|
||||||
~ScopedTraceErrorDisabler()
|
|
||||||
{
|
|
||||||
ResetTraceErrors();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename S>
|
template <typename T, typename S>
|
||||||
@@ -51,129 +43,21 @@ struct Helper
|
|||||||
t.erase(std::remove_if(t.begin(), t.end(), func), t.end());
|
t.erase(std::remove_if(t.begin(), t.end(), func), t.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<std::string_view> Split(const std::string& str, const std::string& delim)
|
static std::vector<std::string_view> Split(const std::string& str, const std::string& delim);
|
||||||
{
|
static bool StartsWith(const std::string_view& str, const std::string& prefix, size_t offset = 0);
|
||||||
std::vector<std::string_view> split;
|
static bool EndsWith(const std::string_view& str, const std::string& prefix);
|
||||||
size_t pos = str.find(delim);
|
static void Replace(std::string& str, size_t size, const std::string& other, size_t pos);
|
||||||
size_t lastPos = 0;
|
|
||||||
while (pos != std::string::npos)
|
|
||||||
{
|
|
||||||
split.emplace_back(std::string_view{str.c_str() + lastPos, pos - lastPos});
|
|
||||||
lastPos = pos + delim.size();
|
|
||||||
pos = str.find(delim, pos + delim.size());
|
|
||||||
}
|
|
||||||
split.emplace_back(std::string_view{str.c_str() + lastPos, str.size() - lastPos});
|
|
||||||
return split;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool StartsWith(const std::string_view& str, const std::string& prefix, size_t offset = 0)
|
static bool IsDigit(char c);
|
||||||
{
|
static int GetNumberOfDigits(int n);
|
||||||
return str.substr(offset, prefix.size()) == prefix;
|
static int GetNumberOfDigits(int64_t n);
|
||||||
}
|
static int64_t Pow10(int pow);
|
||||||
|
|
||||||
static bool EndsWith(const std::string_view& str, const std::string& prefix)
|
|
||||||
{
|
|
||||||
return str.substr(str.size() - prefix.size(), prefix.size()) == prefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Replace(std::string& str, size_t size, const std::string& other, size_t pos)
|
|
||||||
{
|
|
||||||
str.replace(pos, size, other);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool IsDigit(char c)
|
|
||||||
{
|
|
||||||
return c >= '0' && c <= '9';
|
|
||||||
}
|
|
||||||
|
|
||||||
static int GetNumberOfDigits(int n)
|
|
||||||
{
|
|
||||||
assert(n > 0);
|
|
||||||
// clang-format off
|
|
||||||
if (n < 10) return 1;
|
|
||||||
if (n < 100) return 2;
|
|
||||||
if (n < 1'000) return 3;
|
|
||||||
if (n < 10'000) return 4;
|
|
||||||
if (n < 100'000) return 5;
|
|
||||||
if (n < 1'000'000) return 6;
|
|
||||||
if (n < 10'000'000) return 7;
|
|
||||||
if (n < 100'000'000) return 8;
|
|
||||||
if (n < 1'000'000'000) return 9;
|
|
||||||
// clang-format on
|
|
||||||
return 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int GetNumberOfDigits(int64_t n)
|
|
||||||
{
|
|
||||||
assert(n > 0);
|
|
||||||
// clang-format off
|
|
||||||
if (n < 10) return 1;
|
|
||||||
if (n < 100) return 2;
|
|
||||||
if (n < 1'000) return 3;
|
|
||||||
if (n < 10'000) return 4;
|
|
||||||
if (n < 100'000) return 5;
|
|
||||||
if (n < 1'000'000) return 6;
|
|
||||||
if (n < 10'000'000) return 7;
|
|
||||||
if (n < 100'000'000) return 8;
|
|
||||||
if (n < 1'000'000'000) return 9;
|
|
||||||
if (n < 10'000'000'000) return 10;
|
|
||||||
if (n < 100'000'000'000) return 11;
|
|
||||||
if (n < 1'000'000'000'000) return 12;
|
|
||||||
if (n < 10'000'000'000'000) return 13;
|
|
||||||
if (n < 100'000'000'000'000) return 14;
|
|
||||||
if (n < 1'000'000'000'000'000) return 15;
|
|
||||||
if (n < 10'000'000'000'000'000) return 16;
|
|
||||||
if (n < 100'000'000'000'000'000) return 17;
|
|
||||||
if (n < 1'000'000'000'000'000'000) return 18;
|
|
||||||
// clang-format on
|
|
||||||
return 19;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t Pow10(int pow)
|
|
||||||
{
|
|
||||||
int64_t p = 10;
|
|
||||||
for (int i = 1; i < pow; i++)
|
|
||||||
{
|
|
||||||
p *= 10;
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converts a binary string to an int32_t ie "10100111001" to 1337
|
// Converts a binary string to an int32_t ie "10100111001" to 1337
|
||||||
static int BinStrToInt(const std::string& str)
|
static int BinStrToInt(const std::string& str);
|
||||||
{
|
|
||||||
if (str.size() >= 32)
|
|
||||||
{
|
|
||||||
std::cout << "BinStrToInt: Too big string, use BinStrToInt64 instead" << std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int val = 0;
|
|
||||||
for (size_t i = 0; i < str.size(); i++)
|
|
||||||
{
|
|
||||||
if (str[i] == '1')
|
|
||||||
val |= (1 << (str.size() - i - 1));
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converts a binary string to an int64_t ie "10100111001" to 1337
|
// Converts a binary string to an int64_t ie "10100111001" to 1337
|
||||||
static int64_t BinStrToInt64(const std::string& str)
|
static int64_t BinStrToInt64(const std::string& str);
|
||||||
{
|
|
||||||
if (str.size() >= 64)
|
|
||||||
{
|
|
||||||
std::cout << "BinStrToInt64: Too big string" << std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t val = 0;
|
|
||||||
for (size_t i = 0; i < str.size(); i++)
|
|
||||||
{
|
|
||||||
if (str[i] == '1')
|
|
||||||
val |= (1ll << (str.size() - i - 1));
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename S>
|
template <typename T, typename S>
|
||||||
static T Sum(const S& container)
|
static T Sum(const S& container)
|
||||||
@@ -544,15 +428,7 @@ struct Helper
|
|||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string Repeat(const std::string& str, int count)
|
static std::string Repeat(const std::string& str, int count);
|
||||||
{
|
|
||||||
std::string s;
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
s += str;
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static std::vector<T> Repeat(const std::vector<T>& vec, int count)
|
static std::vector<T> Repeat(const std::vector<T>& vec, int count)
|
||||||
@@ -568,99 +444,27 @@ struct Helper
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<std::string> GetAllRegexMatches(const std::string& str, const std::string& regex)
|
static std::vector<std::string> GetAllRegexMatches(const std::string& str, const std::string& regex);
|
||||||
{
|
|
||||||
std::vector<std::string> matches;
|
|
||||||
std::regex reg{regex};
|
|
||||||
auto it = std::sregex_iterator(str.begin(), str.end(), reg);
|
|
||||||
auto end = std::sregex_iterator();
|
|
||||||
for (; it != end; it++)
|
|
||||||
{
|
|
||||||
matches.emplace_back(it->str());
|
|
||||||
}
|
|
||||||
return matches;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::vector<Index2D> GetNeighborDirections()
|
static std::vector<Index2D> GetNeighborDirections();
|
||||||
{
|
static Index2D GetDirection(char c);
|
||||||
return std::vector<Index2D>{Index2D{1, 0}, Index2D{0, 1}, Index2D{-1, 0}, Index2D{0, -1}};
|
|
||||||
}
|
|
||||||
|
|
||||||
static Index2D GetDirection(char c)
|
|
||||||
{
|
|
||||||
if (c == 'v' || c == 'V')
|
|
||||||
return Index2D{0, 1};
|
|
||||||
if (c == '^')
|
|
||||||
return Index2D{0, -1};
|
|
||||||
if (c == '>')
|
|
||||||
return Index2D{1, 0};
|
|
||||||
if (c == '<')
|
|
||||||
return Index2D{-1, 0};
|
|
||||||
std::cerr << "GetDirection: Invalid char: " << c << std::endl;
|
|
||||||
return Index2D{-1, -1};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Solve for N:
|
// Solve for N:
|
||||||
// remainder1 = N % mod1
|
// remainder1 = N % mod1
|
||||||
// remainder2 = N % mod2
|
// remainder2 = N % mod2
|
||||||
static int64_t ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t remainder1, int64_t remainder2)
|
static int64_t ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t remainder1, int64_t remainder2);
|
||||||
{
|
|
||||||
int64_t i = remainder1;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (i % mod2 == remainder2)
|
|
||||||
return i;
|
|
||||||
|
|
||||||
i += mod1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Solve for N:
|
// Solve for N:
|
||||||
// remainders = N % mods
|
// remainders = N % mods
|
||||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& remainders)
|
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& remainders);
|
||||||
{
|
|
||||||
int64_t currentMod = mods.front();
|
|
||||||
int64_t currentRem = remainders.front();
|
|
||||||
for (int i = 1; i < mods.size(); i++)
|
|
||||||
{
|
|
||||||
int64_t result = ChineseRemainderTheoremTwo(currentMod, mods[i], currentRem, remainders[i]);
|
|
||||||
currentRem = result;
|
|
||||||
currentMod = currentMod * mods[i];
|
|
||||||
}
|
|
||||||
return currentRem;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Solve for N:
|
// Solve for N:
|
||||||
// remainders = (start + N) % mods
|
// remainders = (start + N) % mods
|
||||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods,
|
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods,
|
||||||
std::vector<int64_t> remainders,
|
std::vector<int64_t> remainders,
|
||||||
const std::vector<int64_t>& starts)
|
const std::vector<int64_t>& starts);
|
||||||
{
|
|
||||||
for (int i = 0; i < mods.size(); i++)
|
|
||||||
{
|
|
||||||
remainders[i] = ((remainders[i] - starts[i]) % mods[i] + mods[i]) % mods[i];
|
|
||||||
}
|
|
||||||
return ChineseRemainderTheorem(mods, remainders);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64_t FastExponentiation(uint64_t start, uint64_t multiplication, uint64_t power, uint64_t mod)
|
static uint64_t FastExponentiation(uint64_t start, uint64_t multiplication, uint64_t power, uint64_t mod);
|
||||||
{
|
|
||||||
while (power > 0)
|
|
||||||
{
|
|
||||||
if (power % 2 == 0)
|
|
||||||
{
|
|
||||||
multiplication = (multiplication * multiplication) % mod;
|
|
||||||
power /= 2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
start = (start * multiplication) % mod;
|
|
||||||
power--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return start;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Input - Puzzle input
|
// Input - Puzzle input
|
||||||
// Function - bool(int64_t);
|
// Function - bool(int64_t);
|
||||||
@@ -687,20 +491,11 @@ struct Helper
|
|||||||
return BinarySearch(input, min, i - 1, func);
|
return BinarySearch(input, min, i - 1, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t ManhattanDistance(const Index2D& from, const Index2D& to)
|
static int64_t ManhattanDistance(const Index2D& from, const Index2D& to);
|
||||||
{
|
|
||||||
return std::abs(from.x - to.x) + std::abs(from.y - to.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void DisableTraceErrors()
|
static void DisableTraceErrors();
|
||||||
{
|
|
||||||
traceErrors = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ResetTraceErrors()
|
static void ResetTraceErrors();
|
||||||
{
|
|
||||||
traceErrors = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename Key, typename Value, typename Compare, typename Eval>
|
template <typename Key, typename Value, typename Compare, typename Eval>
|
||||||
@@ -763,8 +558,6 @@ public:
|
|||||||
static bool traceErrors;
|
static bool traceErrors;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool Helper::traceErrors = true;
|
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container)
|
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user