Add solution for 2016 Day 17
- Fix Md5 algorithm
This commit is contained in:
1 parent
7b2567a71e
commit
ed11090cc2
3 files changed
+97
-12
No files matched your search
+65
-11
@@ -2,30 +2,84 @@
|
||||
|
||||
namespace y2016::day17
|
||||
{
|
||||
REGISTER_DAY(2016, Day17, std::vector<int>, int);
|
||||
REGISTER_DAY(2016, Day17, std::string, std::string);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day17, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day17, Input, 2, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 1, "DRURDRUDDLLDLUURRDULRLDUUDDDRR");
|
||||
REGISTER_TEST(2016, Day17, Input, 1, "DUDDRLRRRD");
|
||||
REGISTER_TEST_EXAMPLE(2016, Day17, ExampleInput, 2, "830");
|
||||
REGISTER_TEST(2016, Day17, Input, 2, "578");
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
}
|
||||
return vec;
|
||||
getline(input, str);
|
||||
return str;
|
||||
}
|
||||
|
||||
struct State
|
||||
{
|
||||
std::string str{""};
|
||||
int x{0};
|
||||
int y{0};
|
||||
};
|
||||
|
||||
bool IsOpen(char c)
|
||||
{
|
||||
return c >= 'b' && c <= 'f';
|
||||
}
|
||||
|
||||
void Branch(const State& state, std::queue<State>& queue)
|
||||
{
|
||||
std::string hash = Md5::Hash(state.str);
|
||||
if (state.y > 0 && IsOpen(hash[0]))
|
||||
queue.emplace(State{state.str + "U", state.x, state.y - 1});
|
||||
if (state.y < 3 && IsOpen(hash[1]))
|
||||
queue.emplace(State{state.str + "D", state.x, state.y + 1});
|
||||
if (state.x > 0 && IsOpen(hash[2]))
|
||||
queue.emplace(State{state.str + "L", state.x - 1, state.y});
|
||||
if (state.x < 3 && IsOpen(hash[3]))
|
||||
queue.emplace(State{state.str + "R", state.x + 1, state.y});
|
||||
}
|
||||
|
||||
bool IsGoal(const State& state)
|
||||
{
|
||||
return state.x == 3 && state.y == 3;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
std::queue<State> queue;
|
||||
queue.emplace(State{input, 0, 0});
|
||||
while (!queue.empty())
|
||||
{
|
||||
State state = queue.front();
|
||||
queue.pop();
|
||||
if (IsGoal(state))
|
||||
return state.str.substr(input.size());
|
||||
Branch(state, queue);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
return 0;
|
||||
std::queue<State> queue;
|
||||
queue.emplace(State{input, 0, 0});
|
||||
int longest = 0;
|
||||
while (!queue.empty())
|
||||
{
|
||||
State state = queue.front();
|
||||
queue.pop();
|
||||
if (IsGoal(state))
|
||||
{
|
||||
longest = state.str.size() - input.size();
|
||||
continue;
|
||||
}
|
||||
Branch(state, queue);
|
||||
}
|
||||
|
||||
return std::to_string(longest);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user