Add solution for 2016 Day 8
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
rect 3x2
|
||||
rotate column x=1 by 1
|
||||
rotate row y=0 by 4
|
||||
rotate column x=1 by 1
|
||||
|
||||
+77
-7
@@ -2,30 +2,100 @@
|
||||
|
||||
namespace y2016::day08
|
||||
{
|
||||
REGISTER_DAY(2016, Day08, std::vector<int>, int);
|
||||
enum class Operation
|
||||
{
|
||||
Rect,
|
||||
Col,
|
||||
Row
|
||||
};
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day08, ExampleInput, 1, 0);
|
||||
REGISTER_TEST(2016, Day08, Input, 1, 0);
|
||||
REGISTER_TEST_EXAMPLE(2016, Day08, ExampleInput, 2, 0);
|
||||
REGISTER_TEST(2016, Day08, Input, 2, 0);
|
||||
struct Instruction
|
||||
{
|
||||
Operation operation;
|
||||
int i1;
|
||||
int i2;
|
||||
};
|
||||
|
||||
REGISTER_DAY(2016, Day08, std::vector<Instruction>, int);
|
||||
|
||||
REGISTER_TEST_EXAMPLE(2016, Day08, ExampleInput, 1, 6);
|
||||
REGISTER_TEST(2016, Day08, Input, 1, 115);
|
||||
|
||||
READ_INPUT(input)
|
||||
{
|
||||
std::vector<int> vec;
|
||||
std::vector<Instruction> vec;
|
||||
std::string str;
|
||||
while (getline(input, str))
|
||||
{
|
||||
Instruction instruction;
|
||||
std::stringstream ss{str};
|
||||
if (str[1] == 'e') // rect
|
||||
{
|
||||
instruction.operation = Operation::Rect;
|
||||
ss >> "rect " >> instruction.i1 >> "x" >> instruction.i2;
|
||||
}
|
||||
else if (str[7] == 'r') // rotate row
|
||||
{
|
||||
instruction.operation = Operation::Row;
|
||||
ss >> "rotate row y=" >> instruction.i1 >> "by " >> instruction.i2;
|
||||
}
|
||||
else // rotate column
|
||||
{
|
||||
instruction.operation = Operation::Col;
|
||||
ss >> "rotate column x=" >> instruction.i1 >> "by " >> instruction.i2;
|
||||
}
|
||||
vec.emplace_back(instruction);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
OUTPUT1(input)
|
||||
{
|
||||
return 0;
|
||||
Array2D<char> data{isExample ? 7 : 50, isExample ? 3 : 6, ' '};
|
||||
for (const auto& instruction : input)
|
||||
{
|
||||
switch (instruction.operation)
|
||||
{
|
||||
case Operation::Rect:
|
||||
for (int y = 0; y < instruction.i2; y++)
|
||||
{
|
||||
for (int x = 0; x < instruction.i1; x++)
|
||||
{
|
||||
data.Set(x, y, '#');
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Operation::Row:
|
||||
for (int i = 0; i < instruction.i2; i++)
|
||||
{
|
||||
char tmp = data.Get(data.width - 1, instruction.i1);
|
||||
for (int x = data.width - 1; x > 0; x--)
|
||||
{
|
||||
data.Set(x, instruction.i1, data.Get(x - 1, instruction.i1));
|
||||
}
|
||||
data.Set(0, instruction.i1, tmp);
|
||||
}
|
||||
break;
|
||||
case Operation::Col:
|
||||
for (int i = 0; i < instruction.i2; i++)
|
||||
{
|
||||
char tmp = data.Get(instruction.i1, data.height - 1);
|
||||
for (int y = data.height - 1; y > 0; y--)
|
||||
{
|
||||
data.Set(instruction.i1, y, data.Get(instruction.i1, y - 1));
|
||||
}
|
||||
data.Set(instruction.i1, 0, tmp);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << data << std::endl;
|
||||
return data.Count('#');
|
||||
}
|
||||
|
||||
OUTPUT2(input)
|
||||
{
|
||||
// Solution is printed in Part 1
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -93,7 +93,7 @@ namespace y2021::day13
|
||||
{
|
||||
board[index] = '#';
|
||||
}
|
||||
std::cout << board;
|
||||
std::cout << board << std::endl;
|
||||
// Not the "correct" output to part 2, but it verifies something at least
|
||||
return indices.size();
|
||||
}
|
||||
|
||||
@@ -508,7 +508,8 @@ struct Array2D
|
||||
{
|
||||
os << array2D.Get(x, y);
|
||||
}
|
||||
os << std::endl;
|
||||
if (y != array2D.height - 1)
|
||||
os << std::endl;
|
||||
}
|
||||
return std::cout;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user