diff options
Diffstat (limited to 'src/2017/day19/aoc.cpp')
-rw-r--r-- | src/2017/day19/aoc.cpp | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/src/2017/day19/aoc.cpp b/src/2017/day19/aoc.cpp index 8056eb8..9336fdb 100644 --- a/src/2017/day19/aoc.cpp +++ b/src/2017/day19/aoc.cpp @@ -2,5 +2,74 @@ namespace aoc2017 { -std::pair<int64_t, int64_t> day19(line_view) { return {0, 0}; } +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) { + while (true) { + char c = g.get(h.p); + if (c == ' ') + break; + if (c >= 'A' && c <= 'Z') { + alpha.push_back(c); + } + h = next(h, g); + } +} + +std::pair<std::string, int64_t> 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{""}; + move(start, g, alpha); + return {alpha, 0}; +} } // namespace aoc2017 |