Compare commits

..

22 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
Thraix 482794f994 Add solution for 2016 Day 13 2026-08-23 10:43:10 +02:00
Thraix f26d9fd416 Add solution for 2016 Day 12 2026-08-22 18:37:41 +02:00
Thraix 5d44b9484f Add solution for 2016 Day 11 2026-08-22 14:33:45 +02:00
Thraix fc16b73a01 Add solution for 2016 Day 10 2026-08-16 20:29:20 +02:00
Thraix e5acfb8635 Add solution for 2016 Day 9 2026-08-16 11:56:04 +02:00
Thraix 3d9b19be4d Add solution for 2016 Day 8 2026-08-15 22:18:32 +02:00
Thraix b60954656b Add solution for 2016 Day 7 2026-08-15 20:48:27 +02:00
Thraix 474e662ebd Add solution for 2016 Day 6 2026-08-15 20:15:30 +02:00
Thraix bf12e141a6 Add solution for 2016 Day 5
- Add Md5 algorithm to library
2026-08-15 15:24:44 +02:00
Thraix c228e8e230 Add solution for 2016 Day 4 2026-08-14 16:09:32 +02:00
Thraix fc76349588 Add solution for 2016 Day 3 2026-08-12 21:00:56 +02:00
Thraix d71df0c742 Add solution for 2016 Day 2 2026-08-11 23:02:52 +02:00
Thraix c8f0000c4d Add solution for 2016 Day 1 2026-08-06 22:17:35 +02:00
Thraix 80eacc6ed2 Add 2016 templates 2026-08-06 21:33:10 +02:00
78 changed files with 2713 additions and 256 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: Never AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true AllowShortLoopsOnASingleLine: false
AllowShortNamespacesOnASingleLine: false AllowShortNamespacesOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true AlwaysBreakBeforeMultilineStrings: true
+1
View File
@@ -0,0 +1 @@
R8, R4, R4, R8
+4
View File
@@ -0,0 +1,4 @@
ULL
RRDDD
LURDL
UUUUD
+3
View File
@@ -0,0 +1,3 @@
5 10 25
11 15 25
8 25 20
+4
View File
@@ -0,0 +1,4 @@
aaaaa-bbb-z-y-x-123[abxyz]
a-b-c-d-e-f-g-h-987[abcde]
not-a-real-room-404[oarel]
totally-real-room-200[decoy]
+1
View File
@@ -0,0 +1 @@
abc
+16
View File
@@ -0,0 +1,16 @@
eedadn
drvtee
eandsr
raavrd
atevrs
tsrnev
sdttsa
rasrtv
nssdts
ntnada
svetve
tesnvt
vntsnd
vrdear
dvrsen
enarar
+8
View File
@@ -0,0 +1,8 @@
abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn
aba[bab]xyz
xyx[xyx]xyx
aaa[kek]eke
zazbz[bzb]cdb
+4
View File
@@ -0,0 +1,4 @@
rect 3x2
rotate column x=1 by 1
rotate row y=0 by 4
rotate column x=1 by 1
+1
View File
@@ -0,0 +1 @@
X(8x2)(3x3)ABCY
+6
View File
@@ -0,0 +1,6 @@
value 5 goes to bot 2
bot 2 gives low to bot 1 and high to bot 0
value 3 goes to bot 1
bot 1 gives low to output 1 and high to bot 0
bot 0 gives low to output 2 and high to output 0
value 2 goes to bot 2
+4
View File
@@ -0,0 +1,4 @@
The first floor contains a hydrogen-compatible microchip and a lithium-compatible microchip.
The second floor contains a hydrogen generator.
The third floor contains a lithium generator.
The fourth floor contains nothing relevant.
+6
View File
@@ -0,0 +1,6 @@
cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a
+1
View File
@@ -0,0 +1 @@
10
+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
View File
View File
View File
View File
View File
View File
+73
View File
@@ -0,0 +1,73 @@
#include "common/aoc.h"
namespace y2016::day01
{
struct Move
{
char r;
int count;
};
REGISTER_DAY(2016, Day01, std::vector<Move>, int);
REGISTER_TEST_EXAMPLE(2016, Day01, ExampleInput, 1, 8);
REGISTER_TEST(2016, Day01, Input, 1, 271);
REGISTER_TEST_EXAMPLE(2016, Day01, ExampleInput, 2, 4);
REGISTER_TEST(2016, Day01, Input, 2, 153);
READ_INPUT(input)
{
std::vector<Move> vec;
std::string str;
while (getline(input, str, ' '))
{
Move move;
move.r = str[0];
move.count = std::strtol(&str[1], nullptr, 10);
vec.emplace_back(move);
}
return vec;
}
OUTPUT1(input)
{
Index2D dir{1, 0};
Index2D pos{0, 0};
for (int i = 0; i < input.size(); i++)
{
if (input[i].r == 'R')
dir.RotateCW();
else
dir.RotateCCW();
pos = pos + dir * input[i].count;
}
return Helper::ManhattanDistance(pos, Index2D{0, 0});
}
OUTPUT2(input)
{
Index2D dir{1, 0};
Index2D pos{0, 0};
std::set<Index2D> visited;
visited.emplace(pos);
for (int i = 0; i < input.size(); i++)
{
if (input[i].r == 'R')
dir.RotateCW();
else
dir.RotateCCW();
for (int j = 0; j < input[i].count; j++)
{
pos = pos + dir;
if (visited.count(pos) != 0)
{
return Helper::ManhattanDistance(pos, Index2D{0, 0});
}
visited.emplace(pos);
}
}
return 0;
}
}
+126
View File
@@ -0,0 +1,126 @@
#include "common/aoc.h"
namespace y2016::day02
{
REGISTER_DAY(2016, Day02, std::vector<std::string>, std::string);
REGISTER_TEST_EXAMPLE(2016, Day02, ExampleInput, 1, "1985");
REGISTER_TEST(2016, Day02, Input, 1, "14894");
REGISTER_TEST_EXAMPLE(2016, Day02, ExampleInput, 2, "5DB3");
REGISTER_TEST(2016, Day02, Input, 2, "26B96");
READ_INPUT(input)
{
std::vector<std::string> vec;
std::string str;
while (getline(input, str))
{
vec.emplace_back(str);
}
return vec;
}
char IndexToChar1(Index2D index)
{
return (index.x + index.y * 3) + '1';
}
char IndexToChar2(Index2D index)
{
if (index.y == 0)
return '1';
if (index.y == 1)
return '2' + index.x - 1;
if (index.y == 2)
return '5' + index.x;
if (index.y == 3)
return 'A' + index.x - 1;
if (index.y == 4)
return 'D';
return 'X';
}
void ClampX(Index2D& pos)
{
if (pos.y == 0 || pos.y == 4)
pos.x = std::clamp(pos.x, 2, 2);
if (pos.y == 1 || pos.y == 3)
pos.x = std::clamp(pos.x, 1, 3);
if (pos.y == 2)
pos.x = std::clamp(pos.x, 0, 4);
}
void ClampY(Index2D& pos)
{
if (pos.x == 0 || pos.x == 4)
pos.y = std::clamp(pos.y, 2, 2);
if (pos.x == 1 || pos.x == 3)
pos.y = std::clamp(pos.y, 1, 3);
if (pos.x == 2)
pos.y = std::clamp(pos.y, 0, 4);
}
OUTPUT1(input)
{
Index2D pos{1, 1};
std::string code = "";
for (const auto& instructions : input)
{
for (const auto& instruction : instructions)
{
switch (instruction)
{
case 'R':
pos.x++;
break;
case 'L':
pos.x--;
break;
case 'U':
pos.y--;
break;
case 'D':
pos.y++;
break;
}
pos.x = std::clamp(pos.x, 0, 2);
pos.y = std::clamp(pos.y, 0, 2);
}
code += IndexToChar1(pos);
}
return code;
}
OUTPUT2(input)
{
Index2D pos{0, 2};
std::string code = "";
for (const auto& instructions : input)
{
for (const auto& instruction : instructions)
{
switch (instruction)
{
case 'R':
pos.x++;
ClampX(pos);
break;
case 'L':
pos.x--;
ClampX(pos);
break;
case 'U':
pos.y--;
ClampY(pos);
break;
case 'D':
pos.y++;
ClampY(pos);
break;
}
}
code += IndexToChar2(pos);
}
return code;
}
}
+52
View File
@@ -0,0 +1,52 @@
#include "common/aoc.h"
namespace y2016::day03
{
REGISTER_DAY(2016, Day03, Array2D<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 1, 2);
REGISTER_TEST(2016, Day03, Input, 1, 983);
REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 2, 2);
REGISTER_TEST(2016, Day03, Input, 2, 1836);
READ_INPUT(input)
{
return Input::ReadIntsAsArray2D(input);
}
bool IsValid(const std::array<int, 3>& triangle)
{
for (int i = 0; i < 3; i++)
{
int length = triangle.at(i) + triangle.at((i + 1) % 3);
if (length <= triangle.at((i + 2) % 3))
return false;
}
return true;
}
OUTPUT1(input)
{
int count = 0;
for (int y = 0; y < input.height; y++)
{
if (IsValid({input.Get(0, y), input.Get(1, y), input.Get(2, y)}))
count++;
}
return count;
}
OUTPUT2(input)
{
int count = 0;
for (int y = 0; y < input.height; y += 3)
{
for (int x = 0; x < 3; x++)
{
if (IsValid({input.Get(x, y), input.Get(x, y + 1), input.Get(x, y + 2)}))
count++;
}
}
return count;
}
}
+109
View File
@@ -0,0 +1,109 @@
#include "common/aoc.h"
namespace y2016::day04
{
struct Room
{
std::string name;
int sectorId;
std::string checksum;
};
REGISTER_DAY(2016, Day04, std::vector<Room>, int);
REGISTER_TEST_EXAMPLE(2016, Day04, ExampleInput, 1, 1514);
REGISTER_TEST(2016, Day04, Input, 1, 278221);
REGISTER_TEST(2016, Day04, Input, 2, 267);
READ_INPUT(input)
{
std::vector<Room> vec;
std::string str;
while (getline(input, str))
{
Room room;
std::stringstream ss{str};
getline(ss, room.name, '[');
size_t pos = room.name.find_last_of('-');
room.sectorId = std::strtoll(room.name.c_str() + pos + 1, nullptr, 10);
room.name = room.name.substr(0, pos);
getline(ss, room.checksum, ']');
vec.emplace_back(room);
}
return vec;
}
struct CharCount
{
char c;
int count;
bool operator<(const CharCount& rhs) const
{
if (count != rhs.count)
return count > rhs.count;
return c < rhs.c;
}
};
bool ValidRoom(const Room& room)
{
std::array<int, 'z' - 'a' + 1> counts;
counts.fill(0);
for (auto c : room.name)
{
if (c >= 'a' && c <= 'z')
counts[c - 'a']++;
}
std::set<CharCount> mostUsed;
for (int i = 0; i < counts.size(); i++)
{
if (counts[i] > 0)
mostUsed.emplace(CharCount{(char)('a' + i), counts[i]});
}
int i = 0;
for (auto it = mostUsed.begin(); i < 5; it++, i++)
{
if (room.checksum[i] != it->c)
{
return false;
}
}
return true;
}
OUTPUT1(input)
{
int count = 0;
for (const auto& room : input)
{
if (ValidRoom(room))
count += room.sectorId;
}
return count;
}
OUTPUT2(input)
{
for (const auto& room : input)
{
if (!ValidRoom(room))
continue;
std::string newName = room.name;
for (auto& c : newName)
{
if (c >= 'a' && c <= 'z')
c = ((c - 'a') + room.sectorId) % ('z' - 'a' + 1) + 'a';
}
if (newName == "northpole-object-storage")
return room.sectorId;
}
return 0;
}
}
+61
View File
@@ -0,0 +1,61 @@
#include "common/aoc.h"
namespace y2016::day05
{
REGISTER_DAY(2016, Day05, std::string, std::string);
REGISTER_TEST(2016, Day05, Input, 1, "f97c354d");
REGISTER_TEST(2016, Day05, Input, 2, "863dde27");
READ_INPUT(input)
{
std::string str;
getline(input, str);
return str;
}
OUTPUT1(input)
{
int i = 0;
std::string result;
while (true)
{
std::string str = input + std::to_string(i);
std::string s = Md5::Hash(str);
if (Helper::StartsWith(s, "00000"))
{
result.push_back(s[5]);
if (result.size() == 8)
return result;
}
i++;
}
return "";
}
OUTPUT2(input)
{
int i = 0;
std::string result{"XXXXXXXX"};
int replacements = 0;
for (int i = 0;; i++)
{
std::string str = input + std::to_string(i);
std::string s = Md5::Hash(str);
if (Helper::StartsWith(s, "00000"))
{
if (s[5] < '0' || s[5] > '7')
continue;
if (result[s[5] - '0'] == 'X')
{
result[s[5] - '0'] = s[6];
replacements++;
}
if (replacements == 8)
return result;
}
}
return "";
}
}
+66
View File
@@ -0,0 +1,66 @@
#include "common/aoc.h"
namespace y2016::day06
{
REGISTER_DAY(2016, Day06, Array2D<char>, std::string);
REGISTER_TEST_EXAMPLE(2016, Day06, ExampleInput, 1, "easter");
REGISTER_TEST(2016, Day06, Input, 1, "xhnqpqql");
REGISTER_TEST_EXAMPLE(2016, Day06, ExampleInput, 2, "advent");
REGISTER_TEST(2016, Day06, Input, 2, "brhailro");
READ_INPUT(input)
{
return Input::ReadArray2D(input);
}
OUTPUT1(input)
{
std::string result;
for (int x = 0; x < input.width; x++)
{
std::map<char, int> map;
for (int y = 0; y < input.height; y++)
{
map[input.Get(x, y)]++;
}
int max = 0;
char maxChar;
for (const auto& [c, count] : map)
{
if (count > max)
{
maxChar = c;
max = count;
}
}
result.push_back(maxChar);
}
return result;
}
OUTPUT2(input)
{
std::string result;
for (int x = 0; x < input.width; x++)
{
std::map<char, int> map;
for (int y = 0; y < input.height; y++)
{
map[input.Get(x, y)]++;
}
int min = std::numeric_limits<int>::max();
char minChar;
for (const auto& [c, count] : map)
{
if (count < min)
{
minChar = c;
min = count;
}
}
result.push_back(minChar);
}
return result;
}
}
+166
View File
@@ -0,0 +1,166 @@
#include "common/aoc.h"
namespace y2016::day07
{
struct IP
{
std::vector<std::string> s;
std::vector<std::string> hypernets;
friend std::ostream& operator<<(std::ostream& os, const IP& ip)
{
for (int i = 0; i < ip.s.size() + ip.hypernets.size(); i++)
{
if (i % 2 == 0)
os << ip.s[i / 2] << " ";
else
os << "[" << ip.hypernets[i / 2] << "] ";
}
return os;
}
};
REGISTER_DAY(2016, Day07, std::vector<IP>, int);
REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 1, 2);
REGISTER_TEST(2016, Day07, Input, 1, 115);
REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 2, 3);
REGISTER_TEST(2016, Day07, Input, 2, 231);
READ_INPUT(input)
{
std::vector<IP> vec;
std::string str;
while (getline(input, str))
{
IP ip;
bool readHypernet = false;
char c = '[';
size_t prevPos = 0;
size_t pos = str.find(c);
while (pos != std::string::npos)
{
if (!readHypernet)
{
c = ']';
ip.s.emplace_back(str.substr(prevPos, pos - prevPos));
}
else
{
c = '[';
ip.hypernets.emplace_back(str.substr(prevPos, pos - prevPos));
}
prevPos = pos + 1;
pos = str.find(c, prevPos);
readHypernet = !readHypernet;
}
ip.s.emplace_back(str.substr(prevPos));
vec.emplace_back(ip);
}
return vec;
}
bool HasABBA(const std::string& str)
{
for (size_t i = 0; i < str.size() - 3; i++)
{
if (str[i] == str[i + 3] && str[i + 1] == str[i + 2] && str[i] != str[i + 1])
return true;
}
return false;
}
bool SupportsTLS(const IP& ip)
{
for (const auto& hypernet : ip.hypernets)
{
if (HasABBA(hypernet))
{
return false;
}
}
for (const auto& s : ip.s)
{
if (HasABBA(s))
{
return true;
}
}
return false;
}
std::vector<std::pair<char, char>> GetABAs(const std::string& str)
{
std::vector<std::pair<char, char>> result;
for (size_t i = 0; i < str.size() - 2; i++)
{
if (str[i] == str[i + 2] && str[i] != str[i + 1])
result.emplace_back(str[i], str[i + 1]);
}
return result;
}
bool HasABA(const std::string& str, char c1, char c2)
{
for (size_t i = 0; i < str.size() - 2; i++)
{
if (str[i] == c1 && str[i + 1] == c2 && str[i + 2] == c1)
return true;
}
return false;
}
bool HasABA(const std::vector<std::string>& hypernets, char c1, char c2)
{
for (const auto& hypernet : hypernets)
{
if (HasABA(hypernet, c1, c2))
return true;
}
return false;
}
bool SupportsSSL(const IP& ip)
{
for (const auto& s : ip.s)
{
std::vector<std::pair<char, char>> abas = GetABAs(s);
for (const auto& aba : abas)
{
if (HasABA(ip.hypernets, aba.second, aba.first))
{
return true;
}
}
}
return false;
}
OUTPUT1(input)
{
int count = 0;
for (const auto& ip : input)
{
if (SupportsTLS(ip))
{
count++;
}
}
return count;
}
OUTPUT2(input)
{
int count = 0;
for (const auto& ip : input)
{
if (SupportsSSL(ip))
{
count++;
}
}
return count;
}
}
+101
View File
@@ -0,0 +1,101 @@
#include "common/aoc.h"
namespace y2016::day08
{
enum class Operation
{
Rect,
Col,
Row
};
struct Instruction
{
Operation operation;
int i1;
int i2;
};
REGISTER_DAY(2016, Day08, std::vector<Instruction>, int);
REGISTER_TEST_EXAMPLE(2016, Day08, ExampleInput, 1, 6);
REGISTER_TEST(2016, Day08, Input, 1, 115);
READ_INPUT(input)
{
std::vector<Instruction> vec;
std::string str;
while (getline(input, str))
{
Instruction instruction;
std::stringstream ss{str};
if (str[1] == 'e') // rect
{
instruction.operation = Operation::Rect;
ss >> "rect " >> instruction.i1 >> "x" >> instruction.i2;
}
else if (str[7] == 'r') // rotate row
{
instruction.operation = Operation::Row;
ss >> "rotate row y=" >> instruction.i1 >> "by " >> instruction.i2;
}
else // rotate column
{
instruction.operation = Operation::Col;
ss >> "rotate column x=" >> instruction.i1 >> "by " >> instruction.i2;
}
vec.emplace_back(instruction);
}
return vec;
}
OUTPUT1(input)
{
Array2D<char> data{isExample ? 7 : 50, isExample ? 3 : 6, ' '};
for (const auto& instruction : input)
{
switch (instruction.operation)
{
case Operation::Rect:
for (int y = 0; y < instruction.i2; y++)
{
for (int x = 0; x < instruction.i1; x++)
{
data.Set(x, y, '#');
}
}
break;
case Operation::Row:
for (int i = 0; i < instruction.i2; i++)
{
char tmp = data.Get(data.width - 1, instruction.i1);
for (int x = data.width - 1; x > 0; x--)
{
data.Set(x, instruction.i1, data.Get(x - 1, instruction.i1));
}
data.Set(0, instruction.i1, tmp);
}
break;
case Operation::Col:
for (int i = 0; i < instruction.i2; i++)
{
char tmp = data.Get(instruction.i1, data.height - 1);
for (int y = data.height - 1; y > 0; y--)
{
data.Set(instruction.i1, y, data.Get(instruction.i1, y - 1));
}
data.Set(instruction.i1, 0, tmp);
}
break;
}
}
std::cout << data << std::endl;
return data.Count('#');
}
OUTPUT2(input)
{
// Solution is printed in Part 1
return 0;
}
}
+75
View File
@@ -0,0 +1,75 @@
#include "common/aoc.h"
namespace y2016::day09
{
REGISTER_DAY(2016, Day09, std::string, uint64_t);
REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 1, 18);
REGISTER_TEST(2016, Day09, Input, 1, 99145);
REGISTER_TEST_EXAMPLE(2016, Day09, ExampleInput, 2, 20);
REGISTER_TEST(2016, Day09, Input, 2, 10943094568);
READ_INPUT(input)
{
std::string str;
getline(input, str);
return str;
}
std::pair<int, int> GetMarker(const char* str)
{
char* end;
int length = std::strtol(str, &end, 10);
int repeat = std::strtol(end + 1, nullptr, 10);
return {length, repeat};
}
uint64_t GetLength(const char* str, size_t strLength)
{
size_t i = 0;
uint64_t count = 0;
while (i < strLength)
{
if (str[i] == '(')
{
size_t close = std::strstr(str + i, ")") - str;
auto [length, repeat] = GetMarker(str + i + 1);
count += GetLength(str + close + 1, length) * repeat;
i = close + 1 + length;
}
else
{
count++;
i++;
}
}
return count;
}
OUTPUT1(input)
{
uint64_t count = 0;
size_t i = 0;
while (i < input.size())
{
if (input[i] == '(')
{
size_t close = input.find(')', i);
auto [length, repeat] = GetMarker(input.c_str() + i + 1);
count += length * repeat;
i = close + 1 + length;
}
else
{
count++;
i++;
}
}
return count;
}
OUTPUT2(input)
{
return GetLength(input.c_str(), input.size());
}
}
+146
View File
@@ -0,0 +1,146 @@
#include "common/aoc.h"
namespace y2016::day10
{
enum class Output
{
Output,
Bot
};
struct Bot
{
std::vector<int> inputs;
int lowId{-1};
int highId{-1};
Output lowOutput;
Output highOutput;
};
using Bots = std::map<int, Bot>;
REGISTER_DAY(2016, Day10, Bots, int);
REGISTER_TEST_EXAMPLE(2016, Day10, ExampleInput, 1, 2);
REGISTER_TEST(2016, Day10, Input, 1, 47);
REGISTER_TEST_EXAMPLE(2016, Day10, ExampleInput, 2, 30);
REGISTER_TEST(2016, Day10, Input, 2, 2666);
READ_INPUT(input)
{
std::map<int, Bot> map;
std::string str;
while (getline(input, str))
{
std::stringstream ss{str};
if (str[0] == 'v')
{
int value;
int id;
ss >> "value" >> value >> "goes to bot " >> id;
map[id].inputs.emplace_back(value);
}
else
{
int idFrom;
int idLow;
int idHigh;
std::string typeLow;
std::string typeHigh;
ss >> "bot " >> idFrom >> "gives low to " >> typeLow >> idLow >> "and high to " >> typeHigh >> idHigh;
Bot& from = map[idFrom];
from.lowId = idLow;
from.highId = idHigh;
if (typeLow == "bot")
from.lowOutput = Output::Bot;
else
from.lowOutput = Output::Output;
if (typeHigh == "bot")
from.highOutput = Output::Bot;
else
from.highOutput = Output::Output;
}
}
return map;
}
bool HasInput(const Bot& bot, int input1, int input2)
{
return bot.inputs.size() == 2 && ((bot.inputs[0] == input1 && bot.inputs[1] == input2) ||
(bot.inputs[0] == input2 && bot.inputs[1] == input1));
}
OUTPUT1(input)
{
int input1 = isExample ? 5 : 61;
int input2 = isExample ? 2 : 17;
std::map<int, Bot> bots = input;
while (true)
{
for (auto it = bots.begin(); it != bots.end();)
{
if (HasInput(it->second, input1, input2))
return it->first;
if (it->second.inputs.size() == 2)
{
int low = Helper::Min(it->second.inputs);
int high = Helper::Max(it->second.inputs);
if (it->second.lowOutput == Output::Bot)
{
bots[it->second.lowId].inputs.emplace_back(low);
if (HasInput(bots[it->second.lowId], input1, input2))
return it->second.lowId;
}
if (it->second.highOutput == Output::Bot)
{
bots[it->second.highId].inputs.emplace_back(high);
if (HasInput(bots[it->second.highId], input1, input2))
return it->second.highId;
}
it = bots.erase(it);
}
else
{
it++;
}
}
}
return -1;
}
OUTPUT2(input)
{
std::map<int, int> outputs;
int input1 = isExample ? 5 : 61;
int input2 = isExample ? 2 : 17;
std::map<int, Bot> bots = input;
while (!bots.empty())
{
for (auto it = bots.begin(); it != bots.end();)
{
if (it->second.inputs.size() == 2)
{
int low = Helper::Min(it->second.inputs);
int high = Helper::Max(it->second.inputs);
if (it->second.lowOutput == Output::Bot)
bots[it->second.lowId].inputs.emplace_back(low);
else
outputs[it->second.lowId] = low;
if (it->second.highOutput == Output::Bot)
bots[it->second.highId].inputs.emplace_back(high);
else
outputs[it->second.highId] = high;
it = bots.erase(it);
}
else
{
it++;
}
}
}
return outputs[0] * outputs[1] * outputs[2];
}
}
+220
View File
@@ -0,0 +1,220 @@
#include "common/aoc.h"
namespace y2016::day11
{
struct Item
{
int floorPos;
bool generator;
int id;
bool operator==(const Item& item) const
{
return floorPos == item.floorPos && id == item.id && generator == item.generator;
}
};
static int FLOORS = 4;
REGISTER_DAY(2016, Day11, std::vector<Item>, int);
REGISTER_TEST_EXAMPLE(2016, Day11, ExampleInput, 1, 11);
REGISTER_TEST(2016, Day11, Input, 1, 37);
REGISTER_TEST(2016, Day11, Input, 2, 61);
Item ParseItem(const std::string_view& str, int floorPos, std::map<std::string, int>& nameToId)
{
size_t posDash = str.find('-');
std::string name;
bool generator;
if (posDash == std::string::npos)
{
name = str.substr(0, str.find(' '));
generator = true;
}
else
{
name = str.substr(0, posDash);
generator = false;
}
auto it = nameToId.find(name);
if (it != nameToId.end())
return Item{floorPos, generator, it->second};
int size = nameToId.size();
nameToId[name] = size;
return Item{floorPos, generator, size};
}
READ_INPUT(input)
{
std::vector<Item> vec;
std::string str;
std::map<std::string, int> nameToId;
for (int i = 0; i < FLOORS; i++)
{
getline(input, str);
std::vector<std::string_view> split = Helper::Split(str, " a ");
for (size_t j = 1; j < split.size(); j++)
{
vec.emplace_back(ParseItem(split[j], i, nameToId));
}
}
return vec;
}
struct State
{
std::vector<Item> items;
int elevatorPos;
int amountOfPairs;
std::vector<std::pair<int, int>> GetPairs() const
{
// Convert the data to a format where the pairs of generator/microchip don't matter. All that matters are their
// positions
std::vector<std::pair<int, int>> pairs(amountOfPairs);
for (const auto& item : items)
{
if (item.generator)
pairs[item.id].first = item.floorPos;
else
pairs[item.id].second = item.floorPos;
}
std::sort(pairs.begin(), pairs.end());
return pairs;
}
bool operator<(const State& state) const
{
if (elevatorPos != state.elevatorPos)
return elevatorPos < state.elevatorPos;
return GetPairs() < state.GetPairs();
}
};
bool HasItem(const State& state, int floor, bool generator, int id)
{
return std::find(state.items.begin(), state.items.end(), Item{floor, generator, id}) != state.items.end();
}
bool IsValidState(const State& state)
{
for (int i = 0; i < FLOORS; i++)
{
bool hasChipNotConnected = false;
int generators = 0;
for (const auto& item : state.items)
{
if (item.floorPos != i)
continue;
if (item.generator)
{
generators++;
continue;
}
if (HasItem(state, i, true, item.id))
continue;
hasChipNotConnected = true;
}
if (hasChipNotConnected && generators > 0)
{
return false;
}
}
return true;
}
std::vector<std::pair<int, State>> Branch(const std::vector<Item>& input, const State& state)
{
std::vector<std::pair<int, State>> branches;
for (int i = -1; i <= 1; i += 2)
{
if (state.elevatorPos + i < 0 || state.elevatorPos + i > 3)
continue;
State newState = state;
newState.elevatorPos += i;
for (auto& item : newState.items)
{
if (item.floorPos != state.elevatorPos)
continue;
item.floorPos = newState.elevatorPos;
if (IsValidState(newState))
branches.emplace_back(1, newState);
item.floorPos = state.elevatorPos;
}
for (size_t i = 0; i < newState.items.size(); i++)
{
if (newState.items[i].floorPos != state.elevatorPos)
continue;
newState.items[i].floorPos = newState.elevatorPos;
for (size_t j = i + 1; j < newState.items.size(); j++)
{
if (newState.items[j].floorPos != state.elevatorPos)
continue;
newState.items[j].floorPos = newState.elevatorPos;
if (IsValidState(newState))
branches.emplace_back(1, newState);
newState.items[j].floorPos = state.elevatorPos;
}
newState.items[i].floorPos = state.elevatorPos;
}
}
return branches;
}
bool Goal(const std::vector<Item>& input, const State& state)
{
for (size_t i = 0; i < state.items.size(); i++)
{
if (state.items[i].floorPos != 3)
return false;
}
return true;
}
int Heuristic(const std::vector<Item>& input, const State& state)
{
int heuristic = 0;
int moves = 0;
for (size_t i = 0; i < state.items.size(); i++)
{
moves += (3 - state.items[i].floorPos);
}
heuristic += moves / 2;
return heuristic;
}
OUTPUT1(input)
{
int max = 0;
for (const auto& item : input)
{
max = std::max(max, item.id);
}
State state{input, 0, max + 1};
return Helper::AStar(input, state, Heuristic, Branch, Goal);
}
OUTPUT2(input)
{
std::vector<Item> items = input;
int max = 0;
for (const auto& item : items)
{
max = std::max(max, item.id);
}
items.emplace_back(Item{0, true, max + 1});
items.emplace_back(Item{0, false, max + 1});
items.emplace_back(Item{0, true, max + 2});
items.emplace_back(Item{0, false, max + 2});
State state{items, 0, max + 3};
return Helper::AStar(input, state, Heuristic, Branch, Goal);
}
}
+116
View File
@@ -0,0 +1,116 @@
#include "common/aoc.h"
namespace y2016::day12
{
enum class Operator
{
Cpy,
Inc,
Dec,
Jnz
};
struct Instruction
{
Operator op;
std::variant<char, int> arg1;
std::variant<char, int> arg2;
};
REGISTER_DAY(2016, Day12, std::vector<Instruction>, int);
REGISTER_TEST_EXAMPLE(2016, Day12, ExampleInput, 1, 42);
REGISTER_TEST(2016, Day12, Input, 1, 318007);
REGISTER_TEST_EXAMPLE(2016, Day12, ExampleInput, 2, 42);
REGISTER_TEST(2016, Day12, Input, 2, 9227661);
READ_INPUT(input)
{
std::vector<Instruction> vec;
std::string str;
while (getline(input, str))
{
Instruction instruction;
if (str[0] == 'c')
{
instruction.op = Operator::Cpy;
if (str[4] >= 'a' && str[4] <= 'd')
instruction.arg1 = str[4];
else
instruction.arg1 = (int)std::atoi(str.c_str() + 4);
instruction.arg2 = str[str.size() - 1];
}
else if (str[0] == 'i')
{
instruction.op = Operator::Inc;
instruction.arg1 = str[4];
}
else if (str[0] == 'd')
{
instruction.op = Operator::Dec;
instruction.arg1 = str[4];
}
else if (str[0] == 'j')
{
instruction.op = Operator::Jnz;
if (str[4] >= 'a' && str[4] <= 'd')
instruction.arg1 = str[4];
else
instruction.arg1 = (int)std::atoi(str.c_str() + 4);
instruction.arg2 = atoi(str.c_str() + 6);
}
vec.emplace_back(instruction);
}
return vec;
}
int Run(const std::vector<Instruction>& instructions, std::vector<int> registers)
{
size_t pc = 0;
while (pc < instructions.size())
{
switch (instructions[pc].op)
{
case Operator::Cpy:
{
int number = 0;
if (std::holds_alternative<int>(instructions[pc].arg1))
number = std::get<int>(instructions[pc].arg1);
else
number = registers[std::get<char>(instructions[pc].arg1) - 'a'];
registers[std::get<char>(instructions[pc].arg2) - 'a'] = number;
break;
}
case Operator::Inc:
registers[std::get<char>(instructions[pc].arg1) - 'a']++;
break;
case Operator::Dec:
registers[std::get<char>(instructions[pc].arg1) - 'a']--;
break;
case Operator::Jnz:
int value = 0;
if (std::holds_alternative<int>(instructions[pc].arg1))
value = std::get<int>(instructions[pc].arg1);
else
value = registers[std::get<char>(instructions[pc].arg1) - 'a'];
if (value != 0)
pc += std::get<int>(instructions[pc].arg2) - 1;
break;
}
pc++;
}
return registers[0];
}
OUTPUT1(input)
{
return Run(input, std::vector<int>(4));
}
OUTPUT2(input)
{
std::vector<int> registers(4);
registers[2] = 1;
return Run(input, registers);
}
}
+103
View File
@@ -0,0 +1,103 @@
#include "common/aoc.h"
namespace y2016::day13
{
REGISTER_DAY(2016, Day13, int, int);
REGISTER_TEST_EXAMPLE(2016, Day13, ExampleInput, 1, 11);
REGISTER_TEST(2016, Day13, Input, 1, 82);
REGISTER_TEST_EXAMPLE(2016, Day13, ExampleInput, 2, 151);
REGISTER_TEST(2016, Day13, Input, 2, 138);
READ_INPUT(input)
{
int i;
input >> i;
return i;
}
bool IsWall(int x, int y, int number)
{
int i = x * x + 3 * x + 2 * x * y + y + y * y;
i += number;
std::string str = std::bitset<32>(i).to_string();
// std::cout << str << ": " << std::count(str.begin(), str.end(), '1') % 2 << std::endl;
return std::count(str.begin(), str.end(), '1') % 2 != 0;
}
struct State
{
int x;
int y;
bool operator==(const State& state) const
{
return x == state.x && y == state.y;
}
bool operator<(const State& state) const
{
if (x != state.x)
return x < state.x;
return y < state.y;
}
friend std::ostream& operator<<(std::ostream& os, const State& state)
{
return os << "(" << state.x << ", " << state.y << ")";
}
};
std::vector<std::pair<int, State>> Branch(int input, const State& state)
{
std::vector<std::pair<int, State>> branches;
if (state.x > 0 && !IsWall(state.x - 1, state.y, input))
branches.emplace_back(1, State{state.x - 1, state.y});
if (state.y > 0 && !IsWall(state.x, state.y - 1, input))
branches.emplace_back(1, State{state.x, state.y - 1});
if (!IsWall(state.x + 1, state.y, input))
branches.emplace_back(1, State{state.x + 1, state.y});
if (!IsWall(state.x, state.y + 1, input))
branches.emplace_back(1, State{state.x, state.y + 1});
return branches;
}
bool Goal(int input, const State& state)
{
if (input == 10)
return state == State{7, 4};
else
return state == State{31, 39};
}
OUTPUT1(input)
{
State state{1, 1};
return Helper::Dijkstras(input, state, Branch, Goal);
}
OUTPUT2(input)
{
std::queue<std::pair<int, State>> queue{};
queue.emplace(0, State{1, 1});
std::set<State> visited;
while (!queue.empty())
{
std::pair<int, State> state = queue.front();
queue.pop();
std::vector<std::pair<int, State>> branches = Branch(input, state.second);
for (const auto& branch : branches)
{
if (!visited.emplace(branch.second).second)
continue;
if (state.first >= 49)
continue;
else
queue.emplace(state.first + 1, branch.second);
}
}
return visited.size();
}
}
+113
View File
@@ -0,0 +1,113 @@
#include "common/aoc.h"
namespace y2016::day14
{
REGISTER_DAY(2016, Day14, std::string, int);
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::string 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 '\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 FindKeys(input, 0);
}
OUTPUT2(input)
{
return FindKeys(input, 2016);
}
}
+58
View File
@@ -0,0 +1,58 @@
#include "common/aoc.h"
namespace y2016::day15
{
struct Disc
{
int positions;
int position;
};
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<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 Solve(input);
}
OUTPUT2(input)
{
auto discs = input;
discs.emplace_back(Disc{11, 0});
return Solve(discs);
}
}
+58
View File
@@ -0,0 +1,58 @@
#include "common/aoc.h"
namespace y2016::day16
{
REGISTER_DAY(2016, Day16, std::string, std::string);
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::string 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 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 Solve(input, isExample ? 20 : 272);
}
OUTPUT2(input)
{
return Solve(input, 35651584);
}
}
+85
View File
@@ -0,0 +1,85 @@
#include "common/aoc.h"
namespace y2016::day17
{
REGISTER_DAY(2016, Day17, std::string, std::string);
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;
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)
{
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)
{
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);
}
}
+57
View File
@@ -0,0 +1,57 @@
#include "common/aoc.h"
namespace y2016::day18
{
REGISTER_DAY(2016, Day18, std::string, int);
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::string 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 safes;
}
OUTPUT1(input)
{
return Solve(input, isExample ? 10 : 40);
}
OUTPUT2(input)
{
return Solve(input, 400000);
}
}
+69
View File
@@ -0,0 +1,69 @@
#include "common/aoc.h"
namespace y2016::day19
{
REGISTER_DAY(2016, Day19, int, int);
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)
{
int i;
input >> i;
return i;
}
OUTPUT1(input)
{
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)
{
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();
}
}
+31
View File
@@ -0,0 +1,31 @@
#include "common/aoc.h"
namespace y2016::day20
{
REGISTER_DAY(2016, Day20, std::vector<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day20, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day20, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day20, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day20, Input, 2, 0);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
}
OUTPUT1(input)
{
return 0;
}
OUTPUT2(input)
{
return 0;
}
}
+31
View File
@@ -0,0 +1,31 @@
#include "common/aoc.h"
namespace y2016::day21
{
REGISTER_DAY(2016, Day21, std::vector<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day21, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day21, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day21, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day21, Input, 2, 0);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
}
OUTPUT1(input)
{
return 0;
}
OUTPUT2(input)
{
return 0;
}
}
+31
View File
@@ -0,0 +1,31 @@
#include "common/aoc.h"
namespace y2016::day22
{
REGISTER_DAY(2016, Day22, std::vector<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day22, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day22, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day22, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day22, Input, 2, 0);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
}
OUTPUT1(input)
{
return 0;
}
OUTPUT2(input)
{
return 0;
}
}
+31
View File
@@ -0,0 +1,31 @@
#include "common/aoc.h"
namespace y2016::day23
{
REGISTER_DAY(2016, Day23, std::vector<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day23, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day23, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day23, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day23, Input, 2, 0);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
}
OUTPUT1(input)
{
return 0;
}
OUTPUT2(input)
{
return 0;
}
}
+31
View File
@@ -0,0 +1,31 @@
#include "common/aoc.h"
namespace y2016::day24
{
REGISTER_DAY(2016, Day24, std::vector<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day24, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day24, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day24, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day24, Input, 2, 0);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
}
OUTPUT1(input)
{
return 0;
}
OUTPUT2(input)
{
return 0;
}
}
+31
View File
@@ -0,0 +1,31 @@
#include "common/aoc.h"
namespace y2016::day25
{
REGISTER_DAY(2016, Day25, std::vector<int>, int);
REGISTER_TEST_EXAMPLE(2016, Day25, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day25, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day25, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day25, Input, 2, 0);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
}
OUTPUT1(input)
{
return 0;
}
OUTPUT2(input)
{
return 0;
}
}
+4 -2
View File
@@ -28,7 +28,8 @@ namespace y2021::day08
std::string s; std::string s;
ss >> s; ss >> s;
std::set<char> set; std::set<char> set;
for (char c : s) set.emplace(c); for (char c : s)
set.emplace(c);
signal.numbers.emplace_back(set); signal.numbers.emplace_back(set);
} }
char c; char c;
@@ -38,7 +39,8 @@ namespace y2021::day08
std::string s; std::string s;
ss >> s; ss >> s;
std::set<char> set; std::set<char> set;
for (char c : s) set.emplace(c); for (char c : s)
set.emplace(c);
signal.display.emplace_back(set); signal.display.emplace_back(set);
} }
signals.emplace_back(signal); signals.emplace_back(signal);
+6 -3
View File
@@ -5,7 +5,8 @@ namespace y2021::day11
std::set<Index2D> CalculateFlashing(Array2D<int>& array) std::set<Index2D> CalculateFlashing(Array2D<int>& array)
{ {
for (auto& val : array) val++; for (auto& val : array)
val++;
std::set<Index2D> flashed; std::set<Index2D> flashed;
bool hasChanged = true; bool hasChanged = true;
while (hasChanged) while (hasChanged)
@@ -59,7 +60,8 @@ namespace y2021::day11
{ {
std::set<Index2D> flashed = CalculateFlashing(array); std::set<Index2D> flashed = CalculateFlashing(array);
count += flashed.size(); count += flashed.size();
for (auto& flash : flashed) array[flash] = 0; for (auto& flash : flashed)
array[flash] = 0;
} }
return count; return count;
} }
@@ -74,7 +76,8 @@ namespace y2021::day11
std::set<Index2D> flashed = CalculateFlashing(array); std::set<Index2D> flashed = CalculateFlashing(array);
if (flashed.size() == array.width * array.height) if (flashed.size() == array.width * array.height)
return count; return count;
for (auto& flash : flashed) array[flash] = 0; for (auto& flash : flashed)
array[flash] = 0;
count++; count++;
} }
return count; return count;
+1 -1
View File
@@ -93,7 +93,7 @@ namespace y2021::day13
{ {
board[index] = '#'; board[index] = '#';
} }
std::cout << board; std::cout << board << std::endl;
// Not the "correct" output to part 2, but it verifies something at least // Not the "correct" output to part 2, but it verifies something at least
return indices.size(); return indices.size();
} }
+2 -1
View File
@@ -272,7 +272,8 @@ namespace y2021::day18
while (changed) while (changed)
{ {
changed = false; changed = false;
while (node.Explode()) changed = true; while (node.Explode())
changed = true;
if (node.Split()) if (node.Split())
changed = true; changed = true;
} }
+2 -1
View File
@@ -68,7 +68,8 @@ namespace y$1::day11
std::getline(input, str); std::getline(input, str);
std::stringstream ss{str}; std::stringstream ss{str};
ss >> "Starting items: "; 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 { // operator
std::getline(input, str); std::getline(input, str);
+2 -1
View File
@@ -77,7 +77,8 @@ namespace y$1::day13
Node node; Node node;
node.nodes = std::stoi(str.substr(index)); node.nodes = std::stoi(str.substr(index));
nodes.emplace_back(node); nodes.emplace_back(node);
while (str[index] >= '0' && str[index] <= '9') index++; while (str[index] >= '0' && str[index] <= '9')
index++;
} }
else else
{ {
+4 -2
View File
@@ -131,7 +131,8 @@ namespace y$1::day22
if (input.directions[i] >= '0' && input.directions[i] <= '9') if (input.directions[i] >= '0' && input.directions[i] <= '9')
{ {
int amount = std::stoi(&input.directions[i]); 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++) for (int j = 0; j < amount; j++)
{ {
@@ -219,7 +220,8 @@ namespace y$1::day22
if (input.directions[i] >= '0' && input.directions[i] <= '9') if (input.directions[i] >= '0' && input.directions[i] <= '9')
{ {
int amount = std::stoi(&input.directions[i]); 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++) for (int j = 0; j < amount; j++)
{ {
+2 -1
View File
@@ -39,7 +39,8 @@ namespace y$1::day25
// Convert to base 5 // Convert to base 5
std::string base5 = ""; std::string base5 = "";
int64_t numbers = log(base10) / log(5) + 1; 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); int64_t base5Max = Pow(5, numbers - 2);
for (int i = 1; i < base5.size(); i++) 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))) if (Helper::IsDigit(array.Get(x, y)))
{ {
Index2D numberStart = Index2D{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++; numberStart.x++;
int number = GetNumber(array, numberStart); int number = GetNumber(array, numberStart);
@@ -64,7 +65,8 @@ namespace y2023::day03
else else
return firstNum * number; 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--; x--;
} }
} }
@@ -88,7 +90,8 @@ namespace y2023::day03
if (Helper::IsDigit(input.Get(x, y))) if (Helper::IsDigit(input.Get(x, y)))
{ {
int i = 1; 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)) if (HasAdjecent(input, Index2D{x, y}, i))
{ {
sum += GetNumber(input, Index2D{x, y}); sum += GetNumber(input, Index2D{x, y});
+4 -2
View File
@@ -28,12 +28,14 @@ namespace y2023::day04
std::stringstream ss{str}; std::stringstream ss{str};
ss >> "Card " >> i >> ":"; 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.clear(); // clear the failbit to be able to read more data
ss >> "|"; ss >> "|";
while (ss >> i) game.numbers.emplace(i); while (ss >> i)
game.numbers.emplace(i);
games.emplace_back(game); games.emplace_back(game);
} }
+2 -1
View File
@@ -82,7 +82,8 @@ namespace y2023::day05
std::stringstream ss{str}; std::stringstream ss{str};
int64_t i; int64_t i;
ss >> "seeds: "; ss >> "seeds: ";
while (ss >> i) plantation.seeds.emplace_back(i); while (ss >> i)
plantation.seeds.emplace_back(i);
std::getline(input, str); std::getline(input, str);
while (std::getline(input, str)) while (std::getline(input, str))
{ {
+2 -1
View File
@@ -18,7 +18,8 @@ namespace y2023::day06
std::stringstream ss{str}; std::stringstream ss{str};
int i; int i;
ss >> "Time: "; ss >> "Time: ";
while (ss >> i) games.emplace_back(i, 0); while (ss >> i)
games.emplace_back(i, 0);
std::getline(input, str); std::getline(input, str);
std::stringstream ss2{str}; std::stringstream ss2{str};
+2 -1
View File
@@ -18,7 +18,8 @@ namespace y2023::day09
std::vector<int> numbers; std::vector<int> numbers;
std::stringstream ss{str}; std::stringstream ss{str};
int i; int i;
while (ss >> i) numbers.emplace_back(i); while (ss >> i)
numbers.emplace_back(i);
ints.emplace_back(numbers); ints.emplace_back(numbers);
} }
+2 -1
View File
@@ -26,7 +26,8 @@ namespace y2024::day03
{ {
int32_t sum = 0; int32_t sum = 0;
std::vector<std::string> matches = Helper::GetAllRegexMatches(input, "mul\\([0-9]*,[0-9]*\\)"); 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; return sum;
} }
+2 -1
View File
@@ -50,7 +50,8 @@ namespace y2024::day07
int64_t Concat(int64_t i1, int64_t i2) int64_t Concat(int64_t i1, int64_t i2)
{ {
int64_t base = 10; int64_t base = 10;
while (i2 >= base) base *= 10; while (i2 >= base)
base *= 10;
return i1 * base + i2; return i1 * base + i2;
} }
+6 -3
View File
@@ -13,7 +13,8 @@ namespace y2024::day11
READ_INPUT(input) READ_INPUT(input)
{ {
Stones stones; Stones stones;
for (auto i : Input::ReadInt64s(input, ' ')) stones[i]++; for (auto i : Input::ReadInt64s(input, ' '))
stones[i]++;
return stones; return stones;
} }
@@ -33,7 +34,8 @@ namespace y2024::day11
{ {
int64_t pow = 1; int64_t pow = 1;
for (int j = 0; j < i; j++) pow *= 10; for (int j = 0; j < i; j++)
pow *= 10;
return pow; return pow;
} }
@@ -74,7 +76,8 @@ namespace y2024::day11
{ {
std::map<int64_t, int64_t> cpy = stones; 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; int64_t sum = 0;
for (auto& [cur, count] : cpy) for (auto& [cur, count] : cpy)
+2 -1
View File
@@ -21,7 +21,8 @@ namespace y2024::day15
map.map = Input::ReadArray2D(input); map.map = Input::ReadArray2D(input);
std::string str; std::string str;
while (std::getline(input, str)) map.input += str; while (std::getline(input, str))
map.input += str;
return map; 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); Helper::DijkstrasAllVisited(input, std::pair{input.Find('S'), Index2D{1, 0}}, Branch, Goal);
std::set<Index2D> solutions; 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(); return solutions.size();
} }
+4 -2
View File
@@ -92,13 +92,15 @@ namespace y2024::day23
FindLargest(input, computers, largest); FindLargest(input, computers, largest);
} }
for (const auto& s : largest) std::cout << s << ","; for (const auto& s : largest)
std::cout << s << ",";
std::cout << std::endl; std::cout << std::endl;
// Some arbitrary solution number to have some form of verification in the code. // 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) // Sum all letters of the computers (with a=0, b=1, etc)
int64_t sum = 0; 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; return sum;
} }
+1
View File
@@ -30,6 +30,7 @@
#include "common/lib/Json.h" #include "common/lib/Json.h"
#include "common/lib/Mapper.h" #include "common/lib/Mapper.h"
#include "common/lib/Math.h" #include "common/lib/Math.h"
#include "common/lib/Md5.h"
#include "common/lib/Timer.h" #include "common/lib/Timer.h"
#define TERM_RED "\x1B[31m" #define TERM_RED "\x1B[31m"
+15
View File
@@ -9,6 +9,20 @@ struct Index2D
int x = -1; int x = -1;
int y = -1; int y = -1;
void RotateCW()
{
int tmp = y;
y = x;
x = -tmp;
}
void RotateCCW()
{
int tmp = y;
y = -x;
x = tmp;
}
bool IsValid() bool IsValid()
{ {
return x != -1 && y != -1; return x != -1 && y != -1;
@@ -494,6 +508,7 @@ struct Array2D
{ {
os << array2D.Get(x, y); os << array2D.Get(x, y);
} }
if (y != array2D.height - 1)
os << std::endl; os << std::endl;
} }
return std::cout; return std::cout;
+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;
}
+54 -216
View File
@@ -4,12 +4,12 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <deque>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <map> #include <map>
#include <numeric> #include <numeric>
#include <regex>
#include <set> #include <set>
#include <vector> #include <vector>
@@ -18,11 +18,14 @@
#define BIT(x) (1 << (x)) #define BIT(x) (1 << (x))
template <typename T> template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container); static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container);
template <typename T> template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::set<T>& container); 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);
template <typename T, typename S> template <typename T, typename S>
static std::ostream& operator<<(std::ostream& stream, const std::map<T, S>& container); static std::ostream& operator<<(std::ostream& stream, const std::map<T, S>& container);
@@ -34,15 +37,8 @@ struct Helper
{ {
struct ScopedTraceErrorDisabler struct ScopedTraceErrorDisabler
{ {
ScopedTraceErrorDisabler() ScopedTraceErrorDisabler();
{ ~ScopedTraceErrorDisabler();
DisableTraceErrors();
}
~ScopedTraceErrorDisabler()
{
ResetTraceErrors();
}
}; };
template <typename T, typename S> template <typename T, typename S>
@@ -51,114 +47,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 bool StartsWith(const std::string_view& str, const std::string& prefix, size_t offset = 0) 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);
return str.substr(offset, prefix.size()) == prefix; 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 EndsWith(const std::string_view& str, const std::string& prefix) static bool IsDigit(char c);
{ static int GetNumberOfDigits(int n);
return str.substr(str.size() - prefix.size(), prefix.size()) == prefix; static int GetNumberOfDigits(int64_t n);
} static int64_t Pow10(int pow);
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)
@@ -298,7 +201,8 @@ struct Helper
{ {
std::map<State, int> visited; std::map<State, int> visited;
std::multimap<int, std::pair<int, State>> open; 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()) while (!open.empty())
{ {
auto it = open.begin(); auto it = open.begin();
@@ -340,7 +244,8 @@ struct Helper
{ {
std::map<State, int> visited; std::map<State, int> visited;
std::multimap<int, std::pair<int, std::vector<State>>> open; 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()) while (!open.empty())
{ {
const auto& [heuristicVal, state] = *open.begin(); const auto& [heuristicVal, state] = *open.begin();
@@ -527,15 +432,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)
@@ -551,81 +448,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) // Solve for N:
{ // remainder1 = N % mod1
if (c == 'v' || c == 'V') // remainder2 = N % mod2
return Index2D{0, 1}; static int64_t ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t remainder1, int64_t remainder2);
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};
}
static int64_t ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t res1, int64_t res2) // Solve for N:
{ // remainders = N % mods
int64_t i = res1; static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& remainders);
while (true)
{
if (i % mod2 == res2)
return i;
i += mod1; // Solve for N:
} // remainders = (start + N) % mods
return 0; 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) static uint64_t FastExponentiation(uint64_t start, uint64_t multiplication, uint64_t power, uint64_t mod);
{
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;
}
// Input - Puzzle input // Input - Puzzle input
// Function - bool(int64_t); // Function - bool(int64_t);
@@ -652,20 +495,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>
@@ -728,16 +562,20 @@ public:
static bool traceErrors; static bool traceErrors;
}; };
inline bool Helper::traceErrors = true; template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container)
template <typename T>
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container)
{ {
return Helper::Print(stream, container); return Helper::Print(stream, container);
} }
template <typename T> template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::set<T>& container) 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)
{ {
return Helper::Print(stream, container); return Helper::Print(stream, container);
} }
+39 -8
View File
@@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <iostream> #include <iostream>
#include <sstream>
#include <string> #include <string>
#include "Array2D.h" #include "Array2D.h"
@@ -12,9 +13,11 @@
static std::istream& operator>>(std::istream& stream, char const* pattern) static std::istream& operator>>(std::istream& stream, char const* pattern)
{ {
char c; 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') if (*pattern != '\0')
stream.setstate(std::ios::failbit); stream.setstate(std::ios::failbit);
@@ -36,7 +39,8 @@ struct Input
break; break;
width = (int)str.length(); width = (int)str.length();
height++; height++;
for (auto c : str) data.emplace_back(c); for (auto c : str)
data.emplace_back(c);
} }
return Array2D<char>(width, height, data); return Array2D<char>(width, height, data);
} }
@@ -51,7 +55,30 @@ struct Input
{ {
width = (int)str.length(); width = (int)str.length();
height++; 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);
}
static Array2D<int> ReadIntsAsArray2D(std::istream& input)
{
std::vector<int> data;
int width = 0;
int height = 0;
std::string str;
while (getline(input, str))
{
height++;
std::stringstream ss{str};
int i;
int size = 0;
while (ss >> i)
{
data.emplace_back(i);
size++;
}
width = size;
} }
return Array2D<int>(width, height, data); return Array2D<int>(width, height, data);
} }
@@ -70,7 +97,8 @@ struct Input
{ {
std::vector<int> ints; std::vector<int> ints;
std::string str; 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; return ints;
} }
@@ -78,7 +106,8 @@ struct Input
{ {
std::vector<int64_t> ints; std::vector<int64_t> ints;
std::string str; 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; return ints;
} }
@@ -86,7 +115,8 @@ struct Input
{ {
std::vector<std::string> strings; std::vector<std::string> strings;
std::string str; std::string str;
while (getline(input, str)) strings.emplace_back(str); while (getline(input, str))
strings.emplace_back(str);
return strings; return strings;
} }
@@ -94,7 +124,8 @@ struct Input
{ {
std::string result; std::string result;
std::string str; std::string str;
while (getline(input, str)) result += str + '\n'; while (getline(input, str))
result += str + '\n';
if (!result.empty()) if (!result.empty())
result.pop_back(); result.pop_back();
return result; 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; uint64_t stringStart = pos;
while (true) while (true)
{ {
while (pos < str.size() && str[pos] != '\"') pos++; while (pos < str.size() && str[pos] != '\"')
pos++;
if (pos >= str.size() || str[pos - 1] != '\\') if (pos >= str.size() || str[pos - 1] != '\\')
break; break;
} }
+170
View File
@@ -0,0 +1,170 @@
#include "common/lib/Md5.h"
#include <array>
#include <cstring>
#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];
}
uint32_t Md5::LeftRotate(uint32_t x, uint32_t n)
{
return (x << n) | (x >> (32 - 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
{
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
};
const static std::array<uint32_t, 64> S
{
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
};
// clang-format on
uint32_t A = a0;
uint32_t B = b0;
uint32_t C = c0;
uint32_t D = d0;
for (int i = 0; i < 64; i++)
{
uint32_t F;
uint32_t g;
if (i < 16)
{
F = (B & C) | ((~B) & D);
g = i;
}
else if (i < 32)
{
F = (D & B) | ((~D) & C);
g = (5 * i + 1) % 16;
}
else if (i < 48)
{
F = B ^ C ^ D;
g = (3 * i + 5) % 16;
}
else
{
F = C ^ (B | (~D));
g = (7 * i) % 16;
}
F = F + A + K[i] + BytesToUint32(data + g * 4);
A = D;
D = C;
C = B;
B = B + LeftRotate(F, S[i]);
}
a0 += A;
b0 += B;
c0 += C;
d0 += D;
}
void Md5::PrintUint32(std::ostream& os, uint32_t n)
{
os << std::hex;
os << std::setw(2) << std::setfill('0') << (n & 0xff);
os << std::setw(2) << std::setfill('0') << ((n >> 8) & 0xff);
os << std::setw(2) << std::setfill('0') << ((n >> 16) & 0xff);
os << std::setw(2) << std::setfill('0') << ((n >> 24) & 0xff);
os << std::dec;
}
std::string Md5::Hash(std::string str)
{
str += 0x80;
uint32_t a0 = 0x67452301;
uint32_t b0 = 0xefcdab89;
uint32_t c0 = 0x98badcfe;
uint32_t d0 = 0x10325476;
uint64_t size = str.size();
uint64_t bytesToAdd = (64 + 56 - (size % 64)) % 64;
// std::cout << str.size() << " " << bytesToAdd << std::endl;
int loops = (size + bytesToAdd + 8) / 64;
if ((size + bytesToAdd + 8) % 64 != 0)
loops++;
uint64_t offset = 0;
for (int i = 0; i < loops - 1; i++)
{
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;
}
uint8_t data[64];
std::memset(data, 0x0, 64);
if (str.size() >= offset)
std::memcpy(data, str.c_str() + offset, str.size() - offset);
for (int i = 0; i < 8; i++)
{
data[56 + i] = (((str.size() - 1) * 8) >> (i * 8)) & 0xff;
}
Transform(data, a0, b0, c0, d0);
std::stringstream ss{};
PrintUint32(ss, a0);
PrintUint32(ss, b0);
PrintUint32(ss, c0);
PrintUint32(ss, d0);
return ss.str();
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#include <string>
class Md5
{
public:
static std::string Hash(std::string str);
private:
static uint32_t BytesToUint32(uint8_t* data);
static uint32_t LeftRotate(uint32_t x, uint32_t n);
static void Transform(uint8_t* data, uint32_t& a0, uint32_t& b0, uint32_t& c0, uint32_t& d0);
static void PrintUint32(std::ostream& os, uint32_t n);
};