From 5653ea0ec0f04ce88cfff7074c5644e4e4e72ead Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Mon, 23 Jan 2023 15:16:21 -0600 Subject: [PATCH] 2022-10 --- 2022/10.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++ 2022/CMakeLists.txt | 3 ++ 2 files changed, 107 insertions(+) create mode 100644 2022/10.cpp diff --git a/2022/10.cpp b/2022/10.cpp new file mode 100644 index 0000000..cd105eb --- /dev/null +++ b/2022/10.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace { + +auto Execute(std::istream & in) -> std::vector +{ + std::int64_t x {1}; // current register value (defined to start at 1) + std::int64_t d; // change in register value + std::string instruction; + std::vector snapshots; + + while (in >> instruction) { + snapshots.push_back(x); + if ("addx" == instruction) { + snapshots.push_back(x); + if (!(in >> d)) { + throw std::runtime_error{"missing operand"}; + } + x += d; + } else if ("noop" != instruction) { + throw std::runtime_error{"bad instruction"}; + } + } + return snapshots; +} + +auto Part1(std::vector const& snapshots) -> std::int64_t +{ + std::int64_t result {0}; + for (auto i : {20,60,100,140,180,220}) { + result += i * snapshots[i-1]; + } + return result; +} + +auto Part2(std::vector const& snapshots, std::ostream & out) -> void +{ + auto constexpr rows {6}; + auto constexpr cols {40}; + + if (snapshots.size() < rows * cols) { + throw std::runtime_error{"snapshots too short"}; + } + + auto cursor = snapshots.begin(); + for (int r = 0; r < rows; r++) { + for (int c = 0; c < cols; c++) { + auto const d = *cursor++ - c; + out << (-1 <= d && d <= 1 ? "█" : "░"); + } + out << std::endl; + } +} + +} // namespace + +TEST_SUITE("2022-10 examples") { + TEST_CASE("example") { + std::istringstream in { + "addx 15\naddx -11\naddx 6\naddx -3\naddx 5\naddx -1\naddx -8\naddx 13\naddx 4\nnoop\n" + "addx -1\naddx 5\naddx -1\naddx 5\naddx -1\naddx 5\naddx -1\naddx 5\naddx -1\naddx -35\n" + "addx 1\naddx 24\naddx -19\naddx 1\naddx 16\naddx -11\nnoop\nnoop\naddx 21\naddx -15\n" + "noop\nnoop\naddx -3\naddx 9\naddx 1\naddx -3\naddx 8\naddx 1\naddx 5\nnoop\nnoop\nnoop\n" + "noop\nnoop\naddx -36\nnoop\naddx 1\naddx 7\nnoop\nnoop\nnoop\naddx 2\naddx 6\nnoop\n" + "noop\nnoop\nnoop\nnoop\naddx 1\nnoop\nnoop\naddx 7\naddx 1\nnoop\naddx -13\naddx 13\n" + "addx 7\nnoop\naddx 1\naddx -33\nnoop\nnoop\nnoop\naddx 2\nnoop\nnoop\nnoop\naddx 8\nnoop\n" + "addx -1\naddx 2\naddx 1\nnoop\naddx 17\naddx -9\naddx 1\naddx 1\naddx -3\naddx 11\nnoop\n" + "noop\naddx 1\nnoop\naddx 1\nnoop\nnoop\naddx -13\naddx -19\naddx 1\naddx 3\naddx 26\n" + "addx -30\naddx 12\naddx -1\naddx 3\naddx 1\nnoop\nnoop\nnoop\naddx -9\naddx 18\naddx 1\n" + "addx 2\nnoop\nnoop\naddx 9\nnoop\nnoop\nnoop\naddx -1\naddx 2\naddx -37\naddx 1\naddx 3\n" + "noop\naddx 15\naddx -21\naddx 22\naddx -6\naddx 1\nnoop\naddx 2\naddx 1\nnoop\naddx -10\n" + "noop\nnoop\naddx 20\naddx 1\naddx 2\naddx 2\naddx -6\naddx -11\nnoop\nnoop\nnoop\n" + }; + + auto snapshots = Execute(in); + CHECK(13140 == Part1(snapshots)); + std::ostringstream out; + Part2(snapshots, out); + CHECK(out.str() == + "██░░██░░██░░██░░██░░██░░██░░██░░██░░██░░\n" + "███░░░███░░░███░░░███░░░███░░░███░░░███░\n" + "████░░░░████░░░░████░░░░████░░░░████░░░░\n" + "█████░░░░░█████░░░░░█████░░░░░█████░░░░░\n" + "██████░░░░░░██████░░░░░░██████░░░░░░████\n" + "███████░░░░░░░███████░░░░░░░███████░░░░░\n" + ); + } +} + +auto main(int argc, char** argv) -> int +{ + auto snapshots = Execute(*aocpp::Startup(argc, argv)); + std::cout << "Part 1: " << Part1(snapshots) << std::endl; + Part2(snapshots, std::cout << "Part 2:\n"); +} diff --git a/2022/CMakeLists.txt b/2022/CMakeLists.txt index 0a3ddfb..a291217 100644 --- a/2022/CMakeLists.txt +++ b/2022/CMakeLists.txt @@ -13,6 +13,9 @@ target_link_libraries(2022_08 aocpp) add_executable(2022_09 09.cpp) target_link_libraries(2022_09 aocpp) +add_executable(2022_10 10.cpp) +target_link_libraries(2022_10 aocpp) + add_executable(2022_12 12.cpp) target_link_libraries(2022_12 aocpp)