aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day22/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-22 22:38:23 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-22 22:38:23 +0800
commit1e48202abdf34def6d88e85f3801abdbc8d642ad (patch)
treea9fa8ae93779ae42e42fad45f78ea1fa952ace7e /src/2022/day22/aoc.cpp
parent0fa2fe526f0f57692d890c3f2c162a90015a451f (diff)
downloadadvent-of-code-1e48202abdf34def6d88e85f3801abdbc8d642ad.tar.gz
advent-of-code-1e48202abdf34def6d88e85f3801abdbc8d642ad.zip
2022 day22 part1
Diffstat (limited to 'src/2022/day22/aoc.cpp')
-rw-r--r--src/2022/day22/aoc.cpp204
1 files changed, 202 insertions, 2 deletions
diff --git a/src/2022/day22/aoc.cpp b/src/2022/day22/aoc.cpp
index c7ed99f..3a0f4e2 100644
--- a/src/2022/day22/aoc.cpp
+++ b/src/2022/day22/aoc.cpp
@@ -2,8 +2,208 @@
namespace aoc2022 {
-std::pair<int, int> day22(line_view) {
- return {0, 0};
+std::vector<monkey_map> maps;
+std::vector<route> routes;
+
+struct person {
+ int x;
+ int y;
+ facing f;
+};
+
+int get_x(int x, const monkey_map& r0, const monkey_map& r1) {
+ int s0 = r0.start + r0.line.length;
+ int s1 = r1.start + r1.line.length;
+
+ if (s0 < s1) {
+ }
+ if (s0 > s1) {
+ }
+ return x;
+}
+
+int find_last(int y, const std::vector<monkey_map>& ms) {
+ const monkey_map& r = ms[y];
+ int n = y;
+ while (n < (int)ms.size() && ms[n].start + ms[n].line.length == r.start + r.line.length) {
+ n++;
+ if (n + 1 == (int)ms.size()) {
+ break;
+ }
+ }
+ return n;
+}
+
+int find_first(int y, const std::vector<monkey_map>& ms) {
+ const monkey_map& r = ms[y];
+ int n = y;
+ while (n > 0 && ms[n].start + ms[n].line.length == r.start + r.line.length) {
+ n--;
+ if (n == 0)
+ break;
+ }
+ return n;
+}
+
+void go_up(int s, person* p, const std::vector<monkey_map>& ms) {
+ if (s > 0) {
+ // const monkey_map& r = ms[p->y - 1];
+ // const monkey_map& r0 = ms[p->y - 2];
+ // int x = p->x - 1;
+ // if (r0.start < r.start) {
+ // if (r.start + x >= r0.start + (int)r0.line.length) { // wrap
+ // auto n = p->y - 1;
+ // while (ms[n].start == r.start && n < (int)ms.size())
+ // n++;
+ // const monkey_map& r1 = ms[n - 1];
+ // char c = *(r1.line.line + x);
+ // if (c == ' ') {
+ // printf("brrrr < wrap %d %d\n", p->x - 1, p->y - 1);
+ // return;
+ // }
+ // if (c == '.') {
+ // p->y = n - 1;
+ // go_up(s - 1, p, ms);
+ // }
+ // } else {
+ // int x0 = r.start + x - r0.start;
+ // char c = *(r0.line.line + x0);
+ // if (c == ' ') {
+ // printf("brrrr < no wrap %d %d\n", p->x - 1, p->y - 1);
+ // return;
+ // }
+ // if (c == '.') {
+ // p->y -= 1;
+ // p->x = x0;
+ // go_up(s - 1, p, ms);
+ // }
+ // }
+ // }
+ // if (r0.start == r.start) {
+ // char c = *(r0.line.line + x);
+ // if (c == ' ') {
+ // printf("brrrr = %d %d\n", p->x - 1, p->y - 1);
+ // return;
+ // }
+ // if (c == '.') {
+ // p->y -= 1;
+ // go_up(s - 1, p, ms);
+ // }
+ // }
+ // if (r0.start > r.start) {
+ // if (r.start + x < r0.start) {
+ // auto n = p->y - 1;
+ // while (ms[n].start == r.start && n < (int)ms.size())
+ // n++;
+ // const monkey_map& r1 = ms[n - 1];
+ // char c = *(r1.line.line + x);
+ // if (c == ' ') {
+ // printf("brrrr > wrap %d %d\n", p->x - 1, p->y - 1);
+ // return;
+ // }
+ // if (c == '.') {
+ // p->y = n - 1;
+ // go_up(s - 1, p, ms);
+ // }
+ // } else {
+ // int x0 = r.start + x - r0.start;
+ // char c = *(r0.line.line + x0);
+ // if (c == ' ') {
+ // printf("brrrr > no wrap %d %d\n", p->x - 1, p->y - 1);
+ // }
+ // if (c == '.') {
+ // p->y -= 1;
+ // p->x = x0;
+ // go_up(s - 1, p, ms);
+ // }
+ // }
+ // }
+ }
+}
+
+void go_down(int s, person* p, const std::vector<monkey_map>& ms) {
+ if (s > 0) {
+ }
+}
+
+void go_left(int s, person* p, const std::vector<monkey_map>& ms) {
+ if (s > 0) {
+ const monkey_map& m = ms[p->y];
+ char c = p->x - 1 >= 0 ? *(m.line.line + p->x - 1) : *(m.line.line + m.line.length - 1);
+ if (c == ' ') {
+ printf("brrrr left %d %d\n", p->x, p->y);
+ }
+ if (c == '.') {
+ p->x -= 1;
+ go_left(s - 1, p, ms);
+ }
+ }
}
+
+void go_right(int s, person* p, const std::vector<monkey_map>& ms) {
+ if (s > 0) {
+ const monkey_map& m = ms[p->y];
+ char c = p->x + 1 < (int)m.line.length ? *(m.line.line + p->x + 1) : *(m.line.line);
+ if (c == ' ') {
+ printf("brrrr right %d %d\n", p->x, p->y);
+ }
+ if (c == '.') {
+ p->x += 1;
+ go_right(s - 1, p, ms);
+ }
+ }
+}
+
+void travel(person* p, const std::vector<monkey_map>& ms, const route& r) {
+ if (r.is_step == false) {
+ p->f = r.ins.f;
+ } else {
+ switch (p->f) {
+ case up:
+ go_up(r.ins.steps, p, ms);
+ break;
+ case down:
+ go_down(r.ins.steps, p, ms);
+ break;
+ case left:
+ go_left(r.ins.steps, p, ms);
+ break;
+ case right:
+ go_right(r.ins.steps, p, ms);
+ break;
+ }
+ }
}
+std::pair<int, int> day22(line_view file) {
+ per_line(file, [](line_view lv) {
+ const char* p = lv.line;
+ if (*p == ' ' || *p == '.' || *p == '#') {
+ maps.emplace_back(p);
+ }
+ if (*p >= '0' && *p <= '9') {
+ facing f = right;
+ while (*p != '\n') {
+ routes.emplace_back(&p, &f);
+ }
+ }
+ return true;
+ });
+
+ // for(auto&m : maps) {
+ // m.print();
+ // }
+
+ // facing f = right;
+ // for(auto& r: routes) {
+ // r.print(&f);
+ // }
+ // printf("\n");
+ person p{0, 0, right};
+ for (auto& r : routes) {
+ travel(&p, maps, r);
+ }
+
+ return {0, 0};
+}
+} // namespace aoc2022