diff options
-rw-r--r-- | src/2022/day22/README.md | 65 | ||||
-rw-r--r-- | src/2022/day22/aoc.cpp | 25 | ||||
-rw-r--r-- | test/test_2022.cpp | 4 |
3 files changed, 80 insertions, 14 deletions
diff --git a/src/2022/day22/README.md b/src/2022/day22/README.md index 385626a..04c8e0c 100644 --- a/src/2022/day22/README.md +++ b/src/2022/day22/README.md @@ -76,4 +76,69 @@ In the above example, the final row is 6, the final column is 8, and the final f Follow the path given in the monkeys' notes. What is the final password? +--- Part Two --- +As you reach the force field, you think you hear some Elves in the distance. Perhaps they've already arrived? +You approach the strange input device, but it isn't quite what the monkeys drew in their notes. Instead, you are met with a large cube; each of its six faces is a square of 50x50 tiles. + +To be fair, the monkeys' map does have six 50x50 regions on it. If you were to carefully fold the map, you should be able to shape it into a cube! + +In the example above, the six (smaller, 4x4) faces of the cube are: + + 1111 + 1111 + 1111 + 1111 +222233334444 +222233334444 +222233334444 +222233334444 + 55556666 + 55556666 + 55556666 + 55556666 +You still start in the same position and with the same facing as before, but the wrapping rules are different. Now, if you would walk off the board, you instead proceed around the cube. From the perspective of the map, this can look a little strange. In the above example, if you are at A and move to the right, you would arrive at B facing down; if you are at C and move down, you would arrive at D facing up: + + ...# + .#.. + #... + .... +...#.......# +........#..A +..#....#.... +.D........#. + ...#..B. + .....#.. + .#...... + ..C...#. +Walls still block your path, even if they are on a different face of the cube. If you are at E facing up, your movement is blocked by the wall marked by the arrow: + + ...# + .#.. + -->#... + .... +...#..E....# +........#... +..#....#.... +..........#. + ...#.... + .....#.. + .#...... + ......#. +Using the same method of drawing the last facing you had with an arrow on each tile you visit, the full path taken by the above example now looks like this: + + >>v# + .#v. + #.v. + ..v. +...#..^...v# +.>>>>>^.#.>> +.^#....#.... +.^........#. + ...#..v. + .....#v. + .#v<<<<. + ..v...#. +The final password is still calculated from your final position and facing from the perspective of the map. In this example, the final row is 5, the final column is 7, and the final facing is 3, so the final password is 1000 * 5 + 4 * 7 + 3 = 5031. + +Fold the map into a cube, then follow the path given in the monkeys' notes. What is the final password? diff --git a/src/2022/day22/aoc.cpp b/src/2022/day22/aoc.cpp index ba3ff0d..bdb255f 100644 --- a/src/2022/day22/aoc.cpp +++ b/src/2022/day22/aoc.cpp @@ -45,10 +45,10 @@ void mark(int x, int y, char c) { 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]); + mark(ax, 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) == '.') { - mark(ax, p->y, '^'); p->y = y; p->x = ax - r.start; go_up(s - 1, p, ms); @@ -59,10 +59,10 @@ void go_up(int s, person* p, const std::vector<monkey_map>& 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]); + mark(ax, p->y, 'v'); int y = find_next(p->x, p->y, ms, next_y); const monkey_map& r = ms[y]; if (*(r.line.line + ax - r.start) == '.') { - mark(ax, p->y, 'v'); p->y = y; p->x = ax - r.start; go_down(s - 1, p, ms); @@ -73,10 +73,10 @@ void go_down(int s, person* p, const std::vector<monkey_map>& ms) { void go_left(int s, person* p, const std::vector<monkey_map>& ms) { if (s > 0) { const monkey_map& m = ms[p->y]; + mark(m.start + 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 == '.') { - mark(m.start + p->x, p->y, '<'); p->x = x; go_left(s - 1, p, ms); } @@ -86,10 +86,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]; + mark(m.start + 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 == '.') { - mark(m.start + p->x, p->y, '>'); p->x = x; go_right(s - 1, p, ms); } @@ -97,12 +97,12 @@ void go_right(int s, person* p, const std::vector<monkey_map>& ms) { } void travel(person* p, const std::vector<monkey_map>& ms, const route& r) { - char fs[] = {'>', 'v', '<', '^'}; + // char fs[] = {'>', 'v', '<', '^'}; if (r.is_step == false) { - printf("%c", fs[r.ins.f]); + // printf("%c", fs[r.ins.f]); p->f = r.ins.f; } else { - printf("%d\n", r.ins.steps); + // printf("%d\n", r.ins.steps); switch (p->f) { case up: go_up(r.ins.steps, p, ms); @@ -149,13 +149,14 @@ std::pair<int64_t, int64_t> day22(line_view file) { travel(&p, maps, r); } - for(auto&tm : tmaps) { - tm.print(); - } + // for(auto&tm : tmaps) { + // tm.print(); + // } // char fs[] = {'>', 'v', '<', '^'}; - // printf("person is at x[%d] y[%d] facing[%c]\n", p.x + 1, p.y + 1, fs[p.f]); - int64_t n1 = (p.y + 1) * 1000 + 4 * (p.x + 1) + (int) p.f; + const monkey_map& r = maps[p.y]; + // printf("person is at x[%d] y[%d] facing[%c]\n", r.start + p.x + 1, p.y + 1, fs[p.f]); + int64_t n1 = (p.y + 1) * 1000 + 4 * (r.start + p.x + 1) + (int) p.f; return {n1, 0}; } diff --git a/test/test_2022.cpp b/test/test_2022.cpp index dc5a9a7..af8c998 100644 --- a/test/test_2022.cpp +++ b/test/test_2022.cpp @@ -177,9 +177,9 @@ TEST_CASE("Monkey Math", "[2022]") { } TEST_CASE("Monkey Map", "[2022]") { - line_view lv = load_file("../src/2022/day22/input0"); + line_view lv = load_file("../src/2022/day22/input"); auto p = aoc2022::day22(lv); - REQUIRE(0 == p.first); + REQUIRE(75388 == p.first); REQUIRE(0 == p.second); } |