Add solution for 2016 Day 13
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
10
|
||||||
|
|||||||
+84
-12
@@ -2,30 +2,102 @@
|
|||||||
|
|
||||||
namespace y2016::day13
|
namespace y2016::day13
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2016, Day13, std::vector<int>, int);
|
REGISTER_DAY(2016, Day13, int, int);
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day13, ExampleInput, 1, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day13, ExampleInput, 1, 11);
|
||||||
REGISTER_TEST(2016, Day13, Input, 1, 0);
|
REGISTER_TEST(2016, Day13, Input, 1, 82);
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day13, ExampleInput, 2, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day13, ExampleInput, 2, 151);
|
||||||
REGISTER_TEST(2016, Day13, Input, 2, 0);
|
REGISTER_TEST(2016, Day13, Input, 2, 138);
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
int i;
|
||||||
std::string str;
|
input >> i;
|
||||||
while (getline(input, str))
|
return i;
|
||||||
{
|
|
||||||
}
|
}
|
||||||
return vec;
|
|
||||||
|
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)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
State state{1, 1};
|
||||||
|
return Helper::Dijkstras(input, state, Branch, Goal);
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user