Compare commits
6 Commits
988c5552d0
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 85f5de1be2 | |||
| 8162a46091 | |||
| ed11090cc2 | |||
| 7b2567a71e | |||
| c669dedbb7 | |||
| 5111d633b7 |
@@ -0,0 +1,3 @@
|
||||
Disc #1 has 5 positions; at time=0, it is at position 4.
|
||||
Disc #2 has 2 positions; at time=0, it is at position 1.
|
||||
Disc #2 has 3 positions; at time=0, it is at position 2.
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
10000
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ulqzkmiv
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
.^^.^.^^^^
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
5
|
||||
|
||||
+35
-8
@@ -2,30 +2,57 @@
|
||||
|
||||
namespace y2016::day15
|
||||
{
|
||||
REGISTER_DAY(2016, Day15, std::vector<int>, int);
|
||||
struct Disc
|
||||
{
|
||||
int positions;
|
||||
int position;
|
||||
};
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day15, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day15, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day15, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day15, Input, 2, 0);
|
||||
REGISTER_DAY(2016, Day15, std::vector<Disc>, int64_t);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day15, ExampleInput, 1, 25);
|
||||
REGISTER_TEST(2016, Day15, Input, 1, 400589);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day15, ExampleInput, 2, 205);
|
||||
REGISTER_TEST(2016, Day15, Input, 2, 3045959);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::vector<Disc> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
std::stringstream ss{str};
|
||||
int i;
|
||||
Disc disc;
|
||||
ss >> "Disc #" >> i >> "has " >> disc.positions >> "positions; at time=" >> i >> ", it is at position " >>
|
||||
disc.position;
|
||||
vec.emplace_back(disc);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
int64_t Solve(const std::vector<Disc>& discs)
|
||||
{
|
||||
std::vector<int64_t> starts;
|
||||
std::vector<int64_t> mods;
|
||||
std::vector<int64_t> remainders(discs.size(), 0);
|
||||
for (int j = 0; j < discs.size(); j++)
|
||||
{
|
||||
starts.emplace_back(discs[j].position + j + 1);
|
||||
mods.emplace_back(discs[j].positions);
|
||||
}
|
||||
return Helper::ChineseRemainderTheorem(mods, remainders, starts);
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input);
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
auto discs = input;
|
||||
discs.emplace_back(Disc{11, 0});
|
||||
return Solve(discs);
|
||||
}
|
||||
}
|
||||
|
||||
+38
-11
@@ -2,30 +2,57 @@
|
||||
|
||||
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(2016, Day16, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day16, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day16, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day16, ExampleInput, 1, "01100");
|
||||
REGISTER_TEST(2016, Day16, Input, 1, "10100011010101011");
|
||||
REGISTER_TEST(2016, Day16, Input, 2, "01010001101011001");
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
getline(input, str);
|
||||
return str;
|
||||
}
|
||||
return vec;
|
||||
|
||||
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 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)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, isExample ? 20 : 272);
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, 35651584);
|
||||
}
|
||||
}
|
||||
|
||||
+64
-10
@@ -2,30 +2,84 @@
|
||||
|
||||
namespace y2016::day17
|
||||
{
|
||||
REGISTER_DAY(2016, Day17, std::vector<int>, int);
|
||||
REGISTER_DAY(2016, Day17, std::string, std::string);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day17, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day17, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 1, "DRURDRUDDLLDLUURRDULRLDUUDDDRR");
|
||||
REGISTER_TEST(2016, Day17, Input, 1, "DUDDRLRRRD");
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 2, "830");
|
||||
REGISTER_TEST(2016, Day17, Input, 2, "578");
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
getline(input, str);
|
||||
return str;
|
||||
}
|
||||
return vec;
|
||||
|
||||
struct State
|
||||
{
|
||||
std::string str{""};
|
||||
int x{0};
|
||||
int y{0};
|
||||
};
|
||||
|
||||
bool IsOpen(char c)
|
||||
{
|
||||
return c >= 'b' && c <= 'f';
|
||||
}
|
||||
|
||||
void Branch(const State& state, std::queue<State>& queue)
|
||||
{
|
||||
std::string hash = Md5::Hash(state.str);
|
||||
if (state.y > 0 && IsOpen(hash[0]))
|
||||
queue.emplace(State{state.str + "U", state.x, state.y - 1});
|
||||
if (state.y < 3 && IsOpen(hash[1]))
|
||||
queue.emplace(State{state.str + "D", state.x, state.y + 1});
|
||||
if (state.x > 0 && IsOpen(hash[2]))
|
||||
queue.emplace(State{state.str + "L", state.x - 1, state.y});
|
||||
if (state.x < 3 && IsOpen(hash[3]))
|
||||
queue.emplace(State{state.str + "R", state.x + 1, state.y});
|
||||
}
|
||||
|
||||
bool IsGoal(const State& state)
|
||||
{
|
||||
return state.x == 3 && state.y == 3;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
std::queue<State> queue;
|
||||
queue.emplace(State{input, 0, 0});
|
||||
while (!queue.empty())
|
||||
{
|
||||
State state = queue.front();
|
||||
queue.pop();
|
||||
if (IsGoal(state))
|
||||
return state.str.substr(input.size());
|
||||
Branch(state, queue);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
std::queue<State> queue;
|
||||
queue.emplace(State{input, 0, 0});
|
||||
int longest = 0;
|
||||
while (!queue.empty())
|
||||
{
|
||||
State state = queue.front();
|
||||
queue.pop();
|
||||
if (IsGoal(state))
|
||||
{
|
||||
longest = state.str.size() - input.size();
|
||||
continue;
|
||||
}
|
||||
Branch(state, queue);
|
||||
}
|
||||
|
||||
return std::to_string(longest);
|
||||
}
|
||||
}
|
||||
|
||||
+37
-11
@@ -2,30 +2,56 @@
|
||||
|
||||
namespace y2016::day18
|
||||
{
|
||||
REGISTER_DAY(2016, Day18, std::vector<int>, int);
|
||||
REGISTER_DAY(2016, Day18, std::string, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day18, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day18, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 1, 38);
|
||||
REGISTER_TEST(2016, Day18, Input, 1, 1913);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 2, 1935478);
|
||||
REGISTER_TEST(2016, Day18, Input, 2, 19993564);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
getline(input, str);
|
||||
return str;
|
||||
}
|
||||
return vec;
|
||||
|
||||
int Solve(const std::string& str, int rows)
|
||||
{
|
||||
int safes = std::count(str.begin(), str.end(), '.');
|
||||
std::vector<char> row(str.size() + 2, false);
|
||||
for (int i = 0; i < str.size(); i++)
|
||||
row[i + 1] = str[i] == '^';
|
||||
|
||||
for (int i = 1; i < rows; i++)
|
||||
{
|
||||
std::vector<char> newRow(row.size(), false);
|
||||
for (int j = 1; j < newRow.size() - 1; j++)
|
||||
{
|
||||
bool isTrap = false;
|
||||
if (row[j - 1] && row[j] && !row[j + 1])
|
||||
newRow[j] = true;
|
||||
else if (!row[j - 1] && row[j] && row[j + 1])
|
||||
newRow[j] = true;
|
||||
else if (row[j - 1] && !row[j] && !row[j + 1])
|
||||
newRow[j] = true;
|
||||
else if (!row[j - 1] && !row[j] && row[j + 1])
|
||||
newRow[j] = true;
|
||||
else
|
||||
safes++;
|
||||
}
|
||||
row = newRow;
|
||||
}
|
||||
return safes;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, isExample ? 10 : 40);
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, 400000);
|
||||
}
|
||||
}
|
||||
|
||||
+51
-13
@@ -2,30 +2,68 @@
|
||||
|
||||
namespace y2016::day19
|
||||
{
|
||||
REGISTER_DAY(2016, Day19, std::vector<int>, int);
|
||||
REGISTER_DAY(2016, Day19, int, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day19, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day19, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 1, 3);
|
||||
REGISTER_TEST(2016, Day19, Input, 1, 1842613);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 2, 2);
|
||||
REGISTER_TEST(2016, Day19, Input, 2, 1424135);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
}
|
||||
return vec;
|
||||
int i;
|
||||
input >> i;
|
||||
return i;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
std::vector<int> elves(input, 0);
|
||||
for (int i = 0; i < input; i++)
|
||||
{
|
||||
elves[i] = i + 1;
|
||||
}
|
||||
int offset = 0;
|
||||
while (elves.size() > 1)
|
||||
{
|
||||
std::vector<int> newElves;
|
||||
for (int i = offset; i < elves.size(); i += 2)
|
||||
{
|
||||
newElves.emplace_back(elves[i]);
|
||||
}
|
||||
offset = (offset + (elves.size() % 2)) % 2;
|
||||
elves = newElves;
|
||||
}
|
||||
return elves[0];
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
std::deque<int> start;
|
||||
std::deque<int> end;
|
||||
for (int i = 0; i < input / 2; i++)
|
||||
{
|
||||
start.push_back(i + 1);
|
||||
}
|
||||
for (int i = input / 2; i < input; i++)
|
||||
{
|
||||
end.push_back(i + 1);
|
||||
}
|
||||
|
||||
while (!start.empty())
|
||||
{
|
||||
end.pop_front();
|
||||
|
||||
end.push_back(start.front());
|
||||
start.pop_front();
|
||||
|
||||
if (end.size() - start.size() == 2)
|
||||
{
|
||||
start.push_back(end.front());
|
||||
end.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
return end.front();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
+42
-221
@@ -4,12 +4,12 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <regex>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
@@ -21,6 +21,9 @@
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container);
|
||||
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::deque<Args...>& container);
|
||||
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::set<Args...>& container);
|
||||
|
||||
@@ -34,15 +37,8 @@ struct Helper
|
||||
{
|
||||
struct ScopedTraceErrorDisabler
|
||||
{
|
||||
ScopedTraceErrorDisabler()
|
||||
{
|
||||
DisableTraceErrors();
|
||||
}
|
||||
|
||||
~ScopedTraceErrorDisabler()
|
||||
{
|
||||
ResetTraceErrors();
|
||||
}
|
||||
ScopedTraceErrorDisabler();
|
||||
~ScopedTraceErrorDisabler();
|
||||
};
|
||||
|
||||
template <typename T, typename S>
|
||||
@@ -51,129 +47,21 @@ struct Helper
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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);
|
||||
static bool EndsWith(const std::string_view& str, const std::string& prefix);
|
||||
static void Replace(std::string& str, size_t size, const std::string& other, size_t pos);
|
||||
|
||||
static bool StartsWith(const std::string_view& str, const std::string& prefix, size_t offset = 0)
|
||||
{
|
||||
return str.substr(offset, prefix.size()) == prefix;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
static bool IsDigit(char c);
|
||||
static int GetNumberOfDigits(int n);
|
||||
static int GetNumberOfDigits(int64_t n);
|
||||
static int64_t Pow10(int pow);
|
||||
|
||||
// Converts a binary string to an int32_t ie "10100111001" to 1337
|
||||
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;
|
||||
}
|
||||
static int BinStrToInt(const std::string& str);
|
||||
|
||||
// Converts a binary string to an int64_t ie "10100111001" to 1337
|
||||
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;
|
||||
}
|
||||
static int64_t BinStrToInt64(const std::string& str);
|
||||
|
||||
template <typename T, typename S>
|
||||
static T Sum(const S& container)
|
||||
@@ -544,15 +432,7 @@ struct Helper
|
||||
return stream;
|
||||
}
|
||||
|
||||
static std::string Repeat(const std::string& str, int count)
|
||||
{
|
||||
std::string s;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
s += str;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
static std::string Repeat(const std::string& str, int count);
|
||||
|
||||
template <typename T>
|
||||
static std::vector<T> Repeat(const std::vector<T>& vec, int count)
|
||||
@@ -568,81 +448,27 @@ struct Helper
|
||||
return ret;
|
||||
}
|
||||
|
||||
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<std::string> GetAllRegexMatches(const std::string& str, const std::string& regex);
|
||||
|
||||
static std::vector<Index2D> GetNeighborDirections()
|
||||
{
|
||||
return std::vector<Index2D>{Index2D{1, 0}, Index2D{0, 1}, Index2D{-1, 0}, Index2D{0, -1}};
|
||||
}
|
||||
static std::vector<Index2D> GetNeighborDirections();
|
||||
static Index2D GetDirection(char c);
|
||||
|
||||
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:
|
||||
// remainder1 = N % mod1
|
||||
// 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 res1, int64_t res2)
|
||||
{
|
||||
int64_t i = res1;
|
||||
while (true)
|
||||
{
|
||||
if (i % mod2 == res2)
|
||||
return i;
|
||||
// Solve for N:
|
||||
// remainders = N % mods
|
||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& remainders);
|
||||
|
||||
i += mod1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// Solve for N:
|
||||
// remainders = (start + N) % mods
|
||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods,
|
||||
std::vector<int64_t> remainders,
|
||||
const std::vector<int64_t>& starts);
|
||||
|
||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& results)
|
||||
{
|
||||
int64_t currentMod = mods.front();
|
||||
int64_t currentRes = results.front();
|
||||
for (int i = 1; i < mods.size(); i++)
|
||||
{
|
||||
int64_t result = ChineseRemainderTheoremTwo(currentMod, mods[i], currentRes, results[i]);
|
||||
currentRes = result;
|
||||
currentMod = currentMod * mods[i];
|
||||
}
|
||||
return currentRes;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
static uint64_t FastExponentiation(uint64_t start, uint64_t multiplication, uint64_t power, uint64_t mod);
|
||||
|
||||
// Input - Puzzle input
|
||||
// Function - bool(int64_t);
|
||||
@@ -669,20 +495,11 @@ struct Helper
|
||||
return BinarySearch(input, min, i - 1, func);
|
||||
}
|
||||
|
||||
static int64_t ManhattanDistance(const Index2D& from, const Index2D& to)
|
||||
{
|
||||
return std::abs(from.x - to.x) + std::abs(from.y - to.y);
|
||||
}
|
||||
static int64_t ManhattanDistance(const Index2D& from, const Index2D& to);
|
||||
|
||||
static void DisableTraceErrors()
|
||||
{
|
||||
traceErrors = false;
|
||||
}
|
||||
static void DisableTraceErrors();
|
||||
|
||||
static void ResetTraceErrors()
|
||||
{
|
||||
traceErrors = true;
|
||||
}
|
||||
static void ResetTraceErrors();
|
||||
|
||||
private:
|
||||
template <typename Key, typename Value, typename Compare, typename Eval>
|
||||
@@ -745,14 +562,18 @@ public:
|
||||
static bool traceErrors;
|
||||
};
|
||||
|
||||
inline bool Helper::traceErrors = true;
|
||||
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container)
|
||||
{
|
||||
return Helper::Print(stream, container);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::deque<Args...>& container)
|
||||
{
|
||||
return Helper::Print(stream, container);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::set<Args...>& container)
|
||||
{
|
||||
|
||||
+31
-1
@@ -5,6 +5,30 @@
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
#undef AOC_MD5_TESTING
|
||||
|
||||
void PrintData(uint8_t* data)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)data[i * 8 + j];
|
||||
}
|
||||
std::cout << " ";
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
if (data[i * 8 + j] >= 32 && data[i * 8 + j] < 126)
|
||||
std::cout << data[i * 8 + j];
|
||||
else
|
||||
std::cout << "?";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
uint32_t Md5::BytesToUint32(uint8_t* data)
|
||||
{
|
||||
return ((uint32_t)data[3] << 24) | ((uint32_t)data[2] << 16) | ((uint32_t)data[1] << 8) | (uint32_t)data[0];
|
||||
@@ -17,6 +41,9 @@ uint32_t Md5::LeftRotate(uint32_t x, uint32_t n)
|
||||
|
||||
void Md5::Transform(uint8_t* data, uint32_t& a0, uint32_t& b0, uint32_t& c0, uint32_t& d0)
|
||||
{
|
||||
#ifdef AOC_MD5_TESTING
|
||||
PrintData(data);
|
||||
#endif
|
||||
// clang-format off
|
||||
const static std::array<uint32_t, 64> K
|
||||
{
|
||||
@@ -116,7 +143,10 @@ std::string Md5::Hash(std::string str)
|
||||
uint64_t offset = 0;
|
||||
for (int i = 0; i < loops - 1; i++)
|
||||
{
|
||||
Transform((uint8_t*)(str.c_str() + offset), a0, b0, c0, d0);
|
||||
uint8_t data[64];
|
||||
std::memset(data, 0x0, 64);
|
||||
std::memcpy(data, str.c_str() + offset, std::min<int>(str.size() - offset, 64));
|
||||
Transform(data, a0, b0, c0, d0);
|
||||
offset += 64;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user