diff --git a/res/2016/day08/test_input.txt b/res/2016/day08/test_input.txt index e69de29..a9df2bc 100644 --- a/res/2016/day08/test_input.txt +++ b/res/2016/day08/test_input.txt @@ -0,0 +1,4 @@ +rect 3x2 +rotate column x=1 by 1 +rotate row y=0 by 4 +rotate column x=1 by 1 diff --git a/src/2016/Day08.cpp b/src/2016/Day08.cpp index b224def..e081423 100644 --- a/src/2016/Day08.cpp +++ b/src/2016/Day08.cpp @@ -2,30 +2,100 @@ namespace y2016::day08 { - REGISTER_DAY(2016, Day08, std::vector, 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, int); + + REGISTER_TEST_EXAMPLE(2016, Day08, ExampleInput, 1, 6); + REGISTER_TEST(2016, Day08, Input, 1, 115); READ_INPUT(input) { - std::vector vec; + std::vector 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 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; } } diff --git a/src/2021/Day13.cpp b/src/2021/Day13.cpp index 885e6e8..29e7ef2 100644 --- a/src/2021/Day13.cpp +++ b/src/2021/Day13.cpp @@ -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(); } diff --git a/src/common/lib/Array2D.h b/src/common/lib/Array2D.h index fde1f5d..366cf63 100644 --- a/src/common/lib/Array2D.h +++ b/src/common/lib/Array2D.h @@ -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; }