Compare commits

...

2 Commits

Author SHA1 Message Date
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
24 changed files with 172 additions and 50 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
+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);
}
}
+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;
}
+4 -2
View File
@@ -313,7 +313,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 +356,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();
+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;
}