aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day19/aoc.cpp
blob: 9336fdb206dc88b2b32f240c3c46d888799f9022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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) {
  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