day 24 python part 1

main
magical 2022-12-24 09:56:22 -08:00
parent 9e168db913
commit 5acd7b7812
3 changed files with 141 additions and 0 deletions

22
day24/input 100644
View File

@ -0,0 +1,22 @@
#.######################################################################################################################################################
#.v><><<>^^^<><<>.^v<>v^>v^v^^<..^v>^>^vv.>v^<<>^.v<vvv<.>><v>><^>><>^<><v<>>>><v>v<^<>^.v..v^vv<v<<vv.v>>v<>v.<..^<vv<^v^v.v>>v>><^v>^<.vv^^<<><<>v>><#
#.vv<vv<>.v<<<<<>>vv<>^v^^<^<>><.<^>^<^>^<v<<<<v>v>><.><.^><>vv<^^><<<<>^<.<v^.vv^vv>v>^.<<<><v<.v.<><<v^^v>^^<^>>^.<v^v.v><<v^vv<<v>^v>><^^<^<v>^>><v<#
#<v^>v^vv<><v><<^<>^<^vv>v>>^^><<<vv><v.v.^><<^><>v>^<^v<<^>v<vv><v<<^.vv^>>>.^v>v.><v>v<^<^^<><<>><<^^^>v<^>>^v>>vv.<>^<><<^v>><^.>v^v><<^^^^<v<>^v<v.#
#<<^<vv^<^><>^><>^vv.v^v>><^^<>^<>>^><^.^^<.v...>^^.^<<v<vvv>>>v^..^v.v<^><>.>v^.vv<^^vv>vv^.^.vv<v>>^>v.^^^<v<>><vv^>>^^.v>.v^^<^>vvv^<<v^<v<^><^>v<>>#
#<.<^>>^<v><^>^^.>>v^<v^vvv>>vv.v^<>^^>v<<vv^<>^v.<<<^>>>.^>vvv.^^^<^><<^><v>><^.^>vv^^vv.><<v<^<v^^^.>v>^>v.^^<<^^^>^><><<vv<.<>v>vv>><^^v>>vvv^<...<<#
#><v.v>vv>^^^>>><v>^^v>^>>><vv^<^<^<<<>.<<.<.<..><><v.v^^.v..v^>>>^v><.^^><<>v^<^<>^>>.v^v.v.>v>^>>>v>>^>><^<.v.v<^<<<<<^^>^^^^>^^<v^>>v><>.<>v><>.>><>#
#<><<.>^<>^^v<v>^><^>v^.>><vv^vv^<v<<>.>.>>.vv.v<.^<^<>>^><><^.v>>v^^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.vv<^<v><v<vv^.><>v^<>v.^>>>v<<^<<^vv.>^<.^<>^<v^^^>#
#>>^vv^.<<.<.<>>vvv<<<..<v><^<>^><<>^.<<^^^<v^<<v.<vv<^<>>vv^>.v^v<^<>>vv<<^>>.v>.v>^^^vvv^v<^><v^><>><^<.>v^^^<vv^>^v<v^<vv><>..^><.>^^<^>>..^.v^<^<v>#
#<<><^^><v^v<^^<vv>><v..>>>^><>>>v>v>v>v><>>^vv><>>v>>vvvv<<^><>v^>vv>^.>>v.^<v<^.>>>^^v>>><vvv>><.<v<^<v<^v<v<<vvv..^^<<^<.<><<<<^<>v<<<^^>.^v>^^v<^^<#
#>v^^^<v>^^<v<<<vv><^v.v<v^^^vvv<<><><^^><^^>>>>>><^>^^.>vv>^><<<v<<<^vv.^^^<v>v<^^^^>.^^vv<^<.<v^<vv^^^>><..><^>v><<v>v>^v^>>^^>vv.v^^^>^^vv.<<v.^><<.#
#<^v<<^^vvv.>v>v>.v><v<^^>^>^>^^v>v^vv^<.^.>>>>>v<>.<<.<^<<vvv..<^>^><^>^<^<<>><^>v<v^v>^v^^^>>>>v>^>>^<v..><.>>^^><^<^.<v^>^.v>^^vv^v.<>>>>.v.<<>^^vv>#
#<v^v.>.>^v>^<v.<^<^>v^>^^.>.>v<>>>^>vv<>v<>^>v^<<^<.<<^>v^<><>><^^<^^v<>>vv<<.<>v>.>^^^>><^^<^vv.><v<.<<^^>^>><>v><^vv.>v^v^v..<vvv..<<v<v^<<v>^<^>><<#
#<^v<v^^v^>^<>v^<^<>>..>><>v<.^<.>v.>^v.>>^^<^v>>v^.v>^><>>vv.<<<>v>^.>v>..<<<v.<v^<>^>^<>v<^<v<><^>^v><.<<^^^<^.<<<^.^v^^.>^.<<vv>^v><<>^<^v^^>^v><<^>#
######################################################################################################################################################.#

6
day24/sample 100644
View File

@ -0,0 +1,6 @@
#.######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#

113
day24/sol.py 100644
View File

@ -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))