Compare commits

..

8 Commits

Author SHA1 Message Date
Thraix 85f5de1be2 Add solution for 2016 Day 19 2026-08-31 22:21:13 +02:00
Thraix 8162a46091 Add solution for 2016 Day 18 2026-08-31 20:51:09 +02:00
Thraix ed11090cc2 Add solution for 2016 Day 17
- Fix Md5 algorithm
2026-08-30 22:52:01 +02:00
Thraix 7b2567a71e Add solution for 2016 Day 16 2026-08-27 21:41:22 +02:00
Thraix c669dedbb7 Move Helper functions into cpp file 2026-08-27 20:44:14 +02:00
Thraix 5111d633b7 Add solution for 2016 Day 15 2026-08-26 23:13:22 +02:00
Thraix 988c5552d0 Update clang-format to disallow single line for-loops 2026-08-23 12:12:53 +02:00
Thraix ab6c726ebc Add solution for 2016 Day 14 2026-08-23 12:11:50 +02:00
36 changed files with 725 additions and 324 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AllowShortNamespacesOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
+1
View File
@@ -0,0 +1 @@
abc
+3
View File
@@ -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.
+1
View File
@@ -0,0 +1 @@
10000
+1
View File
@@ -0,0 +1 @@
ulqzkmiv
+1
View File
@@ -0,0 +1 @@
.^^.^.^^^^
+1
View File
@@ -0,0 +1 @@
5
+92 -10
View File
@@ -2,30 +2,112 @@
namespace y2016::day14
{
REGISTER_DAY(2016, Day14, std::vector<int>, int);
REGISTER_DAY(2016, Day14, std::string, int);
REGISTER_TEST_EXAMPLE(2016, Day14, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day14, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day14, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day14, Input, 2, 0);
REGISTER_TEST_EXAMPLE(2016, Day14, ExampleInput, 1, 22728);
REGISTER_TEST(2016, Day14, Input, 1, 15035);
REGISTER_TEST_EXAMPLE(2016, Day14, ExampleInput, 2, 22551);
REGISTER_TEST(2016, Day14, Input, 2, 19968);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
getline(input, str);
return str;
}
char GetConsecutive(const std::string& s, int count)
{
for (size_t i = 0; i < s.size(); i++)
{
bool found = true;
char c = s[i];
for (int j = 1; j < count; j++)
{
if (s[i + j] != c)
{
found = false;
break;
}
}
if (found)
return c;
}
return vec;
return '\0';
}
std::set<char> GetConsecutives(const std::string& s, int count)
{
std::set<char> consecutives;
for (size_t i = 0; i < s.size(); i++)
{
char c = s[i];
bool found = true;
for (int j = 0; j < count; j++)
{
if (s[i + j] != c)
{
found = false;
break;
}
}
if (found)
consecutives.emplace(c);
}
return consecutives;
}
void Hash(std::string& s, int count)
{
for (int i = 0; i < count; i++)
s = Md5::Hash(s);
}
int FindKeys(const std::string& str, int repeat)
{
std::vector<int> foundKeys;
std::map<int, char> keys;
int i = 0;
int upperLimit = std::numeric_limits<int>::max();
while (i < upperLimit)
{
keys.erase(i - 1000);
std::string hash = str + std::to_string(i);
Hash(hash, repeat + 1);
char c = GetConsecutive(hash, 3);
std::set<char> consecutives = GetConsecutives(hash, 5);
for (auto it = keys.begin(); it != keys.end();)
{
if (consecutives.count(it->second) != 0)
{
foundKeys.emplace_back(it->first);
if (foundKeys.size() == 64)
upperLimit = it->first + 1000;
it = keys.erase(it);
}
else
{
it++;
}
}
if (c != '\0')
keys.emplace(i, c);
i++;
}
std::sort(foundKeys.begin(), foundKeys.end());
return foundKeys[63];
}
OUTPUT1(input)
{
return 0;
return FindKeys(input, 0);
}
OUTPUT2(input)
{
return 0;
return FindKeys(input, 2016);
}
}
+35 -8
View File
@@ -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);
}
}
+37 -10
View File
@@ -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;
}
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)
{
return 0;
return Solve(input, isExample ? 20 : 272);
}
OUTPUT2(input)
{
return 0;
return Solve(input, 35651584);
}
}
+65 -11
View File
@@ -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))
{
}
return vec;
getline(input, str);
return str;
}
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);
}
}
+36 -10
View File
@@ -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;
}
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 vec;
return safes;
}
OUTPUT1(input)
{
return 0;
return Solve(input, isExample ? 10 : 40);
}
OUTPUT2(input)
{
return 0;
return Solve(input, 400000);
}
}
+51 -13
View File
@@ -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();
}
}
+4 -2
View File
@@ -28,7 +28,8 @@ namespace y2021::day08
std::string s;
ss >> s;
std::set<char> set;
for (char c : s) set.emplace(c);
for (char c : s)
set.emplace(c);
signal.numbers.emplace_back(set);
}
char c;
@@ -38,7 +39,8 @@ namespace y2021::day08
std::string s;
ss >> s;
std::set<char> set;
for (char c : s) set.emplace(c);
for (char c : s)
set.emplace(c);
signal.display.emplace_back(set);
}
signals.emplace_back(signal);
+6 -3
View File
@@ -5,7 +5,8 @@ namespace y2021::day11
std::set<Index2D> CalculateFlashing(Array2D<int>& array)
{
for (auto& val : array) val++;
for (auto& val : array)
val++;
std::set<Index2D> flashed;
bool hasChanged = true;
while (hasChanged)
@@ -59,7 +60,8 @@ namespace y2021::day11
{
std::set<Index2D> flashed = CalculateFlashing(array);
count += flashed.size();
for (auto& flash : flashed) array[flash] = 0;
for (auto& flash : flashed)
array[flash] = 0;
}
return count;
}
@@ -74,7 +76,8 @@ namespace y2021::day11
std::set<Index2D> flashed = CalculateFlashing(array);
if (flashed.size() == array.width * array.height)
return count;
for (auto& flash : flashed) array[flash] = 0;
for (auto& flash : flashed)
array[flash] = 0;
count++;
}
return count;
+2 -1
View File
@@ -272,7 +272,8 @@ namespace y2021::day18
while (changed)
{
changed = false;
while (node.Explode()) changed = true;
while (node.Explode())
changed = true;
if (node.Split())
changed = true;
}
+2 -1
View File
@@ -68,7 +68,8 @@ namespace y$1::day11
std::getline(input, str);
std::stringstream ss{str};
ss >> "Starting items: ";
while (std::getline(ss, str, ',')) monkey.items.emplace_back(std::stoi(str));
while (std::getline(ss, str, ','))
monkey.items.emplace_back(std::stoi(str));
}
{ // operator
std::getline(input, str);
+2 -1
View File
@@ -77,7 +77,8 @@ namespace y$1::day13
Node node;
node.nodes = std::stoi(str.substr(index));
nodes.emplace_back(node);
while (str[index] >= '0' && str[index] <= '9') index++;
while (str[index] >= '0' && str[index] <= '9')
index++;
}
else
{
+4 -2
View File
@@ -131,7 +131,8 @@ namespace y$1::day22
if (input.directions[i] >= '0' && input.directions[i] <= '9')
{
int amount = std::stoi(&input.directions[i]);
while (input.directions[i] >= '0' && input.directions[i] <= '9') i++;
while (input.directions[i] >= '0' && input.directions[i] <= '9')
i++;
for (int j = 0; j < amount; j++)
{
@@ -219,7 +220,8 @@ namespace y$1::day22
if (input.directions[i] >= '0' && input.directions[i] <= '9')
{
int amount = std::stoi(&input.directions[i]);
while (input.directions[i] >= '0' && input.directions[i] <= '9') i++;
while (input.directions[i] >= '0' && input.directions[i] <= '9')
i++;
for (int j = 0; j < amount; j++)
{
+2 -1
View File
@@ -39,7 +39,8 @@ namespace y$1::day25
// Convert to base 5
std::string base5 = "";
int64_t numbers = log(base10) / log(5) + 1;
for (int i = 0; i < numbers + 1; i++) base5.push_back('0');
for (int i = 0; i < numbers + 1; i++)
base5.push_back('0');
int64_t base5Max = Pow(5, numbers - 2);
for (int i = 1; i < base5.size(); i++)
+6 -3
View File
@@ -55,7 +55,8 @@ namespace y2023::day03
if (Helper::IsDigit(array.Get(x, y)))
{
Index2D numberStart = Index2D{x, y};
while (numberStart.x >= 0 && Helper::IsDigit(array.Get(numberStart))) numberStart.x--;
while (numberStart.x >= 0 && Helper::IsDigit(array.Get(numberStart)))
numberStart.x--;
numberStart.x++;
int number = GetNumber(array, numberStart);
@@ -64,7 +65,8 @@ namespace y2023::day03
else
return firstNum * number;
while (x < array.width && Helper::IsDigit(array.Get(x, y))) x++;
while (x < array.width && Helper::IsDigit(array.Get(x, y)))
x++;
x--;
}
}
@@ -88,7 +90,8 @@ namespace y2023::day03
if (Helper::IsDigit(input.Get(x, y)))
{
int i = 1;
while (x + i < input.width && Helper::IsDigit(input.Get(x + i, y))) i++;
while (x + i < input.width && Helper::IsDigit(input.Get(x + i, y)))
i++;
if (HasAdjecent(input, Index2D{x, y}, i))
{
sum += GetNumber(input, Index2D{x, y});
+4 -2
View File
@@ -28,12 +28,14 @@ namespace y2023::day04
std::stringstream ss{str};
ss >> "Card " >> i >> ":";
while (ss >> i) game.winning.emplace_back(i);
while (ss >> i)
game.winning.emplace_back(i);
ss.clear(); // clear the failbit to be able to read more data
ss >> "|";
while (ss >> i) game.numbers.emplace(i);
while (ss >> i)
game.numbers.emplace(i);
games.emplace_back(game);
}
+2 -1
View File
@@ -82,7 +82,8 @@ namespace y2023::day05
std::stringstream ss{str};
int64_t i;
ss >> "seeds: ";
while (ss >> i) plantation.seeds.emplace_back(i);
while (ss >> i)
plantation.seeds.emplace_back(i);
std::getline(input, str);
while (std::getline(input, str))
{
+2 -1
View File
@@ -18,7 +18,8 @@ namespace y2023::day06
std::stringstream ss{str};
int i;
ss >> "Time: ";
while (ss >> i) games.emplace_back(i, 0);
while (ss >> i)
games.emplace_back(i, 0);
std::getline(input, str);
std::stringstream ss2{str};
+2 -1
View File
@@ -18,7 +18,8 @@ namespace y2023::day09
std::vector<int> numbers;
std::stringstream ss{str};
int i;
while (ss >> i) numbers.emplace_back(i);
while (ss >> i)
numbers.emplace_back(i);
ints.emplace_back(numbers);
}
+2 -1
View File
@@ -26,7 +26,8 @@ namespace y2024::day03
{
int32_t sum = 0;
std::vector<std::string> matches = Helper::GetAllRegexMatches(input, "mul\\([0-9]*,[0-9]*\\)");
for (auto& str : matches) sum += Mult(str);
for (auto& str : matches)
sum += Mult(str);
return sum;
}
+2 -1
View File
@@ -50,7 +50,8 @@ namespace y2024::day07
int64_t Concat(int64_t i1, int64_t i2)
{
int64_t base = 10;
while (i2 >= base) base *= 10;
while (i2 >= base)
base *= 10;
return i1 * base + i2;
}
+6 -3
View File
@@ -13,7 +13,8 @@ namespace y2024::day11
READ_INPUT(input)
{
Stones stones;
for (auto i : Input::ReadInt64s(input, ' ')) stones[i]++;
for (auto i : Input::ReadInt64s(input, ' '))
stones[i]++;
return stones;
}
@@ -33,7 +34,8 @@ namespace y2024::day11
{
int64_t pow = 1;
for (int j = 0; j < i; j++) pow *= 10;
for (int j = 0; j < i; j++)
pow *= 10;
return pow;
}
@@ -74,7 +76,8 @@ namespace y2024::day11
{
std::map<int64_t, int64_t> cpy = stones;
for (int i = 0; i < iterations; i++) Step(cpy);
for (int i = 0; i < iterations; i++)
Step(cpy);
int64_t sum = 0;
for (auto& [cur, count] : cpy)
+2 -1
View File
@@ -21,7 +21,8 @@ namespace y2024::day15
map.map = Input::ReadArray2D(input);
std::string str;
while (std::getline(input, str)) map.input += str;
while (std::getline(input, str))
map.input += str;
return map;
}
+2 -1
View File
@@ -48,7 +48,8 @@ namespace y2024::day16
Helper::DijkstrasAllVisited(input, std::pair{input.Find('S'), Index2D{1, 0}}, Branch, Goal);
std::set<Index2D> solutions;
for (const auto& [pos, dir] : allVisited) solutions.emplace(pos);
for (const auto& [pos, dir] : allVisited)
solutions.emplace(pos);
return solutions.size();
}
+4 -2
View File
@@ -92,13 +92,15 @@ namespace y2024::day23
FindLargest(input, computers, largest);
}
for (const auto& s : largest) std::cout << s << ",";
for (const auto& s : largest)
std::cout << s << ",";
std::cout << std::endl;
// Some arbitrary solution number to have some form of verification in the code.
// Sum all letters of the computers (with a=0, b=1, etc)
int64_t sum = 0;
for (const auto& s : largest) sum += (s[0] - 'a') + (s[1] - 'a');
for (const auto& s : largest)
sum += (s[0] - 'a') + (s[1] - 'a');
return sum;
}
+249
View File
@@ -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;
}
+46 -223
View File
@@ -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)
@@ -313,7 +201,8 @@ struct Helper
{
std::map<State, int> visited;
std::multimap<int, std::pair<int, State>> open;
for (int i = 0; i < initial.size(); i++) open.emplace(0, std::pair<int, State>{0, initial[i]});
for (int i = 0; i < initial.size(); i++)
open.emplace(0, std::pair<int, State>{0, initial[i]});
while (!open.empty())
{
auto it = open.begin();
@@ -355,7 +244,8 @@ struct Helper
{
std::map<State, int> visited;
std::multimap<int, std::pair<int, std::vector<State>>> open;
for (int i = 0; i < initial.size(); i++) open.emplace(0, std::pair<int, std::vector<State>>{0, {initial[i]}});
for (int i = 0; i < initial.size(); i++)
open.emplace(0, std::pair<int, std::vector<State>>{0, {initial[i]}});
while (!open.empty())
{
const auto& [heuristicVal, state] = *open.begin();
@@ -542,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)
@@ -566,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);
@@ -667,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>
@@ -743,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)
{
+16 -8
View File
@@ -13,9 +13,11 @@
static std::istream& operator>>(std::istream& stream, char const* pattern)
{
char c;
while (stream.peek() == ' ') stream.get(c);
while (stream.peek() == ' ')
stream.get(c);
while (*pattern != '\0' && stream && *pattern == stream.peek() && stream.get(c)) ++pattern;
while (*pattern != '\0' && stream && *pattern == stream.peek() && stream.get(c))
++pattern;
if (*pattern != '\0')
stream.setstate(std::ios::failbit);
@@ -37,7 +39,8 @@ struct Input
break;
width = (int)str.length();
height++;
for (auto c : str) data.emplace_back(c);
for (auto c : str)
data.emplace_back(c);
}
return Array2D<char>(width, height, data);
}
@@ -52,7 +55,8 @@ struct Input
{
width = (int)str.length();
height++;
for (auto c : str) data.emplace_back(c - '0');
for (auto c : str)
data.emplace_back(c - '0');
}
return Array2D<int>(width, height, data);
}
@@ -93,7 +97,8 @@ struct Input
{
std::vector<int> ints;
std::string str;
while (getline(input, str, delimiter)) ints.emplace_back(std::atoi(str.c_str()));
while (getline(input, str, delimiter))
ints.emplace_back(std::atoi(str.c_str()));
return ints;
}
@@ -101,7 +106,8 @@ struct Input
{
std::vector<int64_t> ints;
std::string str;
while (getline(input, str, delimiter)) ints.emplace_back(std::atoll(str.c_str()));
while (getline(input, str, delimiter))
ints.emplace_back(std::atoll(str.c_str()));
return ints;
}
@@ -109,7 +115,8 @@ struct Input
{
std::vector<std::string> strings;
std::string str;
while (getline(input, str)) strings.emplace_back(str);
while (getline(input, str))
strings.emplace_back(str);
return strings;
}
@@ -117,7 +124,8 @@ struct Input
{
std::string result;
std::string str;
while (getline(input, str)) result += str + '\n';
while (getline(input, str))
result += str + '\n';
if (!result.empty())
result.pop_back();
return result;
+2 -1
View File
@@ -97,7 +97,8 @@ bool JsonUtil::ReadString(const std::string& str, uint64_t& pos, Json& json)
uint64_t stringStart = pos;
while (true)
{
while (pos < str.size() && str[pos] != '\"') pos++;
while (pos < str.size() && str[pos] != '\"')
pos++;
if (pos >= str.size() || str[pos - 1] != '\\')
break;
}
+31 -1
View File
@@ -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;
}