Compare commits

...

3 Commits

Author SHA1 Message Date
Thraix 85f5de1be2 Add solution for 2016 Day 19 2026-08-31 22:21:13 +02:00
Thraix 8162a46091 Add solution for 2016 Day 18 2026-08-31 20:51:09 +02:00
Thraix ed11090cc2 Add solution for 2016 Day 17
- Fix Md5 algorithm
2026-08-30 22:52:01 +02:00
8 changed files with 196 additions and 35 deletions
+1
View File
@@ -0,0 +1 @@
ulqzkmiv
+1
View File
@@ -0,0 +1 @@
.^^.^.^^^^
+1
View File
@@ -0,0 +1 @@
5
+64 -10
View File
@@ -2,30 +2,84 @@
namespace y2016::day17
{
REGISTER_DAY(2016, Day17, std::vector<int>, int);
REGISTER_DAY(2016, Day17, std::string, std::string);
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day17, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day17, Input, 2, 0);
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 1, "DRURDRUDDLLDLUURRDULRLDUUDDDRR");
REGISTER_TEST(2016, Day17, Input, 1, "DUDDRLRRRD");
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 2, "830");
REGISTER_TEST(2016, Day17, Input, 2, "578");
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
getline(input, str);
return str;
}
return vec;
struct State
{
std::string str{""};
int x{0};
int y{0};
};
bool IsOpen(char c)
{
return c >= 'b' && c <= 'f';
}
void Branch(const State& state, std::queue<State>& queue)
{
std::string hash = Md5::Hash(state.str);
if (state.y > 0 && IsOpen(hash[0]))
queue.emplace(State{state.str + "U", state.x, state.y - 1});
if (state.y < 3 && IsOpen(hash[1]))
queue.emplace(State{state.str + "D", state.x, state.y + 1});
if (state.x > 0 && IsOpen(hash[2]))
queue.emplace(State{state.str + "L", state.x - 1, state.y});
if (state.x < 3 && IsOpen(hash[3]))
queue.emplace(State{state.str + "R", state.x + 1, state.y});
}
bool IsGoal(const State& state)
{
return state.x == 3 && state.y == 3;
}
OUTPUT1(input)
{
return 0;
std::queue<State> queue;
queue.emplace(State{input, 0, 0});
while (!queue.empty())
{
State state = queue.front();
queue.pop();
if (IsGoal(state))
return state.str.substr(input.size());
Branch(state, queue);
}
return "";
}
OUTPUT2(input)
{
return 0;
std::queue<State> queue;
queue.emplace(State{input, 0, 0});
int longest = 0;
while (!queue.empty())
{
State state = queue.front();
queue.pop();
if (IsGoal(state))
{
longest = state.str.size() - input.size();
continue;
}
Branch(state, queue);
}
return std::to_string(longest);
}
}
+37 -11
View File
@@ -2,30 +2,56 @@
namespace y2016::day18
{
REGISTER_DAY(2016, Day18, std::vector<int>, int);
REGISTER_DAY(2016, Day18, std::string, int);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day18, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day18, Input, 2, 0);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 1, 38);
REGISTER_TEST(2016, Day18, Input, 1, 1913);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 2, 1935478);
REGISTER_TEST(2016, Day18, Input, 2, 19993564);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
getline(input, str);
return str;
}
return vec;
int Solve(const std::string& str, int rows)
{
int safes = std::count(str.begin(), str.end(), '.');
std::vector<char> row(str.size() + 2, false);
for (int i = 0; i < str.size(); i++)
row[i + 1] = str[i] == '^';
for (int i = 1; i < rows; i++)
{
std::vector<char> newRow(row.size(), false);
for (int j = 1; j < newRow.size() - 1; j++)
{
bool isTrap = false;
if (row[j - 1] && row[j] && !row[j + 1])
newRow[j] = true;
else if (!row[j - 1] && row[j] && row[j + 1])
newRow[j] = true;
else if (row[j - 1] && !row[j] && !row[j + 1])
newRow[j] = true;
else if (!row[j - 1] && !row[j] && row[j + 1])
newRow[j] = true;
else
safes++;
}
row = newRow;
}
return safes;
}
OUTPUT1(input)
{
return 0;
return Solve(input, isExample ? 10 : 40);
}
OUTPUT2(input)
{
return 0;
return Solve(input, 400000);
}
}
+51 -13
View File
@@ -2,30 +2,68 @@
namespace y2016::day19
{
REGISTER_DAY(2016, Day19, std::vector<int>, int);
REGISTER_DAY(2016, Day19, int, int);
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day19, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day19, Input, 2, 0);
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 1, 3);
REGISTER_TEST(2016, Day19, Input, 1, 1842613);
REGISTER_TEST_EXAMPLE(2016, Day19, ExampleInput, 2, 2);
REGISTER_TEST(2016, Day19, Input, 2, 1424135);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
{
}
return vec;
int i;
input >> i;
return i;
}
OUTPUT1(input)
{
return 0;
std::vector<int> elves(input, 0);
for (int i = 0; i < input; i++)
{
elves[i] = i + 1;
}
int offset = 0;
while (elves.size() > 1)
{
std::vector<int> newElves;
for (int i = offset; i < elves.size(); i += 2)
{
newElves.emplace_back(elves[i]);
}
offset = (offset + (elves.size() % 2)) % 2;
elves = newElves;
}
return elves[0];
}
OUTPUT2(input)
{
return 0;
std::deque<int> start;
std::deque<int> end;
for (int i = 0; i < input / 2; i++)
{
start.push_back(i + 1);
}
for (int i = input / 2; i < input; i++)
{
end.push_back(i + 1);
}
while (!start.empty())
{
end.pop_front();
end.push_back(start.front());
start.pop_front();
if (end.size() - start.size() == 2)
{
start.push_back(end.front());
end.pop_front();
}
}
return end.front();
}
}
+10
View File
@@ -4,6 +4,7 @@
#include <algorithm>
#include <array>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
@@ -20,6 +21,9 @@
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container);
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::deque<Args...>& container);
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::set<Args...>& container);
@@ -564,6 +568,12 @@ static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>
return Helper::Print(stream, container);
}
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::deque<Args...>& container)
{
return Helper::Print(stream, container);
}
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::set<Args...>& container)
{
+31 -1
View File
@@ -5,6 +5,30 @@
#include <iomanip>
#include <iostream>
#undef AOC_MD5_TESTING
void PrintData(uint8_t* data)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)data[i * 8 + j];
}
std::cout << " ";
for (int j = 0; j < 8; j++)
{
if (data[i * 8 + j] >= 32 && data[i * 8 + j] < 126)
std::cout << data[i * 8 + j];
else
std::cout << "?";
}
std::cout << std::endl;
}
std::cout << std::dec << std::endl;
}
uint32_t Md5::BytesToUint32(uint8_t* data)
{
return ((uint32_t)data[3] << 24) | ((uint32_t)data[2] << 16) | ((uint32_t)data[1] << 8) | (uint32_t)data[0];
@@ -17,6 +41,9 @@ uint32_t Md5::LeftRotate(uint32_t x, uint32_t n)
void Md5::Transform(uint8_t* data, uint32_t& a0, uint32_t& b0, uint32_t& c0, uint32_t& d0)
{
#ifdef AOC_MD5_TESTING
PrintData(data);
#endif
// clang-format off
const static std::array<uint32_t, 64> K
{
@@ -116,7 +143,10 @@ std::string Md5::Hash(std::string str)
uint64_t offset = 0;
for (int i = 0; i < loops - 1; i++)
{
Transform((uint8_t*)(str.c_str() + offset), a0, b0, c0, d0);
uint8_t data[64];
std::memset(data, 0x0, 64);
std::memcpy(data, str.c_str() + offset, std::min<int>(str.size() - offset, 64));
Transform(data, a0, b0, c0, d0);
offset += 64;
}