aocpp/2019/25.cpp

82 lines
1.9 KiB
C++
Raw Permalink Normal View History

2022-11-12 19:44:11 -08:00
#include <algorithm>
#include <iostream>
#include <map>
#include <tuple>
#include <utility>
#include <vector>
#include <string_view>
#include <aocpp/Startup.hpp>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {
auto RunWithIO(
2023-01-31 09:15:15 -08:00
std::ostream & out,
2022-11-12 19:44:11 -08:00
Machine m,
std::string const& input
) -> void
{
auto it = input.begin();
Run(m,
[&]() -> ValueType {
if (it < input.end()) { return *it++; }
throw std::runtime_error{"insufficient input"};
},
2023-01-31 09:15:15 -08:00
[&](ValueType o) { out << char(o); });
2022-11-12 19:44:11 -08:00
}
auto Search(std::string & script)
{
unsigned prev = 0;
script += "west\n";
for (unsigned i = 1; i < 256; i++) {
unsigned curr = i ^ (i >> 1);
if (curr < prev) {
script += "take ";
} else {
script += "drop ";
}
switch (curr ^ prev) {
case (1<<0): script += "cake\n"; break;
case (1<<1): script += "sand\n"; break;
case (1<<2): script += "asterisk\n"; break;
case (1<<3): script += "ornament\n"; break;
case (1<<4): script += "festive hat\n"; break;
case (1<<5): script += "food ration\n"; break;
case (1<<6): script += "space heater\n"; break;
case (1<<7): script += "semiconductor\n"; break;
}
script += "west\n";
prev = curr;
}
}
} // namespace
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
2022-11-12 19:44:11 -08:00
std::string script =
"north\n" "take sand\n"
"north\n" "take space heater\n"
"east\n" "take semiconductor\n"
"west\n" "south\n" "south\n" "east\n" "take ornament\n"
"east\n" "east\n" "west\n" "west\n" "south\n" "take festive hat\n"
"east\n" "take asterisk\n"
"south\n" "east\n" "take cake\n"
"east\n" /* electromagnet */ "south\n" "north\n" "west\n"
"west\n" "west\n" "take food ration\n"
"east\n" "north\n" "west\n"
"west\n" /* photons */
"east\n" "north\n" "west\n"
"west\n" /* molten lava */ "west\n" "east\n" "north\n" /* infinite loop */ "north\n";
Search(script);
2023-01-31 09:15:15 -08:00
RunWithIO(out, Machine{ParseStream(in)}, script);
2022-11-12 19:44:11 -08:00
}