#include "aoc.h" namespace aoc2017 { enum class direction { up, right, down, left, }; struct heading { pos p; direction d; }; static heading next(heading h) { switch (h.d) { case direction::up: return {{h.p.x, h.p.y - 1}, h.d}; case direction::right: return {{h.p.x + 1, h.p.y}, h.d}; case direction::down: return {{h.p.x, h.p.y + 1}, h.d}; case direction::left: return {{h.p.x - 1, h.p.y}, h.d}; } return h; } // + or A-Z static heading next(heading h, alpha_grid& g) { char c = g.get(h.p); if (c == '+' || (c >= 'A' && c <= 'Z')) { heading l = {{h.p.x - 1, h.p.y}, direction::left}; heading r = {{h.p.x + 1, h.p.y}, direction::right}; heading u = {{h.p.x, h.p.y - 1}, direction::up}; heading d = {{h.p.x, h.p.y + 1}, direction::down}; heading hs[4][3] = {{l, r, u}, {u, d, r}, {l, r, d}, {u, d, l}}; for (auto hx : hs[(int)h.d]) { if (g.get(hx.p) != ' ') { return hx; } } } return next(h); } static void move(heading h, alpha_grid& g, std::string& alpha, int* count) { while (true) { char c = g.get(h.p); if (c == ' ') break; if (c >= 'A' && c <= 'Z') { alpha.push_back(c); } h = next(h, g); *count += 1; } } std::pair day19(line_view file) { alpha_grid g{200, 200}; // input // alpha_grid g{15, 6}; // demo int r{0}; per_line(file, [&g, &r](line_view lv) { g.load(r++, lv); return true; }); heading start = {g.start, direction::down}; std::string alpha{""}; int count{0}; move(start, g, alpha, &count); return {alpha, count}; } } // namespace aoc2017