#include #include #include #include #include #include "util.h" using Moves = std::vector; using Map = std::unordered_map>; constexpr char EQUAL[] = "="; int encode(std::string str) { int res{}; for (unsigned char c : str) { if ('A' <= c and c <= 'Z') { res *= 1 + 'Z' - 'A'; res += c - 'A'; } } return res; } std::pair parse(const char* path) { Map map{}; Moves moves{}; std::ifstream input{ path }; if (input.is_open()) { std::string line; std::getline(input,line); moves.resize(line.size()); std::transform(line.cbegin(), line.cend(), moves.begin(), [](unsigned char c) { return c == 'L'; }); std::string from, tol, tor; while (not std::getline(input,line).eof()) { if (line.empty()) continue; std::istringstream in{ line }; in >> from >> util::skip >> tol >> tor; map.insert({ encode(from), { encode(tol), encode(tor) } }); } } input.close(); return { std::move(moves), std::move(map) }; } int main(int argc, char* argv[]) { int answer{}; auto [moves, map] = parse(argv[1]); for (int pos = encode("AAA"); pos != encode("ZZZ"); pos = moves[answer++ % moves.size()] ? map[pos].first : map[pos].second); std::cout << answer << std::endl; return 0; }