aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day19/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-02-13 14:24:12 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-02-13 14:24:12 +0800
commit18e9b3dd35e16cb2393ffac02b4c88be89274273 (patch)
tree5e784b0c59a93cc378136fe14eceabfa51a44e5b /src/2017/day19/aoc.cpp
parentead6a8d61d3d03d44e457187cd2725c37922a2d9 (diff)
downloadadvent-of-code-18e9b3dd35e16cb2393ffac02b4c88be89274273.tar.gz
advent-of-code-18e9b3dd35e16cb2393ffac02b4c88be89274273.zip
2017 day19 part1
Diffstat (limited to 'src/2017/day19/aoc.cpp')
-rw-r--r--src/2017/day19/aoc.cpp71
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