2022-11-03 19:49:07 -07:00
|
|
|
#include <iostream>
|
|
|
|
#include <utility>
|
|
|
|
#include <tuple>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <intcode.hpp>
|
|
|
|
using namespace intcode;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using Coord = std::pair<ValueType, ValueType>;
|
|
|
|
|
|
|
|
auto Compute(Machine machine, ValueType start)
|
|
|
|
-> std::map<Coord, ValueType> {
|
|
|
|
|
|
|
|
Coord here {0,0};
|
|
|
|
std::map<Coord, ValueType> paint {{here,start}};
|
2022-11-03 20:16:11 -07:00
|
|
|
ValueType dx {0};
|
|
|
|
ValueType dy {-1};
|
2022-11-03 19:49:07 -07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto effect = machine.Step();
|
|
|
|
|
|
|
|
if (std::holds_alternative<Halt>(effect)) {
|
|
|
|
return paint;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::get<Input>(effect).pos = paint[here];
|
2022-11-03 20:16:11 -07:00
|
|
|
paint[here] = std::get<Output>(machine.Step()).val;
|
2022-11-03 19:49:07 -07:00
|
|
|
auto dir = std::get<Output>(machine.Step()).val;
|
|
|
|
|
|
|
|
std::swap(dx, dy);
|
|
|
|
if (dir) {
|
|
|
|
dx = -dx;
|
|
|
|
} else {
|
|
|
|
dy = -dy;
|
|
|
|
}
|
|
|
|
here.first += dx;
|
|
|
|
here.second += dy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 20:16:11 -07:00
|
|
|
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) {
|
|
|
|
ValueType minX = 0, maxX = 0, minY = 0, maxY = 0;
|
|
|
|
for (auto&& [k, _] : image) {
|
|
|
|
auto [x,y] = k;
|
|
|
|
minX = std::min(minX, x);
|
|
|
|
maxX = std::max(maxX, x);
|
|
|
|
minY = std::min(minY, y);
|
|
|
|
maxY = std::max(maxY, y);
|
|
|
|
}
|
|
|
|
for (ValueType y = minY; y <= maxY; y++) {
|
|
|
|
for (ValueType x = minX; x <= maxX; x++) {
|
|
|
|
out << (image[{x,y}] ? "▓" : "░");
|
|
|
|
}
|
|
|
|
out << "\n";
|
|
|
|
}
|
2022-11-03 19:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
auto main() -> int {
|
|
|
|
auto machine = Machine{ParseStream(std::cin)};
|
2022-11-03 20:16:11 -07:00
|
|
|
std::cout << "Part 1: " << Compute(machine, 0).size() << "\nPart 2\n";
|
|
|
|
Draw(std::cout, Compute(std::move(machine), 1));
|
2022-11-03 19:49:07 -07:00
|
|
|
}
|