Add solution for 2016 Day 1

This commit is contained in:
Thraix
2026-08-06 22:17:35 +02:00
parent 80eacc6ed2
commit c8f0000c4d
3 changed files with 65 additions and 9 deletions
+1 -1
View File
@@ -1 +1 @@
R5, L5, R5, R3
R8, R4, R4, R8
+50 -8
View File
@@ -2,30 +2,72 @@
namespace y2016::day01
{
REGISTER_DAY(2016, Day01, std::vector<int>, int);
struct Move
{
char r;
int count;
};
REGISTER_TEST_EXAMPLE(2016, Day01, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day01, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day01, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day01, Input, 2, 0);
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<int> vec;
std::vector<Move> vec;
std::string str;
while (getline(input, 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)
{
return 0;
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;
}
}
+14
View File
@@ -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;