#include #include #include #include #include #include #include #include #include using namespace aocpp; using namespace intcode; namespace { auto RunWithIO( std::ostream & out, 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"}; }, [&](ValueType o) { out << char(o); }); } 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 auto Main(std::istream & in, std::ostream & out) -> void { 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); RunWithIO(out, Machine{ParseStream(in)}, script); }