This commit is contained in:
Eric Mertens 2022-11-03 20:16:11 -07:00
parent 005bd76e6b
commit 7018a2998a
2 changed files with 21 additions and 11 deletions

View File

@ -15,8 +15,8 @@ auto Compute(Machine machine, ValueType start)
Coord here {0,0}; Coord here {0,0};
std::map<Coord, ValueType> paint {{here,start}}; std::map<Coord, ValueType> paint {{here,start}};
ValueType dx = 1; ValueType dx {0};
ValueType dy = 0; ValueType dy {-1};
for (;;) { for (;;) {
auto effect = machine.Step(); auto effect = machine.Step();
@ -26,9 +26,8 @@ auto Compute(Machine machine, ValueType start)
} }
std::get<Input>(effect).pos = paint[here]; std::get<Input>(effect).pos = paint[here];
auto color = std::get<Output>(machine.Step()).val; paint[here] = std::get<Output>(machine.Step()).val;
auto dir = std::get<Output>(machine.Step()).val; auto dir = std::get<Output>(machine.Step()).val;
paint[here] = color;
std::swap(dx, dy); std::swap(dx, dy);
if (dir) { if (dir) {
@ -41,16 +40,27 @@ auto Compute(Machine machine, ValueType start)
} }
} }
auto Draw(std::map<Coord, ValueType> image) { auto Draw(std::ostream & out, std::map<Coord, ValueType> image) {
ValueType minX = 1000, maxX = -1000, minY = 1000, maxY = -1000; 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";
}
} }
} // namespace } // namespace
auto main() -> int { auto main() -> int {
auto machine = Machine{ParseStream(std::cin)}; auto machine = Machine{ParseStream(std::cin)};
std::cout << "Part 1: " << Compute(machine, 0).size() << "\nPart 2\n";
std::cout << "Part 1: " << Compute(machine, 0).size() << std::endl; Draw(std::cout, Compute(std::move(machine), 1));
Draw(Compute(std::move(machine), 1));
} }

View File

@ -21,7 +21,7 @@ auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
case 1: case 1:
return v; return v;
case 2: case 2:
return m.At(m.Rel(v)); return m.Rel(v);
default: default:
throw BadInstruction{"invalid addressing mode"}; throw BadInstruction{"invalid addressing mode"};
} }