Add solution for 2016 Day 2
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
ULL
|
||||
RRDDD
|
||||
LURDL
|
||||
UUUUD
|
||||
|
||||
+103
-8
@@ -2,30 +2,125 @@
|
||||
|
||||
namespace y2016::day02
|
||||
{
|
||||
REGISTER_DAY(2016, Day02, std::vector<int>, int);
|
||||
REGISTER_DAY(2016, Day02, std::vector<std::string>, std::string);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day02, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day02, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day02, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day02, Input, 2, 0);
|
||||
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<int> vec;
|
||||
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)
|
||||
{
|
||||
return 0;
|
||||
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)
|
||||
{
|
||||
return 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user