aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day22/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-03 11:41:36 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-03 11:41:36 +0800
commit86c1a73437118e7c03280fbc190bb2b654fa5da6 (patch)
tree51600bc8641d58c045f3699c5a8cc29ee19c912e /src/2022/day22/aoc.cpp
parentb71a455726c04a5b12c771faef0d8a38e45de54b (diff)
downloadadvent-of-code-86c1a73437118e7c03280fbc190bb2b654fa5da6.tar.gz
advent-of-code-86c1a73437118e7c03280fbc190bb2b654fa5da6.zip
2022 day22 part1
Diffstat (limited to 'src/2022/day22/aoc.cpp')
-rw-r--r--src/2022/day22/aoc.cpp95
1 files changed, 44 insertions, 51 deletions
diff --git a/src/2022/day22/aoc.cpp b/src/2022/day22/aoc.cpp
index 305d5f2..6133492 100644
--- a/src/2022/day22/aoc.cpp
+++ b/src/2022/day22/aoc.cpp
@@ -11,73 +11,64 @@ struct person {
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 && r0.start + x >= r1.start) {
- return r0.start + x - r1.start;
- }
- if (s0 > s1 && r0.start + x < s1) {
- return r0.start + x - r1.start;
- }
- return x;
+static int absolute_x(int x, const monkey_map& r) {
+ return r.start + 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;
+typedef int (*yf)(int, const std::vector<monkey_map>&);
+
+int prev_y(int y, const std::vector<monkey_map>& ms) {
+ return y == 0 ? ms.size() - 1 : y - 1;
}
-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;
+int next_y(int y, const std::vector<monkey_map>& ms) {
+ return y == (int) ms.size() - 1 ? 0 : y + 1;
}
-int find_next(int y, const std::vector<monkey_map>& ms, facing f) {
- int first = find_first(y, ms);
- int last = find_last(y, ms);
- if (f == down) {
- return last == y ? first : y + 1;
- }
- if (f == up) {
- return first = y ? last : y - 1;
+int find_next(int x, int y, const std::vector<monkey_map>& ms, yf f) {
+ auto ax = absolute_x(x, ms[y]);
+ while (true) {
+ y = f(y, ms);
+ const monkey_map& r = ms[y];
+ if (ax >= r.start && ax < r.start + (int) r.line.length) {
+ return y;
+ }
}
- return y;
}
void go_up(int s, person* p, const std::vector<monkey_map>& ms) {
if (s > 0) {
+ int ax = absolute_x(p->x, ms[p->y]);
+ int y = find_next(p->x, p->y, ms, prev_y);
+ const monkey_map& r = ms[y];
+ if (*(r.line.line + ax - r.start) == '.') {
+ p->y = y;
+ p->x = ax - r.start;
+ go_up(s - 1, p, ms);
+ }
}
}
void go_down(int s, person* p, const std::vector<monkey_map>& ms) {
if (s > 0) {
+ int ax = absolute_x(p->x, ms[p->y]);
+ int y = find_next(p->x, p->y, ms, next_y);
+ const monkey_map& r = ms[y];
+ if (*(r.line.line + ax - r.start) == '.') {
+ p->y = y;
+ p->x = ax - r.start;
+ go_down(s - 1, p, ms);
+ }
}
}
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);
- }
+ int x = p->x - 1 >= 0 ? p->x - 1 : m.line.length - 1;
+ char c = *(m.line.line + x);
if (c == '.') {
- p->x -= 1;
+ p->x = x;
go_left(s - 1, p, ms);
}
}
@@ -86,12 +77,10 @@ void go_left(int s, person* p, const std::vector<monkey_map>& 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);
- }
+ int x = p->x + 1 < (int) m.line.length ? p->x + 1 : 0;
+ char c = *(m.line.line + x);
if (c == '.') {
- p->x += 1;
+ p->x = x;
go_right(s - 1, p, ms);
}
}
@@ -118,7 +107,7 @@ void travel(person* p, const std::vector<monkey_map>& ms, const route& r) {
}
}
-std::pair<int, int> day22(line_view file) {
+std::pair<int64_t, int64_t> day22(line_view file) {
per_line(file, [](line_view lv) {
const char* p = lv.line;
if (*p == ' ' || *p == '.' || *p == '#') {
@@ -146,7 +135,11 @@ std::pair<int, int> day22(line_view file) {
for (auto& r : routes) {
travel(&p, maps, r);
}
+
+ char fs[] = {'>', 'v', '<', '^'};
+ printf("person is at x[%d] y[%d] facing[%c]\n", p.x, p.y, fs[p.f]);
+ int64_t n1 = (p.y + 1) * 1000 + 4 * (p.x + 1) + (int) p.f;
- return {0, 0};
+ return {n1, 0};
}
} // namespace aoc2022