diff --git a/2019/25.cpp b/2019/25.cpp new file mode 100644 index 0000000..f75d163 --- /dev/null +++ b/2019/25.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace aocpp; +using namespace intcode; + +namespace { + +auto RunWithIO( + 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) { std::cout << 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(int argc, char** argv) -> int { + 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(Machine{ParseStream(aocpp::Startup(argc, argv))}, script); +} diff --git a/2019/CMakeLists.txt b/2019/CMakeLists.txt index 23489e9..95705e6 100644 --- a/2019/CMakeLists.txt +++ b/2019/CMakeLists.txt @@ -60,3 +60,6 @@ target_link_libraries(23 aocpp intcode) add_executable(24 24.cpp) target_link_libraries(24 aocpp) + +add_executable(25 25.cpp) +target_link_libraries(25 aocpp intcode) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..155daee --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,17 @@ +{ + "version": 2, + "configurePresets": [ + { + "name": "preset", + "displayName": "Configure preset using toolchain file", + "description": "Sets Ninja generator, build and install directory", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "RelWithDebInfo", + "CMAKE_TOOLCHAIN_FILE": "", + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" + } + } + ] +}