From 6d4a35f85edf3fc19240651d3295f41ecf62145a Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sat, 24 Dec 2022 15:05:16 +0100 Subject: [PATCH] day24 - dijkstra fail edition --- day24.py | 107 ++++++++++++++++++++++++++++++++++++++++++++ inputs/input24 | 27 +++++++++++ inputs/input24_test | 6 +++ 3 files changed, 140 insertions(+) create mode 100644 day24.py create mode 100644 inputs/input24 create mode 100644 inputs/input24_test diff --git a/day24.py b/day24.py new file mode 100644 index 0000000..617a3be --- /dev/null +++ b/day24.py @@ -0,0 +1,107 @@ +from heapq import heappush, heappop +from tools.aoc import AOCDay +from tools.coordinate import Coordinate +from tools.grid import Grid +from typing import Any + + +def expand_valley(valley: Grid, to_z: int) -> None: + while valley.maxZ <= to_z: + for blizzard_pos in valley.getActiveCells(z=valley.maxZ): + for blizzard in valley.get(blizzard_pos): + next_pos = blizzard_pos + blizzard + if next_pos.x < 0: + next_pos = Coordinate(valley.maxX, next_pos.y, next_pos.z) + elif next_pos.x > valley.maxX: + next_pos = Coordinate(0, next_pos.y, next_pos.z) + elif next_pos.y < 0: + next_pos = Coordinate(next_pos.x, valley.maxY, next_pos.z) + elif next_pos.y > valley.maxY: + next_pos = Coordinate(next_pos.x, 0, next_pos.z) + + if not valley.get(next_pos): + valley.set(next_pos, [blizzard]) + else: + valley.get(next_pos).append(blizzard) + + +def get_min_steps(valley: Grid, pos: Coordinate, target: Coordinate) -> int: + check_list = [ + Coordinate(1, 0, 1), + Coordinate(-1, 0, 1), + Coordinate(0, 0, 1), + Coordinate(0, 1, 1), + Coordinate(0, -1, 1) + ] + q = [] + heappush(q, (0, pos)) + v = set() + while q: + _, cur_pos = heappop(q) + if cur_pos in v: + continue + v.add(cur_pos) + + if cur_pos.z >= valley.maxZ: + expand_valley(valley, cur_pos.z + 1) + + for c in check_list: + next_pos = cur_pos + c + if next_pos.x == target.x and next_pos.y == target.y: + return next_pos.z + if not valley.isWithinBoundaries(next_pos) and (next_pos.x != pos.x or next_pos.y != pos.y): + continue + if valley.get(next_pos): + continue + dist = abs(target.x - next_pos.x) + abs(target.y - next_pos.y) + heappush(q, (dist + next_pos.z, next_pos)) + + return -1 + + +class Day(AOCDay): + inputs = [ + [ + (18, "input24_test"), + (269, "input24"), + ], + [ + (54, "input24_test"), + (825, "input24"), + ] + ] + + def get_valley(self) -> Grid: + valley = Grid.from_str( + "/".join(self.getInput()), + translate={ + '#': False, + '.': False, + '>': [Coordinate(1, 0, 1)], + '<': [Coordinate(-1, 0, 1)], + 'v': [Coordinate(0, 1, 1)], + '^': [Coordinate(0, -1, 1)], + }, + default=False, + mode3d=True + ) + valley.shift_zero() + + return valley + + def part1(self) -> Any: + valley = self.get_valley() + return get_min_steps(valley, Coordinate(0, -1, 0), Coordinate(valley.maxX, valley.maxY + 1)) + + def part2(self) -> Any: + valley = self.get_valley() + start = Coordinate(0, -1, 0) + target = Coordinate(valley.maxX, valley.maxY + 1) + steps = get_min_steps(valley, start, target) + steps = get_min_steps(valley, Coordinate(target.x, target.y, steps), start) + return get_min_steps(valley, Coordinate(start.x, start.y, steps), target) + + +if __name__ == '__main__': + day = Day(2022, 24) + day.run(verbose=True) diff --git a/inputs/input24 b/inputs/input24 new file mode 100644 index 0000000..d66380d --- /dev/null +++ b/inputs/input24 @@ -0,0 +1,27 @@ +#.######################################################################################################################## +#.v^<>^^.<^^v>>>.<^<vv>>^<<.v<^v^<^vv.v^^.>^><^<.<^v<>.v>.vvv.v>^^<^>.<<^<^<^>v.^^<# +#.^^><..<>v<^v><<>><>v.>v.^^<<.v.^<^<<^<>^v>v>><>v..v^<^.v><>^>>v^<v.v><^v# +#>>v>v<^>^^^^vv^^<^.^^>^<^vv>^v>><<^^<.>^>v<^v<.v.^<^^v.<.>.<^>v^>.v>>^vv^v.>>>v<<<<<<<.^>.>vvv>># +#<.vv>.><<.<^v.>v^v>>><<^vv>^v^>.>>><^>.v^^vv<>v>^v.>>v^>..v<>^v.^>><.vv><^...<>^^>v>vvv>^<^<>v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>.v<<>>v^.^v>v.v<.>^^v^<<<^^<^^v>><><<><>v>v^># +#.<.^>^>^>^v..v>^.vv><^^v^<.>>.^<.^^<<>>^><<>>^^.^<<^^><^v<.><>.<<<.vvv>>vv^v<^v<>vv<<^v^^v^>v<>>^>^<..^>>^>vv^.^>^<^^v>v>^<>.>vv>>^.^<<^>>^>^>vvv>^<<><<.^^^^>^>>>v<<>>v>v<>v<.# +#<^^v<.>>^>^<>^><.<>>.^>.<>.v>^>^v><><^<><<<^.><>>>v^.^>vv<<^>v<><.^^v<>vv>v^v<><^>>><# +#><>^v>v<>>>v<>^v^^>^v^>^^v>^^v><<^v>>vv<^><>^>^^>>v^^<^.^>v^vv^<.v>^v^.v^><><>>v.v^vv^^>^>^>^>v>^v..<# +#>v<^^<^>.<>v>v><^^v<^^>>^>^<^^>>>^v<^>v>^>v^v>^.<vv<^<.><^><<^>v><^^<^v>v^>><^vvv^>>v>>vv>v^>^><<# +#<^.>v.^><^...v^><<^^><^>^^vv>^v>v^^>^^..<^<<<>.><<>^vvvv>v^><.^^>>v>>>^>>^<^<^vvv>># +#><<<^^.^<.^.^^<>^.vv^^^^>v^>>v<<<.v><<^>>v><.v>v^^v^v<^>v^.<>^.^><>^v><>^<# +#<.<>.>v^><<>^^<^>^v>>>v>v><^v>>^<>>.vv>>^<^vv.>>>.>^v<^>v<^^>v.># +#<>^^<>>^v>v<<>^.>>^^^<^vv^<^<.<<>v>^^<^v><><>.<>.^><<^^^>^vv><>v.<>v<.>>^>vv^v..>^^<^^.><>^><# +########################################################################################################################.# diff --git a/inputs/input24_test b/inputs/input24_test new file mode 100644 index 0000000..6b9b892 --- /dev/null +++ b/inputs/input24_test @@ -0,0 +1,6 @@ +#.###### +#>>.<^<# +#.<..<<# +#>v.><># +#<^v^^># +######.# \ No newline at end of file