Compare commits
29 Commits
ce9bc29372
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 85f5de1be2 | |||
| 8162a46091 | |||
| ed11090cc2 | |||
| 7b2567a71e | |||
| c669dedbb7 | |||
| 5111d633b7 | |||
| 988c5552d0 | |||
| ab6c726ebc | |||
| 482794f994 | |||
| f26d9fd416 | |||
| 5d44b9484f | |||
| fc16b73a01 | |||
| e5acfb8635 | |||
| 3d9b19be4d | |||
| b60954656b | |||
| 474e662ebd | |||
| bf12e141a6 | |||
| c228e8e230 | |||
| fc76349588 | |||
| d71df0c742 | |||
| c8f0000c4d | |||
| 80eacc6ed2 | |||
| 9237445a13 | |||
| 4de110a517 | |||
| e8eb238682 | |||
| 9b09fb26aa | |||
| cd90db48be | |||
| 48023a811a | |||
| 9ef2809593 |
+1
-1
@@ -81,7 +81,7 @@ AllowShortEnumsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortNamespacesOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
|
||||
@@ -13,7 +13,7 @@ for item in $list; do
|
||||
dir="res/$year/day$day/input.txt"
|
||||
if [ ! -e $dir ]; then
|
||||
webitem=${day#0}
|
||||
echo "Fetching $dir"
|
||||
echo "Fetching $dir from https://adventofcode.com/$year/day/$webitem/input"
|
||||
tmp_file=$(mktemp)
|
||||
HTTP_CODE=$(curl -s -w "%{http_code}" -o "$tmp_file" --cookie cookies.txt https://adventofcode.com/$year/day/$webitem/input)
|
||||
if [ "$HTTP_CODE" -eq 200 ]; then
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
e => H
|
||||
e => O
|
||||
H => HO
|
||||
H => OH
|
||||
O => HH
|
||||
|
||||
HOHOHO
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
149
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
Hit Points: 13
|
||||
Damage: 8
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
inc b
|
||||
jio b, +2
|
||||
tpl b
|
||||
inc b
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
11
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
To continue, please consult the code grid in the manual. Enter the code at row 3, column 4.
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
R8, R4, R4, R8
|
||||
@@ -0,0 +1,4 @@
|
||||
ULL
|
||||
RRDDD
|
||||
LURDL
|
||||
UUUUD
|
||||
@@ -0,0 +1,3 @@
|
||||
5 10 25
|
||||
11 15 25
|
||||
8 25 20
|
||||
@@ -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]
|
||||
@@ -0,0 +1 @@
|
||||
abc
|
||||
@@ -0,0 +1,16 @@
|
||||
eedadn
|
||||
drvtee
|
||||
eandsr
|
||||
raavrd
|
||||
atevrs
|
||||
tsrnev
|
||||
sdttsa
|
||||
rasrtv
|
||||
nssdts
|
||||
ntnada
|
||||
svetve
|
||||
tesnvt
|
||||
vntsnd
|
||||
vrdear
|
||||
dvrsen
|
||||
enarar
|
||||
@@ -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
|
||||
@@ -0,0 +1,4 @@
|
||||
rect 3x2
|
||||
rotate column x=1 by 1
|
||||
rotate row y=0 by 4
|
||||
rotate column x=1 by 1
|
||||
@@ -0,0 +1 @@
|
||||
X(8x2)(3x3)ABCY
|
||||
@@ -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
|
||||
@@ -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.
|
||||
@@ -0,0 +1,6 @@
|
||||
cpy 41 a
|
||||
inc a
|
||||
inc a
|
||||
dec a
|
||||
jnz a 2
|
||||
dec a
|
||||
@@ -0,0 +1 @@
|
||||
10
|
||||
@@ -0,0 +1 @@
|
||||
abc
|
||||
@@ -0,0 +1,3 @@
|
||||
Disc #1 has 5 positions; at time=0, it is at position 4.
|
||||
Disc #2 has 2 positions; at time=0, it is at position 1.
|
||||
Disc #2 has 3 positions; at time=0, it is at position 2.
|
||||
@@ -0,0 +1 @@
|
||||
10000
|
||||
@@ -0,0 +1 @@
|
||||
ulqzkmiv
|
||||
@@ -0,0 +1 @@
|
||||
.^^.^.^^^^
|
||||
@@ -0,0 +1 @@
|
||||
5
|
||||
+250
-9
@@ -2,30 +2,271 @@
|
||||
|
||||
namespace y2015::day19
|
||||
{
|
||||
REGISTER_DAY(2015, Day19, std::vector<int>, int);
|
||||
struct Input
|
||||
{
|
||||
std::multimap<std::string, std::string> conversions;
|
||||
std::string molecule;
|
||||
};
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day19, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day19, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day19, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day19, Input, 2, 0);
|
||||
REGISTER_DAY(2015, Day19, Input, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day19, ExampleInput, 1, 7);
|
||||
REGISTER_TEST(2015, Day19, Input, 1, 576);
|
||||
// REGISTER_TEST_EXAMPLE(2015, Day19, ExampleInput, 2, 6);
|
||||
REGISTER_TEST(2015, Day19, Input, 2, 207);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
Input i;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
if (str.empty())
|
||||
break;
|
||||
|
||||
std::string from;
|
||||
std::string to;
|
||||
std::stringstream ss{str};
|
||||
ss >> from >> "=>" >> to;
|
||||
i.conversions.emplace(from, to);
|
||||
}
|
||||
return vec;
|
||||
getline(input, i.molecule);
|
||||
return i;
|
||||
}
|
||||
|
||||
std::vector<size_t> FindAll(const std::string& str, const std::string& find)
|
||||
{
|
||||
std::vector<size_t> positions;
|
||||
size_t pos = str.find(find);
|
||||
while (pos != std::string::npos)
|
||||
{
|
||||
positions.emplace_back(pos);
|
||||
pos = str.find(find, pos + 1);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
bool IsAtom(const std::string& sv)
|
||||
{
|
||||
std::regex regex{"^(e|[A-Z][a-z]?)$"};
|
||||
return std::regex_match(sv, regex);
|
||||
}
|
||||
|
||||
std::string Prettify(const std::string& molecule)
|
||||
{
|
||||
std::string result = molecule;
|
||||
for (size_t i = 0; i < result.size(); i++)
|
||||
{
|
||||
if (Helper::StartsWith(result, "Rn", i))
|
||||
{
|
||||
result.replace(i, 2, "(");
|
||||
i--;
|
||||
}
|
||||
else if (Helper::StartsWith(result, "Ar", i))
|
||||
{
|
||||
result.replace(i, 2, ")");
|
||||
i--;
|
||||
}
|
||||
else if (Helper::StartsWith(result, "Y", i))
|
||||
{
|
||||
result.replace(i, 1, ",");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> Solve(const std::string& molecule,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization);
|
||||
|
||||
void Solve(std::string& molecule,
|
||||
int& steps,
|
||||
size_t& offset,
|
||||
size_t pos,
|
||||
size_t size,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
std::string toReduce = molecule.substr(pos, size);
|
||||
auto [solveStr, solveSteps] = Solve(toReduce, conversions, memoization);
|
||||
Helper::Replace(molecule, toReduce.size(), solveStr, pos);
|
||||
steps += solveSteps;
|
||||
offset -= toReduce.size() - solveStr.size();
|
||||
}
|
||||
|
||||
void SolveRnAr(std::string& molecule,
|
||||
int& steps,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
// Solve all parts between Rn and Ar
|
||||
int stack = 0;
|
||||
size_t position = 0;
|
||||
for (size_t i = 0; i < molecule.size(); i++)
|
||||
{
|
||||
if (Helper::StartsWith(molecule, "Rn", i))
|
||||
{
|
||||
if (stack == 0)
|
||||
position = i + 2;
|
||||
i++;
|
||||
stack++;
|
||||
}
|
||||
else if (Helper::StartsWith(molecule, "Ar", i))
|
||||
{
|
||||
stack--;
|
||||
if (stack == 0)
|
||||
{
|
||||
Solve(molecule, steps, i, position, i - position, conversions, memoization);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SolveY(std::string& molecule,
|
||||
int& steps,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
// Reduce all parts split by Y
|
||||
int stack = 0;
|
||||
size_t position = 0;
|
||||
bool hasY = false;
|
||||
for (size_t i = 0; i < molecule.size(); i++)
|
||||
{
|
||||
if (Helper::StartsWith(molecule, "Rn", i))
|
||||
stack++;
|
||||
else if (Helper::StartsWith(molecule, "Ar", i))
|
||||
stack--;
|
||||
|
||||
if (Helper::StartsWith(molecule, "Y", i))
|
||||
{
|
||||
if (stack != 0)
|
||||
continue;
|
||||
|
||||
hasY = true;
|
||||
Solve(molecule, steps, i, position, i - position, conversions, memoization);
|
||||
position = i + 1;
|
||||
}
|
||||
}
|
||||
if (hasY)
|
||||
{
|
||||
size_t i = 0;
|
||||
Solve(molecule, steps, i, position, molecule.size(), conversions, memoization);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SolveAr(std::string& molecule,
|
||||
int& steps,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
// Reduce all parts ending with Ar (except the last one)
|
||||
for (size_t i = 0; i < molecule.size(); i++)
|
||||
{
|
||||
if (Helper::StartsWith(molecule, "Ar", i) && i != molecule.size() - 2)
|
||||
{
|
||||
Solve(molecule, steps, i, 0, i + 2, conversions, memoization);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<std::string, int> SolveToAtom(const std::string& molecule,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
if (IsAtom(molecule))
|
||||
return {molecule, 0};
|
||||
|
||||
auto it = memoization.find(molecule);
|
||||
if (it != memoization.end())
|
||||
return it->second;
|
||||
|
||||
int steps = std::numeric_limits<int>::max();
|
||||
std::string result = ".";
|
||||
for (const auto& [from, to] : conversions)
|
||||
{
|
||||
std::vector<size_t> positions = FindAll(molecule, from);
|
||||
for (const auto& pos : positions)
|
||||
{
|
||||
std::string moleculeCpy = molecule;
|
||||
moleculeCpy.replace(pos, from.size(), to);
|
||||
auto [solveStr, solveSteps] = SolveToAtom(moleculeCpy, conversions, memoization);
|
||||
if (solveStr == ".")
|
||||
continue;
|
||||
if (solveSteps < steps)
|
||||
{
|
||||
steps = solveSteps;
|
||||
result = solveStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memoization[molecule] = {result, steps + 1};
|
||||
return {result, steps + 1};
|
||||
}
|
||||
|
||||
void SolveToAtom(std::string& molecule,
|
||||
int& steps,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
// Reduce the rest
|
||||
auto [solveStr, solveSteps] = SolveToAtom(molecule, conversions, memoization);
|
||||
if (solveStr == ".")
|
||||
{
|
||||
std::cout << "No solution found for: " << molecule << std::endl;
|
||||
}
|
||||
Helper::Replace(molecule, molecule.size(), solveStr, 0);
|
||||
steps += solveSteps;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> Solve(const std::string& molecule,
|
||||
const std::multimap<std::string, std::string>& conversions,
|
||||
std::map<std::string, std::pair<std::string, int>>& memoization)
|
||||
{
|
||||
if (IsAtom(molecule))
|
||||
return {molecule, 0};
|
||||
|
||||
std::string result = molecule;
|
||||
int steps = 0;
|
||||
|
||||
SolveRnAr(result, steps, conversions, memoization);
|
||||
if (SolveY(result, steps, conversions, memoization))
|
||||
return {result, steps};
|
||||
SolveAr(result, steps, conversions, memoization);
|
||||
SolveToAtom(result, steps, conversions, memoization);
|
||||
|
||||
return {result, steps};
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
std::set<std::string> uniques;
|
||||
for (const auto& [from, to] : input.conversions)
|
||||
{
|
||||
std::vector<size_t> positions = FindAll(input.molecule, from);
|
||||
for (const auto& pos : positions)
|
||||
{
|
||||
std::string str = input.molecule;
|
||||
Helper::Replace(str, from.size(), to, pos);
|
||||
uniques.emplace(str);
|
||||
}
|
||||
}
|
||||
return uniques.size();
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
std::multimap<std::string, std::string> reverse;
|
||||
for (const auto& [from, to] : input.conversions)
|
||||
{
|
||||
reverse.emplace(to, from);
|
||||
}
|
||||
|
||||
std::map<std::string, std::pair<std::string, int>> memoization;
|
||||
return Solve(input.molecule, reverse, memoization).second;
|
||||
}
|
||||
}
|
||||
|
||||
+33
-12
@@ -2,30 +2,51 @@
|
||||
|
||||
namespace y2015::day20
|
||||
{
|
||||
REGISTER_DAY(2015, Day20, std::vector<int>, int);
|
||||
REGISTER_DAY(2015, Day20, int, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day20, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day20, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day20, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day20, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day20, ExampleInput, 1, 8);
|
||||
REGISTER_TEST(2015, Day20, Input, 1, 776160);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day20, ExampleInput, 2, 8);
|
||||
REGISTER_TEST(2015, Day20, Input, 2, 786240);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
int i = 0;
|
||||
input >> i;
|
||||
return i;
|
||||
}
|
||||
return vec;
|
||||
|
||||
int Solve(int input, int visits, int gifts)
|
||||
{
|
||||
// Assumption is that the solution can be found in 'limit' amount houses
|
||||
int limit = 1'000'000;
|
||||
if (visits == -1)
|
||||
visits = limit;
|
||||
std::vector<int> data(limit, 0);
|
||||
for (int i = 1; i < limit; i++)
|
||||
{
|
||||
for (int j = 1; j <= visits; j++)
|
||||
{
|
||||
if (i * j - 1 >= limit)
|
||||
break;
|
||||
data[i * j - 1] += i * gifts;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < data.size(); i++)
|
||||
{
|
||||
if (data[i] > input)
|
||||
return i + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, -1, 10);
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, 50, 11);
|
||||
}
|
||||
}
|
||||
|
||||
+98
-11
@@ -2,30 +2,117 @@
|
||||
|
||||
namespace y2015::day21
|
||||
{
|
||||
REGISTER_DAY(2015, Day21, std::vector<int>, int);
|
||||
struct Entity
|
||||
{
|
||||
int hp;
|
||||
int dmg;
|
||||
int armor;
|
||||
};
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day21, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day21, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day21, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day21, Input, 2, 0);
|
||||
struct Item
|
||||
{
|
||||
int cost;
|
||||
int dmg;
|
||||
int armor;
|
||||
};
|
||||
|
||||
REGISTER_DAY(2015, Day21, Entity, int);
|
||||
|
||||
REGISTER_TEST(2015, Day21, Input, 1, 91);
|
||||
REGISTER_TEST(2015, Day21, Input, 2, 158);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
Entity monster;
|
||||
input >> "Hit Points: " >> monster.hp >> "\n";
|
||||
input >> "Damage: " >> monster.dmg >> "\n";
|
||||
input >> "Armor: " >> monster.armor >> "\n";
|
||||
return monster;
|
||||
}
|
||||
|
||||
bool Win(Entity player, Entity monster)
|
||||
{
|
||||
int playerDmgPerRound = std::max(1, (player.dmg - monster.armor));
|
||||
int monsterDmgPerRound = std::max(1, (monster.dmg - player.armor));
|
||||
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
monster.hp -= playerDmgPerRound;
|
||||
if (monster.hp <= 0)
|
||||
return true;
|
||||
}
|
||||
return vec;
|
||||
else
|
||||
{
|
||||
player.hp -= monsterDmgPerRound;
|
||||
if (player.hp <= 0)
|
||||
return false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Item> weapons{Item{8, 4, 0}, Item{10, 5, 0}, Item{25, 6, 0}, Item{40, 7, 0}, Item{74, 8, 0}};
|
||||
std::vector<Item> armors{
|
||||
Item{13, 0, 1}, Item{31, 0, 2}, Item{53, 0, 3}, Item{75, 0, 4}, Item{102, 0, 5}, Item{0, 0, 0}};
|
||||
std::vector<Item> rings{Item{25, 0, 1},
|
||||
Item{50, 0, 2},
|
||||
Item{100, 0, 3},
|
||||
Item{20, 0, 1},
|
||||
Item{40, 0, 2},
|
||||
Item{80, 0, 3},
|
||||
Item{0, 0, 0},
|
||||
Item{0, 0, 0}};
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
int minCost = std::numeric_limits<int>::max();
|
||||
for (auto& weapon : weapons)
|
||||
{
|
||||
for (auto& armor : armors)
|
||||
{
|
||||
for (int i = 0; i < rings.size(); i++)
|
||||
{
|
||||
for (int j = i + 1; j < rings.size(); j++)
|
||||
{
|
||||
int cost = weapon.cost + armor.cost + rings[i].cost + rings[j].cost;
|
||||
int dmgStat = weapon.dmg + armor.dmg + rings[i].dmg + rings[j].dmg;
|
||||
int armorStat = weapon.armor + armor.armor + rings[i].armor + rings[j].armor;
|
||||
if (cost > minCost)
|
||||
continue;
|
||||
if (Win(Entity{100, dmgStat, armorStat}, input))
|
||||
minCost = cost;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return minCost;
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
int maxCost = 0;
|
||||
for (auto& weapon : weapons)
|
||||
{
|
||||
for (auto& armor : armors)
|
||||
{
|
||||
for (int i = 0; i < rings.size(); i++)
|
||||
{
|
||||
for (int j = i + 1; j < rings.size(); j++)
|
||||
{
|
||||
int cost = weapon.cost + armor.cost + rings[i].cost + rings[j].cost;
|
||||
int dmgStat = weapon.dmg + armor.dmg + rings[i].dmg + rings[j].dmg;
|
||||
int armorStat = weapon.armor + armor.armor + rings[i].armor + rings[j].armor;
|
||||
if (cost < maxCost)
|
||||
continue;
|
||||
if (!Win(Entity{100, dmgStat, armorStat}, input))
|
||||
maxCost = cost;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxCost;
|
||||
}
|
||||
}
|
||||
|
||||
+145
-12
@@ -2,30 +2,163 @@
|
||||
|
||||
namespace y2015::day22
|
||||
{
|
||||
REGISTER_DAY(2015, Day22, std::vector<int>, int);
|
||||
struct Player
|
||||
{
|
||||
int hp;
|
||||
int armor;
|
||||
int mana;
|
||||
};
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day22, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day22, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day22, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day22, Input, 2, 0);
|
||||
struct Monster
|
||||
{
|
||||
int hp;
|
||||
int dmg;
|
||||
};
|
||||
|
||||
REGISTER_DAY(2015, Day22, Monster, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day22, ExampleInput, 1, 212);
|
||||
REGISTER_TEST(2015, Day22, Input, 1, 900);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day22, ExampleInput, 2, 212);
|
||||
REGISTER_TEST(2015, Day22, Input, 2, 1216);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
Monster monster;
|
||||
input >> "Hit Points: " >> monster.hp >> "\n";
|
||||
input >> "Damage: " >> monster.dmg >> "\n";
|
||||
return monster;
|
||||
}
|
||||
return vec;
|
||||
|
||||
struct Spell
|
||||
{
|
||||
int cost;
|
||||
int timer;
|
||||
int dmg;
|
||||
int armor;
|
||||
int manaGain;
|
||||
int heal;
|
||||
};
|
||||
|
||||
struct State
|
||||
{
|
||||
Player player;
|
||||
Monster monster;
|
||||
std::vector<int> spellTimers;
|
||||
|
||||
bool operator<(const State& other) const
|
||||
{
|
||||
if (player.hp != other.player.hp)
|
||||
return player.hp < other.player.hp;
|
||||
if (player.armor != other.player.armor)
|
||||
return player.armor < other.player.armor;
|
||||
if (player.mana != other.player.mana)
|
||||
return player.mana < other.player.mana;
|
||||
|
||||
if (monster.hp != other.monster.hp)
|
||||
return monster.hp < other.monster.hp;
|
||||
if (monster.dmg != other.monster.dmg)
|
||||
return monster.dmg < other.monster.dmg;
|
||||
|
||||
return spellTimers < other.spellTimers;
|
||||
}
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
// cost timer dmg armor manaGain heal
|
||||
const std::vector<Spell> spells{Spell{53, 1, 4, 0, 0, 0},
|
||||
Spell{73, 1, 2, 0, 0, 2},
|
||||
Spell{113, 6, 0, 7, 0, 0},
|
||||
Spell{173, 6, 3, 0, 0, 0},
|
||||
Spell{229, 5, 0, 0, 101, 0}};
|
||||
// clang-format-on
|
||||
|
||||
void ApplyEffects(State& state)
|
||||
{
|
||||
for (int i = 0; i < state.spellTimers.size(); i++)
|
||||
{
|
||||
if (state.spellTimers[i] == 0)
|
||||
continue;
|
||||
|
||||
const auto& spell = spells[i];
|
||||
state.spellTimers[i]--;
|
||||
state.monster.hp -= spell.dmg;
|
||||
state.player.hp += spell.heal;
|
||||
state.player.mana += spell.manaGain;
|
||||
if (state.spellTimers[i] == 0)
|
||||
state.player.armor -= spell.armor;
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<bool, int> PlayerTurn(State state, bool hardmode, std::map<State, std::pair<bool, int>>& memoization)
|
||||
{
|
||||
// Apply rules at start of turn
|
||||
if (hardmode)
|
||||
state.player.hp--;
|
||||
if (state.player.hp <= 0)
|
||||
return {false, 0};
|
||||
ApplyEffects(state);
|
||||
|
||||
if (state.monster.hp <= 0)
|
||||
return {true, 0};
|
||||
|
||||
auto it = memoization.find(state);
|
||||
if (it != memoization.end())
|
||||
return it->second;
|
||||
|
||||
bool canWin = false;
|
||||
int minManaCost = std::numeric_limits<int>::max();
|
||||
for (int i = 0; i < spells.size(); i++)
|
||||
{
|
||||
// Spell is already active
|
||||
if (state.spellTimers[i] > 0)
|
||||
continue;
|
||||
|
||||
const auto& spell = spells[i];
|
||||
if (state.player.mana < spell.cost)
|
||||
continue;
|
||||
|
||||
// Cast spell
|
||||
State newState = state;
|
||||
newState.spellTimers[i] = spell.timer;
|
||||
newState.player.mana -= spell.cost;
|
||||
newState.player.armor += spell.armor;
|
||||
|
||||
// Boss turn
|
||||
ApplyEffects(newState);
|
||||
if (newState.monster.hp <= 0)
|
||||
{
|
||||
if (spell.cost < minManaCost)
|
||||
{
|
||||
minManaCost = spell.cost;
|
||||
canWin = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
newState.player.hp -= std::max(1, newState.monster.dmg - newState.player.armor);
|
||||
|
||||
// Player turn
|
||||
auto result = PlayerTurn(newState, hardmode, memoization);
|
||||
if (result.first && result.second + spell.cost < minManaCost)
|
||||
{
|
||||
canWin = true;
|
||||
minManaCost = result.second + spell.cost;
|
||||
}
|
||||
}
|
||||
|
||||
memoization[state] = {canWin, minManaCost};
|
||||
return {canWin, minManaCost};
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
std::map<State, std::pair<bool, int>> memoization;
|
||||
return PlayerTurn(State{Player{50, 0, 500}, input, {0, 0, 0, 0, 0}}, false, memoization).second;
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
std::map<State, std::pair<bool, int>> memoization;
|
||||
return PlayerTurn(State{Player{50, 0, 500}, input, {0, 0, 0, 0, 0}}, true, memoization).second;
|
||||
}
|
||||
}
|
||||
|
||||
+99
-8
@@ -2,30 +2,121 @@
|
||||
|
||||
namespace y2015::day23
|
||||
{
|
||||
REGISTER_DAY(2015, Day23, std::vector<int>, int);
|
||||
enum class Operation
|
||||
{
|
||||
Half,
|
||||
Triple,
|
||||
Increment,
|
||||
Jump,
|
||||
JumpIfEven,
|
||||
JumpIfOne,
|
||||
};
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day23, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day23, Input, 2, 0);
|
||||
struct Instruction
|
||||
{
|
||||
Operation operation;
|
||||
char reg;
|
||||
int argument;
|
||||
};
|
||||
|
||||
REGISTER_DAY(2015, Day23, std::vector<Instruction>, uint64_t);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 1, 2);
|
||||
REGISTER_TEST(2015, Day23, Input, 1, 255);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 2, 2);
|
||||
REGISTER_TEST(2015, Day23, Input, 2, 334);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::vector<Instruction> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
Instruction instruction;
|
||||
std::stringstream ss{str};
|
||||
std::string s;
|
||||
char c;
|
||||
ss >> s;
|
||||
// std::cout << s << std::endl;
|
||||
if (s == "hlf")
|
||||
{
|
||||
instruction.operation = Operation::Half;
|
||||
ss >> instruction.reg;
|
||||
}
|
||||
else if (s == "tpl")
|
||||
{
|
||||
instruction.operation = Operation::Triple;
|
||||
ss >> instruction.reg;
|
||||
}
|
||||
else if (s == "inc")
|
||||
{
|
||||
instruction.operation = Operation::Increment;
|
||||
ss >> instruction.reg;
|
||||
}
|
||||
else if (s == "jmp")
|
||||
{
|
||||
instruction.operation = Operation::Jump;
|
||||
ss >> instruction.argument;
|
||||
}
|
||||
else if (s == "jie")
|
||||
{
|
||||
instruction.operation = Operation::JumpIfEven;
|
||||
ss >> instruction.reg >> ", " >> instruction.argument;
|
||||
}
|
||||
else if (s == "jio")
|
||||
{
|
||||
instruction.operation = Operation::JumpIfOne;
|
||||
ss >> instruction.reg >> ", " >> instruction.argument;
|
||||
}
|
||||
vec.emplace_back(instruction);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
int64_t Run(const std::vector<Instruction>& instructions, std::map<char, uint64_t> registers)
|
||||
{
|
||||
size_t pc = 0;
|
||||
while (pc < instructions.size())
|
||||
{
|
||||
const auto& instruction = instructions[pc];
|
||||
Operation op = instruction.operation;
|
||||
switch (op)
|
||||
{
|
||||
case Operation::Half:
|
||||
registers[instruction.reg] = registers[instruction.reg] / 2;
|
||||
break;
|
||||
case Operation::Triple:
|
||||
registers[instruction.reg] = registers[instruction.reg] * 3;
|
||||
break;
|
||||
case Operation::Increment:
|
||||
registers[instruction.reg]++;
|
||||
break;
|
||||
case Operation::Jump:
|
||||
pc += instruction.argument - 1;
|
||||
break;
|
||||
case Operation::JumpIfEven:
|
||||
if (registers[instruction.reg] % 2 == 0)
|
||||
pc += instruction.argument - 1;
|
||||
break;
|
||||
case Operation::JumpIfOne:
|
||||
if (registers[instruction.reg] == 1)
|
||||
pc += instruction.argument - 1;
|
||||
break;
|
||||
}
|
||||
pc++;
|
||||
}
|
||||
return registers['b'];
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
return Run(input, std::map<char, uint64_t>{});
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
std::map<char, uint64_t> registers;
|
||||
registers['a'] = 1;
|
||||
return Run(input, registers);
|
||||
}
|
||||
}
|
||||
|
||||
+118
-12
@@ -2,30 +2,136 @@
|
||||
|
||||
namespace y2015::day24
|
||||
{
|
||||
REGISTER_DAY(2015, Day24, std::vector<int>, int);
|
||||
REGISTER_DAY(2015, Day24, std::vector<int>, uint64_t);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day24, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day24, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day24, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day24, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day24, ExampleInput, 1, 99);
|
||||
REGISTER_TEST(2015, Day24, Input, 1, 11266889531);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day24, ExampleInput, 2, 44);
|
||||
REGISTER_TEST(2015, Day24, Input, 2, 77387711);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
return Input::ReadInts(input);
|
||||
}
|
||||
return vec;
|
||||
|
||||
void GetAllSumCombinations(std::vector<int>& listLeft,
|
||||
std::vector<int>& currentList,
|
||||
std::vector<std::vector<int>>& result,
|
||||
int sumLeft,
|
||||
size_t offset)
|
||||
{
|
||||
if (sumLeft < 0)
|
||||
return;
|
||||
if (sumLeft == 0)
|
||||
{
|
||||
result.emplace_back(currentList);
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = offset; i < listLeft.size(); i++)
|
||||
{
|
||||
if (listLeft[i] == 0)
|
||||
continue;
|
||||
if (sumLeft < listLeft[i])
|
||||
break;
|
||||
currentList.emplace_back(listLeft[i]);
|
||||
int number = listLeft[i];
|
||||
listLeft[i] = 0;
|
||||
GetAllSumCombinations(listLeft, currentList, result, sumLeft - number, i + 1);
|
||||
currentList.pop_back();
|
||||
listLeft[i] = number;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<int>> GetAllSumCombinations(std::vector<int> list, int sum, int groups)
|
||||
{
|
||||
std::vector<int> currentList{};
|
||||
std::vector<std::vector<int>> result{};
|
||||
GetAllSumCombinations(list, currentList, result, sum / groups, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Overlap(const std::vector<int>& list1, const std::vector<int>& list2)
|
||||
{
|
||||
size_t i1 = 0;
|
||||
size_t i2 = 0;
|
||||
while (i1 < list1.size() && i2 < list2.size())
|
||||
{
|
||||
if (list1[i1] == list2[i2])
|
||||
return true;
|
||||
|
||||
if (list1[i1] < list2[i2])
|
||||
i1++;
|
||||
else
|
||||
i2++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Overlap(const std::vector<int>& combination, const std::vector<const std::vector<int>*>& combinationList)
|
||||
{
|
||||
for (const auto& elem : combinationList)
|
||||
{
|
||||
if (Overlap(combination, *elem))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool HasSolution(const std::vector<std::vector<int>>& sumCombinations,
|
||||
const std::vector<const std::vector<int>*>& list,
|
||||
size_t offset,
|
||||
int groupsLeft)
|
||||
{
|
||||
for (size_t i = offset; i < sumCombinations.size(); i++)
|
||||
{
|
||||
if (Overlap(sumCombinations[i], list))
|
||||
continue;
|
||||
if (groupsLeft == 1)
|
||||
return true;
|
||||
|
||||
std::vector<const std::vector<int>*> newList = list;
|
||||
newList.emplace_back(&sumCombinations[i]);
|
||||
if (HasSolution(sumCombinations, newList, i + 1, groupsLeft - 1))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t Solve(const std::vector<int>& list, int groups)
|
||||
{
|
||||
int sum = Helper::Sum<int>(list);
|
||||
std::vector<std::vector<int>> sumCombinations = GetAllSumCombinations(list, sum, groups);
|
||||
|
||||
size_t minSize = std::numeric_limits<size_t>::max();
|
||||
for (size_t i = 0; i < sumCombinations.size(); i++)
|
||||
{
|
||||
minSize = std::min(minSize, sumCombinations[i].size());
|
||||
}
|
||||
|
||||
uint64_t minProd = std::numeric_limits<uint64_t>::max();
|
||||
for (size_t i = 0; i < sumCombinations.size(); i++)
|
||||
{
|
||||
if (sumCombinations[i].size() != minSize)
|
||||
continue;
|
||||
size_t product = Helper::Product<uint64_t>(sumCombinations[i]);
|
||||
if (product >= minProd)
|
||||
continue;
|
||||
|
||||
if (HasSolution(sumCombinations, {&sumCombinations[i]}, 0, groups - 1))
|
||||
minProd = product;
|
||||
}
|
||||
|
||||
return minProd;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, 3);
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
return Solve(input, 4);
|
||||
}
|
||||
}
|
||||
|
||||
+16
-9
@@ -2,26 +2,33 @@
|
||||
|
||||
namespace y2015::day25
|
||||
{
|
||||
REGISTER_DAY(2015, Day25, std::vector<int>, int);
|
||||
using Pair = std::pair<int, int>;
|
||||
REGISTER_DAY(2015, Day25, Pair, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2015, Day25, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2015, Day25, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day25, ExampleInput, 1, 7981243);
|
||||
REGISTER_TEST(2015, Day25, Input, 1, 8997277);
|
||||
REGISTER_TEST_EXAMPLE(2015, Day25, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2015, Day25, Input, 2, 0);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
int i1;
|
||||
int i2;
|
||||
input >> "To continue, please consult the code grid in the manual. Enter the code at row " >> i2 >> ", column " >>
|
||||
i1;
|
||||
|
||||
return std::pair{i1, i2};
|
||||
}
|
||||
return vec;
|
||||
|
||||
int GetPower(const std::pair<int, int>& coordinate)
|
||||
{
|
||||
return (coordinate.first + coordinate.second - 1) * (coordinate.first + coordinate.second - 1 + 1) / 2 + 1 -
|
||||
coordinate.second;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
return Helper::FastExponentiation(20151125, 252533, GetPower(input) - 1, 33554393);
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 "";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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];
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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;
|
||||
|
||||
+1
-1
@@ -93,7 +93,7 @@ namespace y2021::day13
|
||||
{
|
||||
board[index] = '#';
|
||||
}
|
||||
std::cout << board;
|
||||
std::cout << board << std::endl;
|
||||
// Not the "correct" output to part 2, but it verifies something at least
|
||||
return indices.size();
|
||||
}
|
||||
|
||||
+2
-1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "common/lib/Json.h"
|
||||
#include "common/lib/Mapper.h"
|
||||
#include "common/lib/Math.h"
|
||||
#include "common/lib/Md5.h"
|
||||
#include "common/lib/Timer.h"
|
||||
|
||||
#define TERM_RED "\x1B[31m"
|
||||
|
||||
@@ -9,6 +9,20 @@ struct Index2D
|
||||
int x = -1;
|
||||
int y = -1;
|
||||
|
||||
void RotateCW()
|
||||
{
|
||||
int tmp = y;
|
||||
y = x;
|
||||
x = -tmp;
|
||||
}
|
||||
|
||||
void RotateCCW()
|
||||
{
|
||||
int tmp = y;
|
||||
y = -x;
|
||||
x = tmp;
|
||||
}
|
||||
|
||||
bool IsValid()
|
||||
{
|
||||
return x != -1 && y != -1;
|
||||
@@ -494,6 +508,7 @@ struct Array2D
|
||||
{
|
||||
os << array2D.Get(x, y);
|
||||
}
|
||||
if (y != array2D.height - 1)
|
||||
os << std::endl;
|
||||
}
|
||||
return std::cout;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
template <typename Key, typename Value>
|
||||
class Graph
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
+56
-193
@@ -1,13 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <regex>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
@@ -16,11 +18,14 @@
|
||||
|
||||
#define BIT(x) (1 << (x))
|
||||
|
||||
template <typename T>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container);
|
||||
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::set<T>& 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);
|
||||
|
||||
template <typename T, typename S>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::map<T, S>& container);
|
||||
@@ -32,15 +37,8 @@ struct Helper
|
||||
{
|
||||
struct ScopedTraceErrorDisabler
|
||||
{
|
||||
ScopedTraceErrorDisabler()
|
||||
{
|
||||
DisableTraceErrors();
|
||||
}
|
||||
|
||||
~ScopedTraceErrorDisabler()
|
||||
{
|
||||
ResetTraceErrors();
|
||||
}
|
||||
ScopedTraceErrorDisabler();
|
||||
~ScopedTraceErrorDisabler();
|
||||
};
|
||||
|
||||
template <typename T, typename S>
|
||||
@@ -49,109 +47,21 @@ struct Helper
|
||||
t.erase(std::remove_if(t.begin(), t.end(), func), t.end());
|
||||
}
|
||||
|
||||
static bool StartsWith(const std::string_view& str, const std::string& prefix)
|
||||
{
|
||||
return str.substr(0, prefix.size()) == prefix;
|
||||
}
|
||||
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 EndsWith(const std::string_view& str, const std::string& prefix)
|
||||
{
|
||||
return str.substr(str.size() - prefix.size(), prefix.size()) == prefix;
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -291,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();
|
||||
@@ -333,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();
|
||||
@@ -520,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)
|
||||
@@ -544,63 +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);
|
||||
|
||||
// Input - Puzzle input
|
||||
// Function - bool(int64_t);
|
||||
@@ -627,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>
|
||||
@@ -703,16 +562,20 @@ public:
|
||||
static bool traceErrors;
|
||||
};
|
||||
|
||||
inline bool Helper::traceErrors = true;
|
||||
|
||||
template <typename T>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container)
|
||||
template <typename... Args>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container)
|
||||
{
|
||||
return Helper::Print(stream, container);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static std::ostream& operator<<(std::ostream& stream, const std::set<T>& 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)
|
||||
{
|
||||
return Helper::Print(stream, container);
|
||||
}
|
||||
|
||||
+39
-8
@@ -3,6 +3,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "Array2D.h"
|
||||
@@ -12,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);
|
||||
@@ -36,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);
|
||||
}
|
||||
@@ -51,7 +55,30 @@ 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -70,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;
|
||||
}
|
||||
|
||||
@@ -78,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;
|
||||
}
|
||||
|
||||
@@ -86,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;
|
||||
}
|
||||
|
||||
@@ -94,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;
|
||||
|
||||
@@ -388,7 +388,7 @@ public:
|
||||
else if (json.IsInt())
|
||||
os << json.GetInt64();
|
||||
else if (json.IsBool())
|
||||
os << json.GetBool() ? "true" : "false";
|
||||
os << (json.GetBool() ? "true" : "false");
|
||||
else if (json.IsNull())
|
||||
os << "null";
|
||||
return os;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "JsonUtil.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
#include "Json.h"
|
||||
|
||||
@@ -98,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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user