From 5acd7b781288c671efe8f27c23ceed1bad9e6ffe Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 24 Dec 2022 09:56:22 -0800 Subject: [PATCH] day 24 python part 1 --- day24/input | 22 ++++++++++ day24/sample | 6 +++ day24/sol.py | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 day24/input create mode 100644 day24/sample create mode 100644 day24/sol.py diff --git a/day24/input b/day24/input new file mode 100644 index 0000000..e867e27 --- /dev/null +++ b/day24/input @@ -0,0 +1,22 @@ +#.###################################################################################################################################################### +#.v><><<>^^^<><<>.^v<>v^>v^v^^<..^v>^>^vv.>v^<<>^.v>><^>><>^<>>>>v<^<>^.v..v^vv>v<>v.<..^>v>><^v>^<.vv^^<<><<>v>><# +#.vv.v<<<<<>>vv<>^v^^<^<>><.<^>^<^>^v>><.><.^><>vv<^^><<<<>^<.v>^.<<<><^^<^>>^.<^v>><^^<^^>>v^vv<><<^<>^<^vv>v>>^^><<<<^><>v>^<^v<<^>v>>.^v>v.>v<^<^^<><<>><<^^^>v<^>>^v>>vv.<>^<><<^v>><^.>v^v><<^^^^^v<>^><>^vv.v^v>><^^<>^<>>^><^.^^<.v...>^^.^<>>v^..^v.v<^><>.>v^.vv<^^vv>vv^.^.vv>^>v.^^^>>^^.v>.v^^<^>vvv^<<^>v<>># +#<.<^>>^<^>^^.>>v^>vv.v^<>^^>v<^v.<<<^>>>.^>vvv.^^^<^><<^>><^.^>vv^^vv.><v>^>v.^^<<^^^>^><><v>vv>><^^v>>vvv^<...<<# +#>vv>^^^>>>^^v>^>>>.<<.<.<..><>>>^v><.^^><<>v^<^<>^>>.v^v.v.>v>^>>>v>>^>><^<.v.v<^<<<<<^^>^^^^>^^>v><>.<>v><>.>><># +#<><<.>^<>^^v^><^>v^.>>.>.>>.vv.v<.^<^<>>^><><^.v>>v^^v><>><<v><>^.>>^>v^^><^>.<^<.^<^>vvv>^<.v<^<>.><.>>v<^>^v<<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v<^<.><..><^>v><v>^v^>>^^>vv.v^^^>^^vv.<<<.# +#<^v<<^^vvv.>v>v>.v>^>^>^^v>v^vv^<.^.>>>>>v<>.<<.<^<^><^>^<^<<>><^>v^v^^^>>>>v>^>>^<.>>^^><^<^.^.v>^^vv^v.<>>>>.v.<<>^^vv># +#.>^v>^v^>^^.>.>v<>>>^>vv<>v<>^>v^<<^<.<<^>v^<><>><^^<^^v<>>vv<<.<>v>.>^^^>><^^<^vv.>^>><>v><^vv.>v^v^v..^<^>><<# +#<^v^<>v^<^<>>..>><>v<.^<.>v.>^v.>>^^<^v>>v^.v>^><>>vv.<<<>v>^.>v>..<<^>^<>v<^<^>^v><.<<^^^<^.<<<^.^v^^.>^.<^v><<>^<^v^^>^v><<^># +######################################################################################################################################################.# diff --git a/day24/sample b/day24/sample new file mode 100644 index 0000000..685dc4f --- /dev/null +++ b/day24/sample @@ -0,0 +1,6 @@ +#.###### +#>>.<^<# +#.<..<<# +#>v.><># +#<^v^^># +######.# diff --git a/day24/sol.py b/day24/sol.py new file mode 100644 index 0000000..a77b05e --- /dev/null +++ b/day24/sol.py @@ -0,0 +1,113 @@ +data = [] +for line in open("input"): + data.append(line.strip()) + +import sys, os; sys.path.append(os.path.join(os.path.dirname(__file__), "../lib")) +import astar + + +#print(*data, sep="\n") + +blizzards = {'>': {}, '<': {}, '^': {}, 'v': {}} +for y in range(len(data)): + for x in range(len(data[y])): + c = data[y][x] + if c in blizzards: + blizzards[c][x,y] = 1 + +def show(t): + H = len(data)-2 + for y in range(len(data)): + s = [] + W = len(data[y])-2 + for x in range(len(data[y])): + c = data[y][x] + if c == '#': + s.append(c) + else: + bliz = [] + u = 1 + ((x - t)-1)%W + if (u,y) in blizzards['>']: + bliz.append('>') + + u = 1 + ((x + t)-1)%W + if (u,y) in blizzards['<']: + bliz.append('<') + + v = 1 + ((y - t)-1)%H + if (x,v) in blizzards['v']: + bliz.append('v') + + v = 1 + ((y + t)-1)%H + if (x,v) in blizzards['^']: + bliz.append('^') + + if len(bliz) == 0: + s.append('.') + elif len(bliz) == 1: + s.append(bliz[0]) + elif len(bliz) < 10: + s.append(str(len(bliz))) + else: + s.append('*') + + print(''.join(s)) + +def blocked(x,y,t): + H = len(data)-2 + W = len(data[y])-2 + + if not 0 <= y < len(data): + return True + if not 0 <= x < len(data[y]): + return True + if data[y][x] == '#': + return True + + u = 1 + ((x - t)-1)%W + if (u,y) in blizzards['>']: + return True + + u = 1 + ((x + t)-1)%W + if (u,y) in blizzards['<']: + return True + + v = 1 + ((y - t)-1)%H + if (x,v) in blizzards['v']: + return True + + v = 1 + ((y + t)-1)%H + if (x,v) in blizzards['^']: + return True + + return False + +start = (data[0].index('.'), 0, 0) +goal = (data[-1].index('.'), len(data)-1) + +def is_goal(node): + x, y, t = node + return (x,y) == goal + +def heuristic(node): + x, y, t = node + return abs(goal[0] - x) + abs(goal[1] - y) + +def neighbors(node): + x, y, t = node + n = [] + def check(dx,dy): + if not blocked(x+dx,y+dy,t+1): + n.append((1, (x+dx, y+dy, t+1))) + check(+1,0) + check(0,+1) + check(-1,0) + check(0,-1) + check(0,0) + return n + +show(0) +show(1) +#show((len(data)-2)*(len(data[0])-2)) + +print(astar.search(start, is_goal, neighbors, heuristic))