2022-10
This commit is contained in:
parent
e67df73b3a
commit
5653ea0ec0
104
2022/10.cpp
Normal file
104
2022/10.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
namespace {
|
||||
|
||||
auto Execute(std::istream & in) -> std::vector<std::int64_t>
|
||||
{
|
||||
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<std::int64_t> 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<std::int64_t> 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<std::int64_t> 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");
|
||||
}
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user