diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-12-22 22:38:23 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-12-22 22:38:23 +0800 |
commit | 1e48202abdf34def6d88e85f3801abdbc8d642ad (patch) | |
tree | a9fa8ae93779ae42e42fad45f78ea1fa952ace7e /src | |
parent | 0fa2fe526f0f57692d890c3f2c162a90015a451f (diff) | |
download | advent-of-code-1e48202abdf34def6d88e85f3801abdbc8d642ad.tar.gz advent-of-code-1e48202abdf34def6d88e85f3801abdbc8d642ad.zip |
2022 day22 part1
Diffstat (limited to 'src')
-rw-r--r-- | src/2022/day22/aoc.cpp | 204 | ||||
-rw-r--r-- | src/2022/day22/aoc.h | 117 |
2 files changed, 318 insertions, 3 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 diff --git a/src/2022/day22/aoc.h b/src/2022/day22/aoc.h index de85db9..c2717c4 100644 --- a/src/2022/day22/aoc.h +++ b/src/2022/day22/aoc.h @@ -3,5 +3,120 @@ namespace aoc2022 { +struct monkey_map { + int start; + line_view line; + + void print() const noexcept { + for (int i = 0; i < start; i++) { + printf(" "); + } + const char*p = line.line; + for (size_t i = 0; i < line.length; i++) { + printf("%c", *(p + i)); + } + printf("\n"); + } + + monkey_map(const char *p) { + const char* p0 = p; + while (*p == ' ') + p++; + start = p - p0; + p0 = p; + while (*p == '.' || *p == '#') + p++; + line = {p0, p}; + } +}; + +enum facing { + right, + left, + up, + down, +}; + +struct route { + bool is_step = true; + union { + int steps = 0; + facing f; + } ins; + + void print(facing* f) const noexcept { + if (is_step) { + printf("%d", ins.steps); + } + else { + switch (*f) { + case right: printf("%c", ins.f == up ? 'L' : 'R'); break; + case left: printf("%c", ins.f == up ? 'R' : 'L'); break; + case up: printf("%c", ins.f == left ? 'L' : 'R'); break; + case down: printf("%c", ins.f == left ? 'R' : 'L'); break; + } + *f = ins.f; + } + } + + route(const char** pp, facing* ff) { + const char* p = *pp; + if (*p >= '0' && *p <= '9') { + while (*p >= '0' && *p <= '9') { + ins.steps = ins.steps * 10 + *p - '0'; + p++; + } + *pp = p; + } else { + is_step = false; + switch (*p) { + case 'L': { + switch (*ff) { + case right: + *ff = up; + ins.f = up; + break; + case left: + *ff = down; + ins.f = down; + break; + case up: + *ff = left; + ins.f = left; + break; + case down: + *ff = right; + ins.f = right; + break; + } + } break; + case 'R': { + switch (*ff) { + case right: + *ff = down; + ins.f = down; + break; + case left: + *ff = up; + ins.f = up; + break; + case up: + *ff = right; + ins.f = right; + break; + case down: + *ff = left; + ins.f = left; + break; + } + } break; + default: + break; + } + *pp = p + 1; + } + } +}; + std::pair<int, int> day22(line_view); -} +} // namespace aoc2022 |