day 16
hello A* my old friend
This commit is contained in:
parent
89b317c60d
commit
96f25dee8b
112
day16/astar.py
Normal file
112
day16/astar.py
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
from heapq import heappush, heappop
|
||||||
|
|
||||||
|
def search(start, is_goal, neighbors, heuristic=None, worst=None):
|
||||||
|
if heuristic == None:
|
||||||
|
def heuristic(x):
|
||||||
|
return 0
|
||||||
|
if not callable(is_goal):
|
||||||
|
goal = is_goal
|
||||||
|
def is_goal(this):
|
||||||
|
return this == goal
|
||||||
|
if not isinstance(start, list):
|
||||||
|
start = [start]
|
||||||
|
i = 0 # tiebreaker
|
||||||
|
q = []
|
||||||
|
for s in start:
|
||||||
|
i += 1
|
||||||
|
heappush(q, (heuristic(s), i, s, None))
|
||||||
|
done = {}
|
||||||
|
if worst:
|
||||||
|
best_worst = min(worst(s) for s in start)
|
||||||
|
while q:
|
||||||
|
z, _, this, prev = heappop(q)
|
||||||
|
if this in done:
|
||||||
|
z -= heuristic(this)
|
||||||
|
if z == done[this][0]:
|
||||||
|
done[this] = (z, done[this][1]+[prev])
|
||||||
|
continue
|
||||||
|
cost_so_far = z - heuristic(this)
|
||||||
|
#print(this,z, cost_so_far)
|
||||||
|
|
||||||
|
done[this] = (cost_so_far, [prev])
|
||||||
|
|
||||||
|
if is_goal(this):
|
||||||
|
print("astar: visited", len(done), "nodes")
|
||||||
|
print("astar: pending", len(q), "nodes")
|
||||||
|
# find all nodes on a best path
|
||||||
|
Q = [this]
|
||||||
|
path = set()
|
||||||
|
while Q:
|
||||||
|
n = Q.pop()
|
||||||
|
if n not in path and n != None:
|
||||||
|
path.add(n)
|
||||||
|
Q.extend(done[n][1])
|
||||||
|
return cost_so_far, this, path
|
||||||
|
|
||||||
|
for c, n in neighbors(this):
|
||||||
|
c = cost_so_far + c
|
||||||
|
if n not in done or c < done[n][0]:
|
||||||
|
h = heuristic(n)
|
||||||
|
if worst:
|
||||||
|
if c+h > best_worst:
|
||||||
|
# if the best possible cost for this node
|
||||||
|
# is worse than the lowest worst-case cost we've seen
|
||||||
|
# then don't even bother exploring it
|
||||||
|
continue
|
||||||
|
w = worst(n)
|
||||||
|
if c+w < best_worst:
|
||||||
|
best_worst = c+w
|
||||||
|
i += 1
|
||||||
|
heappush(q, (c+h, i, n, this))
|
||||||
|
elif n in done:
|
||||||
|
if c == done[n][0]:
|
||||||
|
done[n] = (c, done[n][1]+[n])
|
||||||
|
|
||||||
|
return float('inf'), None, []
|
||||||
|
|
||||||
|
def test():
|
||||||
|
data = [
|
||||||
|
"aabqponm",
|
||||||
|
"abcryxxl",
|
||||||
|
"accszzxk",
|
||||||
|
"acctuvwj",
|
||||||
|
"abdefghi",
|
||||||
|
]
|
||||||
|
#data = [
|
||||||
|
# "aabbaaba",
|
||||||
|
# "abcbaaba",
|
||||||
|
# "accabcbb",
|
||||||
|
# "accbbbba",
|
||||||
|
# "abbbabab",
|
||||||
|
#]
|
||||||
|
start = (0,0)
|
||||||
|
goal = (5,2)
|
||||||
|
|
||||||
|
def get(x,y):
|
||||||
|
return ord(data[y][x])
|
||||||
|
|
||||||
|
def neighbors(src):
|
||||||
|
x,y = src
|
||||||
|
here = get(x,y)
|
||||||
|
n = []
|
||||||
|
def push(x,y):
|
||||||
|
if 0 <= y < len(data):
|
||||||
|
if 0 <= x < len(data[y]):
|
||||||
|
if get(x,y) <= here+1:
|
||||||
|
n.append((1, (x,y)))
|
||||||
|
push(x-1, y)
|
||||||
|
push(x+1,y)
|
||||||
|
push(x, y-1)
|
||||||
|
push(x, y+1)
|
||||||
|
return n
|
||||||
|
|
||||||
|
def heuristic(n):
|
||||||
|
return abs(goal[0] - n[0]) + abs(goal[1] - n[1])
|
||||||
|
|
||||||
|
c, _, path = search(start, goal, neighbors, heuristic)
|
||||||
|
print(*data, sep="\n")
|
||||||
|
print(*path, sep="\n")
|
||||||
|
assert c == 31, c
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test()
|
141
day16/input
Normal file
141
day16/input
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
#############################################################################################################################################
|
||||||
|
#.................#.....#.....#.............#...#.......#.....#.......#.#.........#...........#...#...............#........................E#
|
||||||
|
###########.#######.#.#.#.###.#.###########.#.#.#.###.###.#.#.#.#.#.#.#.#.#####.#.###.#######.#.#.###.#########.#.###.###########.#.#####.#.#
|
||||||
|
#.........#.#.......#...#.#...#...#...#...#.#.#.....#.#...#.#.#...#.#...#.#.#...#...#.#...#.....#...#.......#.........#.....#...#.#...#.#...#
|
||||||
|
#.#######.#.#.#########.#.#.#####.#.#.###.#.#.#######.#.###.###.#.#.###.#.#.#.#####.#.#.#.#########.#.#####.#.#.#########.#.###.#.###.#.###.#
|
||||||
|
#.......#.#.#.......#.#.#.#.#...#...#.........................#.#...#.#.#...#.#...#...#.#.....#...#.#.....#.#...#.........#.....#.#...#.#...#
|
||||||
|
#.#####.#.#.#######.#.#.#.#.#.#.#######.#.#######.#.#.#######.#.#.###.#.###.#.#.#.#####.#.###.#.###.#####.#.#.#.#.#####.#####.###.#.#.#.#.#.#
|
||||||
|
#.#.....#.........#...#...#...#.#.......................#.............................#.#.#...#.....#.....#.......................#.....#.#.#
|
||||||
|
#.#.#######.###.#####.#.#######.#.#############.###.#####.#.#.#.#.#.#.###.###.#.#####.#.#.#.###.#########.#.###.#########.###.#.#.#####.#.###
|
||||||
|
#.#.#.......#.........#...#.....#...#...........#.#.#...#.#.....................#...#.....#.#.#.....#...#.#.....#.......#...#...#.#.#...#...#
|
||||||
|
#.#.#########.#.#######.#.#.#######.#.#######.#.#.#.#.#.#.#####.#.#######.#.###.#.#######.#.#.#####.#.#.###.###.#.#####.###.#.###.#.#.#####.#
|
||||||
|
#.#.......#...#...........#.....#...#.#...#...#...#...#.#.......#.........#.....#.#.....#...#.....#...#.........#.#...#.#...#.....#.....#...#
|
||||||
|
#.#######.#.###.###.#.#.#.#####.#####.#.#.#.#.#########.###.#####.#############.#.#.###.#.###.#####.###########.#.#.#.#.#####.#.###.#####.#.#
|
||||||
|
#...#.....#.#.............#...#...........#.#.........#...#.#.......#...#.....#...#.#.#...#.#.......#.......#...#...#.#.......#.#...#.....#.#
|
||||||
|
#####.#####.#.#.#.###.#.#.###.#############.#########.###.#.#.###.#.#.#.#.###.#####.#.###.#.#.###.###.#####.#.#.#####.#######.#.#####.#####.#
|
||||||
|
#...#.....#.#.#.#.#...#.#...#...#...#...#...#.....#...#...#.#.#.#...#.#.#...#...#...#.#.........#.....#...#.....#.....#.....#.#.......#...#.#
|
||||||
|
#.#.#####.#.###.#.#.###.###.#.###.#.#.#.#.###.###.#.###.###.#.#.#.###.#####.###.#.###.#.#########.#####.#.#.###.#.#########.#.#####.###.#.#.#
|
||||||
|
#.#.......#.....#...#...#.....#...#...#.....#...#.#...#.#...#.#...#...#.....#.#...#...#.#.#.....#...#...#.#...#.#.#.......#.#...#...#...#.#.#
|
||||||
|
#.#######.#######.###.###.#####.#####.#####.#.#.#####.#.#.###.#.###.###.#####.#####.#.#.#.#.###.#.#.#.###.#.#.#.#.#.#####.#.###.#####.###.###
|
||||||
|
#...#...#.#.........#.....#.#...#.....#...#...#.....#...#.....#.......#.#.......#...#.#...#...#.#.#...#...#.#.#.#...#.#.......#.#...#.#.#...#
|
||||||
|
#.#.#.#.#.#.###.###.#.#####.#.#####.###.#.#########.###########.#####.#.#.#####.#.###.#.###.#.#.#.#####.###.#.#######.#.#######.#.#.#.#.###.#
|
||||||
|
#.#.#.#...#...#.#.#.#...#...#.#...#...#.#.#.......#...........#...#...#.#.....#...#...#.#...#...#.......#...#.......#...#.....#...#.#.....#.#
|
||||||
|
#.#.#.#######.#.#.#.###.#.###.#.#.#####.#.#.#####.#########.#####.#.#.#.###.#.#.#######.#.###.#.#######.#.#.###.#####.###.###.#####.#####.#.#
|
||||||
|
#.#.#.....#...#.#...#.#.#...#...#...#...#...#...#.....#...#.....#...#.#...#.#.#.#.......#.....#.#...#...#.#...#.#.....#.#...#.........#...#.#
|
||||||
|
#.#.#####.###.#.#.###.#.###.#######.#.#####.###.#####.#.#.#####.###.#####.#.#.###.#.#####.###.#.#.#.###.#.###.#.#.#####.###.#####.#####.###.#
|
||||||
|
#.#...#.#...#.#.#.....#...#.....#...#.....#.....#.......#.....#.....#.....#.#.....#.......#...#...#...#.#.....#...#...#...#.....#.#.....#.#.#
|
||||||
|
#####.#.###.###.#.#.#.###.###.#.#.#.#####.###.#.###.#########.#.###.#.#####.#.#####.#######.#.#######.#######.#####.#.#.#.#####.#.#.#####.#.#
|
||||||
|
#.....#...#.#...#...#...#.....#.#.#...#.#...#.#...#.......#...#.....#...#...#.#...#.#.....#.#...............#.#.#...#...#.#...............#.#
|
||||||
|
#.#######.#.#.#########.#######.#.###.#.###.#.###.###.###.#.#######.###.#.###.#.#.#.#.#.#.#.#.#.###.#####.#.#.#.#.#.#####.#.#.###.###.#.###.#
|
||||||
|
#.....#...#.........#.....#...#.#.#.......#...#.#.....#.#.#.......#.#...#.#.#...#.#.#.#.#...#.#...#.....#.....#.#.#.#.......#.#.....#.#.#...#
|
||||||
|
#.#.#.#.###.#.#.#####.#####.#.#.#.#######.#####.#######.#.###.#.###.#.###.#.#.###.#.###.#.###.###.#.#.#########.#.#.#######.#.#######.#.#.#.#
|
||||||
|
#...#.#...#.#.#.#.....#.....#...#...#.........#...#.....#...#.#.....#.#.#.#...#...#...#.#.#...#.#...#.......#...#.#.....#.#.#.#.......#.#.#.#
|
||||||
|
###.#.###.#.#.#.#.#####.###########.###.#####.#.#.#.###.###.#.#.#####.#.#.#.###.#.###.#.###.###.###.###.#.#.###.#.#####.#.#.#.#.#######.#.###
|
||||||
|
#...#...#...#.#.#.#.#...#...#...#...#...#.......#.#...#...#.#.#.....#.#.........#.#.#.#.#.........#.....#.......#.#.#...#.#.#.#.#.......#...#
|
||||||
|
#.#.###.#.###.#.#.#.#.###.#.#.#.#.###.#.#.#######.#####.###.#.#######.###########.#.#.#.#.#######.#.###.#.#.#####.#.#.###.#.#.#.#.###.#####.#
|
||||||
|
#.#.#...#...........#.#...#.#.#...#...#.#.#.#...#.......#...#...#.....#...#...#.....#.#...#...#...#...#.#.#.#.......#.#.....#.#...#...#.....#
|
||||||
|
#.#.#.#####.#.#####.#.#.###.#.#####.#####.#.#.#.#########.#####.#.#####.#.#.#.#######.###.#.#.#.#####.###.#.#.#######.#######.#####.###.#####
|
||||||
|
#...#.#...#.#...#.#.#.#...#.#.#...#.......#.#.#...........#...#.#.......#...#.#.........#.#.#.#.#.....#...#.#.#.#.....#.......#.....#...#...#
|
||||||
|
###.#.#.#.#.###.#.#.#.###.#.#.###.#.#######.#.#############.###.#.###########.#.###.###.#.#.#.#.#.###.#.#####.#.#.###.#.#######.#####.###.#.#
|
||||||
|
#...#...#.#.....#...#.....#.#...#.#...#.....#.........#.....#...#.#...#.....#.#...#...#.#.#.#.#...#...#...#.....#.#.#.#...#.....#...#.....#.#
|
||||||
|
#.#.###.#.###.#####.#.#########.#.###.#.#############.#.###.#.#.#.#.#.#####.#.###.#.#.#.###.#.#.#########.#.#####.#.#.###.#.#.###.#.#######.#
|
||||||
|
#.#.......#...#.....#.#.....#...#...............#...#.#.#...#.#.#.#.#.....#.#.#...#.#.#.....#.#.#.......#...#.....#...#...#.#.....#.........#
|
||||||
|
###.###.###.###.###.#.#.###.#.###.#######.###.###.#.#.#.#####.#.#.#.#####.#.#.#.###.###.#.###.###.#####.#####.#####.###.###.#####.#####.#.###
|
||||||
|
#...#.....#.#.....#.#.#.#...#.#...#...#...#...#...#...#.#.....#...#...#...#.#...#.#.....#.........#...#.......#.......#.....#...#.....#.#...#
|
||||||
|
#.#.###.###.#.#####.#.#.#.###.#.###.###.###.###.#######.#.#####.#####.#.###.#####.#######.#.#######.#.###.#######.#####.#####.#.#####.#####.#
|
||||||
|
#.#...#.....#.#.....#.#.#.....#.#...#...#.#...#...#.....#...#.#.......#.#.........#.#.....#.......#.#.#...#.....#.#...#.#.....#.....#.....#.#
|
||||||
|
#.#.#.#.###.#.#.#####.#.###.#.#.#.###.###.###.#.#.###.#.###.#.#########.###.#####.#.#.###########.#.###.###.###.###.#.#.#####.#####.#.###.#.#
|
||||||
|
#...#.....#.#.#.......#.#...#.#...#...#...#.#...#.#...#.#...#.........#...#.....#.#.#.#...#.....#.#.....#...#...#...#.#.....#.#.....#.#...#.#
|
||||||
|
#.#.###.#.###.#########.#.#.#.###.#.###.#.#.#.###.#.#.###.###.#######.###.#####.#.#.#.#.#.###.#.#.#.#####.###.###.###.#####.#.###.#.#.#.###.#
|
||||||
|
#...#...............#...#.#...#...#...#.#...#...#...#.#...#.....#...#...#.......#...#...#...#.....#...#...#.#.#.....#...#.#.#...#.#...#.#...#
|
||||||
|
###.###.#########.###.###.#.###.#####.#.###.###.#####.#.###.###.#.#.#.###.#####.###########.#.#########.###.#.#####.###.#.#.#.#.#.#.###.#.###
|
||||||
|
#.#.....#.....#.....#.#.#.#.#.#.........#.#...#.#.....#.#.#...#...#...#...#...#.#.........#.#.#.......#.#...........#...#...#.....#.....#...#
|
||||||
|
#.#####.#.###.#.###.#.#.#.#.#.#####.#####.###.#.#.#####.#.###.#######.#.###.#.###.#######.#.#.#####.#.#.#############.###.#####.#.#.###.###.#
|
||||||
|
#.....#.#.#...#.#.#...#.#...#.....#.#.#.......#.#.#...#.#...#.#.....#...#...#...#.....#...#.#.......#...#.........#.............#.#...#.#...#
|
||||||
|
#.###.#.#.#.###.#.#####.#####.#####.#.#.#######.#.#.#.#.#.#.#.###.#.#####.#.###.#.###.#.###.#.#########.#.###.#.###.#####.#####.#.#.#.###.#.#
|
||||||
|
#...#.#.#.#.....#.....#...............#.#.......#...#.#.#.#.#.....#...#.........#.#...#.#...#.#.....#...#...#.#.........#.#.....#.....#...#.#
|
||||||
|
###.#.#.#.#######.#####.###############.###.#.#######.#.###.#.###.#####.###.#####.#.###.###.#.#.###.###.###.#.#.#.###.#.###.###.#####.#.###.#
|
||||||
|
#.#.#...#.#...........#.....#...#.#.....#...#.......#.#...#.......#...#...#.#.#...#...#.....#.#.#...#...#...#.........#.#...#.#...#...#...#.#
|
||||||
|
#.#.###.#.#####.#####.#####.#.#.#.#.#####.#########.#.###.#########.#.###.#.#.#.#####.#########.#.###.#.#.###.#########.#.###.#.#.###.###.###
|
||||||
|
#.#...#.#.#...#.....#...#.#...#...#...#.....#.....#...#.#...........#.......#.......#...#...#...#...#.#.....#.#.....#.....#.....#...#.#.#...#
|
||||||
|
#.###.#.#.#.#.#######.#.#.#####.#####.#.###.#.#.#.#####.###################.#######.###.#.#.#.#.###.#.#######.#.###.#####.#.###.###.#.#.###.#
|
||||||
|
#.......#...#.....#...#...#.....#...#.#...#...#.#.#.............#.........#...#...#.#.#...#.....#...#...#...#.....#.....#...#...#...#...#...#
|
||||||
|
#.#####.#########.#.#######.#####.#.#.###.#####.#.#########.#.###.#.#####.###.###.#.#.###########.###.#.#.#.###########.#.###.#.#.###.###.#.#
|
||||||
|
#.....#.#.......#.#.........#.....#.#...#.#.....#...........#.#...#.#...#...#...#.#...#.......#...#.....#.#.....#.......#...#...............#
|
||||||
|
#.#.###.#.#.###.#.###########.#####.###.#.#.###################.###.#.#.###.###.#.###.###.###.#.#.#.#####.#.#.#.#.#########.#.#####.#.#.#####
|
||||||
|
#.#.....#.......#.....#...........#.....#.#.....#.....#.......#.#...#.#...#...#.#.....#...#...#.#.#.#...#.#.#.#...#.........#.#.....#.......#
|
||||||
|
#.#####.###.#.#######.#.###########.#.#########.#.###.#.#.#.#.#.#####.#.#.###.#.#.#####.#.###.#.###.#.#.#.#.#.#####.#.#######.#.#####.###.#.#
|
||||||
|
#...#.......#.#.....#.........#.....#.#.....#.....#.#...#...#...#...#.#.#...#.#.#.......#.#.........#.#...#...#.....#...#.....#.#.......#.#.#
|
||||||
|
###.#.#######.#.#########.#.###.###.#.###.#.#.#.#.#.#.#########.#.#.#.#.###.#.#.#####.#.###.#.###.###.#########.#######.#.#####.#.###.#.#.#.#
|
||||||
|
#.#...#...#...#.......#...#...#...#.#.....#.....#.#...#.......#.#.#.#.#.....#...#.....#...#.#.#...#.#.#.................#.#.....#.#...#.#.#.#
|
||||||
|
#.#####.#.#.#########.#.#####.#.#.#.###.#######.#.#####.#####.###.#.#.###.#######.###.#.#.#.###.###.#.#######.#####.###.#.#.#####.#.#.#.#.#.#
|
||||||
|
#.......#.#.#.......#.#.#.....#.#.#.#...#.....#.#.....#.....#...#.#...#.#.#.....#.#.#...#...#.......#.......#.#...#.....#.#.....#.#...#...#.#
|
||||||
|
#.#####.#.#.#.#####.#.#.#.###.#.#.#.#.###.###.#.#####.#.#.#.###.#.###.#.#.###.#.#.#.#.###.###.#.#.#.#######.###.#.#.#.###.#####.#.###.###.#.#
|
||||||
|
#.#.......#...#.#...#...#.#...#.#.....#...#.#.#...#...#.#.#.#.....#...#.#...#...#.#.#.#...#...#...#.......#...#.#.#.#.......#...#...#...#.#.#
|
||||||
|
#.#####.#######.#.###.#.#.#.#.#.#.###.#.###.#.#####.#.#.###.#.#########.###.#.###.#.#.#####.#####.#.#.#.#.###.#.#.#########.#.#####.#.#.#.#.#
|
||||||
|
#...........#.#...#...#...#...#...#.....#...#...#...#.#...#.....#.......#.#.#.....#.#.......#.....#.#.#.....#.........#...#.#.....#...#...#.#
|
||||||
|
#####.#.#.#.#.#.#######.#####.#.#.#.#######.###.#.#.#####.#######.#######.#.###.###.###########.###.#.#####.#########.#.#.#.#####.###.#.#.#.#
|
||||||
|
#...#...#.#...#.....#...#...#.#.#.#.#.........#.....#...#.......#.....#...#...#.#...........#...#.#.#...#...........#...#.#.#.......#...#...#
|
||||||
|
#.#.###.#.#########.#.###.#.#.#.#.#.###.#####.#.###.#.#.#######.#####.#.#####.#.#.###.#####.#.###.#.#####.#########.#####.#.#.#######.###.#.#
|
||||||
|
#.#...#.#.........#.#...#.#.#.#...#...#.....#.......#.#.#.....#.....#.#.#.....#.#.#...#...#...#...........#.......#.#.....#...............#.#
|
||||||
|
#.###.#.#########.#.###.#.###.#.#####.#####.#.#####.#.#.#.#.#####.#.#.#.#.#####.###.###.#.#.###############.#####.#.#.#########.###.###.#.###
|
||||||
|
#...#.#.....#.....#.....#.#.........#.....#.#.#...#.#.#.#.#.......#...#...#.#...#...#...#.#.#.............#.....#...#.........#...#.....#...#
|
||||||
|
#.#.#.#.###.#.###.#######.#.###.###.#####.#.#.#.#.###.#.#.#######.#####.###.#.###.#####.###.#.###########.#.#####.###########.#.#######.#.#.#
|
||||||
|
#.#.#.#.....#.#.#.#.......#.#.....#...#...#...#.#.....#...#.....#.....#.#...#.........#...#.#.#.....#.#...#.#...#.......#...#...#.....#...#.#
|
||||||
|
###.#.#.#####.#.#.###.#.#.#.#########.#.#######.###########.#.#######.#.###.#####.#.#.###.#.#.#.#.#.#.#.#####.#.#####.###.#.###.#.###.###.#.#
|
||||||
|
#...#.......#.#.#.#...#.#.#.......#...#.........#.........#.#.......#.....#.......#.....#...#.#.#.#.#.#.#.....#...#...#...#...#.#.#.#.....#.#
|
||||||
|
#.#########.#.#.#.#.#.#.#.#######.#.#############.#####.###.#####.###.###.#########.###.#####.###.#.#.#.###.#####.###.#.#####.###.#.#####.#.#
|
||||||
|
#...#.....#.#.#...#.#...#.....#...#.#...........#.#.....#...#...#...#...#...#.......#.........#...#...#.#...#.......#.#.#...#.....#.....#.#.#
|
||||||
|
#.#.#####.#.#.#.###.###.#####.#.###.#.#######.#.#.#.#####.###.#.###.###.###.#.#################.###.###.#.#.#######.#.#.#.###.#####.###.#.#.#
|
||||||
|
#.#.....#...#.#...#...#.#...#.........#.......#.#.#.....#...#.....#...#...#.................#.....#.#...#.#.#.....#.#.#.#.....#...#...#.#.#.#
|
||||||
|
#####.#.###.#.###.###.#.#.###.###.#####.#######.###.###.###.#.#######.#.#.#.#.#############.#####.#.#.###.###.###.#.###.###.###.#.###.#.#.#.#
|
||||||
|
#...#.#...#...#.......#.#.#.....#...#...#.....#...#.#.#...#.#.#.....#.#.#.#.#...#.....#...#.#.....#.#.#.#.....#...#...#...#.#.#.#.#...#.#.#.#
|
||||||
|
#.#.#####.###.#########.#.#.###.###.#.###.###.###.#.#.#.#.#.#.#.###.#.#.###.###.#.#.#.#.###.#.#####.#.#.#.#####.#####.###.#.#.#.#.#.#.#.#.#.#
|
||||||
|
#.#.......#...#.........#.#.....#.#...#.....#.#...#...#...#.#.#.#.#...#...#...#.#...#...#...#.#...#.#.#...#...#.#...#...#.#...#.#...#.#.#.#.#
|
||||||
|
#.#######.#####.#########.#.#.###.#####.###.###.#######.###.#.#.#.###.###.###.#.###.###.#.###.#.#.###.#####.#.#.#.#.###.#.#####.###.###.#.#.#
|
||||||
|
#.#...#...#.....#.........#.#.....#...#...#.#...#.....#...#.#.#.......#.....#.#.........#.#.....#...#.......#.#.#.#.#...#.#.....#.#...#.....#
|
||||||
|
#.#.#.#####.#####.#.#.#####.#####.#.###.###.#.###.###.#.#.#.#.#.#.#####.#.#.#.#####.#####.#########.#########.#.###.#.###.#.###.#.###.###.#.#
|
||||||
|
#.#.#.#.....#...#...#.#.....#.....#.#...#...#.#...#.#.#.#.....#.#.#.....#.#.#.#.#...#...#.#.......#.........#.#.......#...#...#.....#...#...#
|
||||||
|
#.#.#.#.#####.#.#.#.#.#.#####.#####.#.###.###.#.###.#.###.#####.#.#.###.#.###.#.#.###.#.#.#.#####.#######.#.#.#.#.###.#.#.###.#.#.#####.#.###
|
||||||
|
#...#.#.......#.#.#.#.#.#.........#.....#...#.#...#.#...#...#.....#...#.#.....#.....#.#.#.....#...#.......#.#.#.#...#.#.#.......#.#.....#.#.#
|
||||||
|
#.###.#.#######.#.#.###.#########.#.#######.#.###.#.###.#####.###.###.#.#######.#####.#.#.#####.###.#########.#####.#.#.###.###.#.#.#####.#.#
|
||||||
|
#.#.#.............#...#...#.....#.#...#...#.#.....#...#.......#...#...#.#...#...#.....#.#.#.....#.............#.....#...#.....#...#...#.#.#.#
|
||||||
|
#.#.#########.#.#.###.###.#.###.#.###.#.#.#.#########.#########.###.###.#.#.#.###.#####.###.#####.###########.#.###############.#.###.#.#.#.#
|
||||||
|
#.....#...#...#.#...#...#...#.#.#...#.#.#.......#.......#.#.....#.#.#...#.#...#...#...#...#.......#...........#...#.............#...........#
|
||||||
|
#####.#.#.#.###.#####.#######.#.###.###.#######.###.###.#.#.#####.#.#.###.###.#.#####.#.#.#.#####.#.#######.#.###.#.#########.#.#.#.#.#.#.#.#
|
||||||
|
#.....#...#.#.......#.........#...#.....#.....#...#.#...#.#.....#...#.........#.....#...#.#.....#.#.#.......#...#.#.#.......#.......#.#...#.#
|
||||||
|
#.#######.#.#.#####.###.###.#####.#######.#.#####.#.###.#.#####.#.#########.#####.#.###.#.###.#.###.#.#########.#.#.#.#####.#.#######.#.#.#.#
|
||||||
|
#.#.....#.#...#...#...#.#...#.....#...#...#.#.....#...#...#.....#.#.......#.....#.#.#...#...#.#.....#.....#...#...#...#...#.................#
|
||||||
|
#.#.###.#.#####.#.#.#.#.#.###.###.#.###.#.#.#.#####.#.#####.#####.###.#.###.###.###.#.###.#.#.#######.###.#.#.#.#######.#.#.###.#.#######.#.#
|
||||||
|
#...#.....#...#.#...#...#.......#.....#.#.#.#.#.....#.#...#.#...#...#...#...#.#...#.#.....#.#.#.....#.#.#.#.#...#.......#.#.........#.......#
|
||||||
|
#.#######.#.#.#.###.###.#####.#.#####.#.#.###.#######.#.#.#.#.#.###.###.#.###.###.#.#.###.#.###.###.#.#.#.#.#####.#.#########.#.#.#.#.#######
|
||||||
|
#.#.....#.#.#.#.#.#...#.#...#.#.....#...#.....#.......#.....#.#.#...........#...#...#.#...#.....#.#.#...#.#.....#.#.........#.#.#.#.#.#.....#
|
||||||
|
#.#.###.#.#.#.#.#.###.#.#.#.#######.#.#######.#.###.###.#####.#.#######.###.###.#####.#.#########.#.###.#.#######.#######.###.#.#.#.#.#.###.#
|
||||||
|
#.#.#.......#...#...#.....#.......#.#...#...#...#...#...#.....#.....#...#...#.......#.#.........#...#...#.....#...#.....#.#...#.#.#.#.#.#...#
|
||||||
|
###.#####.#########.#############.#.#####.#.#####.###.###.#.#######.#####.###.#####.#.#####.###.#.#####.#####.#.#######.#.#.###.#.#.#.###.#.#
|
||||||
|
#...#.............#.....#.........#...#...#...#.....#...#.#.#.............#.....#...#.#.#...#...#.....#.....#.......#...#...#...#.#.......#.#
|
||||||
|
#.###.###########.###.#.#.###########.#.###.#.#########.#.#.#.###.###.#####.###.#####.#.#.###.#######.#############.#.#.#####.#.#.#.#######.#
|
||||||
|
#...#.#.....#...#...#.#...............#.#...#.........#.#.#...#...#.....#.#...#...#...#.#.#.........#...........#.#.#.#...#...#.#.#.#...#.#.#
|
||||||
|
#.#.#.#.#.#.###.###.#.#####.#####.###.#.#.#.#########.#.#######.#.#.###.#.###.###.#.###.#.#####.#.#.#####.#####.#.#.#.###.#####.#.###.#.#.#.#
|
||||||
|
#.....#.#.#...#.#...#...#.........#.....#.#.......#.#...........#.#.#.#.#.......#...#...#.....#.#.#.#.....#.....#.#...#.#.#...#.#.#...#.#.#.#
|
||||||
|
#.#######.###.#.#.###.#.###.#####.#.###########.#.#.#####.###.#.###.#.#.#.#.###.#####.#.#####.#.#.#.###.#.#.#####.#####.#.#.#.#.#.#.###.#.#.#
|
||||||
|
#.........#.#...#...#.......#.....#.#.........#.#.......#...#.........#.#.#...#...#...#...#...#.#.#.....#.#.....#...........#...#...#.#.#.#.#
|
||||||
|
###########.#.#.###.#.#########.###.#.#.#####.#.#######.###.#.#########.#.###.###.###.#####.#.#.#.#######.#####.###############.#####.#.#.#.#
|
||||||
|
#.....#.......#...#.#...#.......#...#.#.#...#...#.....#.....#.#.........#.......#.....#.....#.#.#.....#.#.....#...........#...#...#...#.#.#.#
|
||||||
|
#.#.#.#.#.###.###.#.###.#.#####.#.#####.#.#######.###.#######.#.###############.#####.#.#######.#####.#.###.#############.###.#.#.#.###.#.#.#
|
||||||
|
#.#.#.#.#.........#.#...#...#...#.#.....#.......#...#.#.....#.#.....#.....#.......#...#...#.....#.#...#.....#.........#...#.....#...#...#...#
|
||||||
|
#.#.#.#.#####.#####.#####.###.#.#.#.#####.###.###.#.#.#####.#.#####.#.###.#.#.#####.#####.#.###.#.#.###.#####.#.#######.#.#.#######.#.#####.#
|
||||||
|
#.#...#.#.#.....#...#.....#...#.#...#.......#.....#.#.......#.......#...#...#.#.....#...#...#.....#.#...#...#.#.........#.#.........#.#...#.#
|
||||||
|
#.###.#.#.#.#.#.#.#.#.#####.#############.#########.#.#####.###########.#####.#.#######.#########.#.###.#.#.#.###########.#.#########.#.#.#.#
|
||||||
|
#...#.#...#...#...#.#...#.#.............#.#.........#.#...#.#...#.......#.#...#.#.....#.........#.#...#.#.#...#.......#...#...#.....#.#.#.#.#
|
||||||
|
###.#.#.#.###.#########.#.#######.#####.###.#########.#.#.#.#.#.#.#######.#.###.#.###.#.#######.#.###.#.#.#####.#####.#.#.###.#####.#.#.#.###
|
||||||
|
#.#.#...#...#...........#...#.....#...#...#.#.#.....#.#.#.#.#.#.#...#.........#...#...#.......#.....#.#.#...#...#...#.......................#
|
||||||
|
#.#.#.#.###.#.#.###########.#.###.#.#.###.#.#.#.###.#.#.#.#.#.#.###.#########.#####.#####.###.#####.#.#####.#.#####.#.#.#.#.#.#.###.#.#.###.#
|
||||||
|
#.#.#.....#...#.....................#...#...#.....#...#.#.#...#.....#.....#.#...#.#.#...#...#.....#.#.....#...#.....#.#.#.#.#.#.#.#.#.#.#.#.#
|
||||||
|
#.#.#.#####.#.#.#######.#####.#.###.###.###.#######.###.#.#######.###.###.#.###.#.#.#.#.###.#.###.#.#####.#####.#.###.#.#.#.#.#.#.#.#.#.#.#.#
|
||||||
|
#.#.#.#...#...#.#...#...#.....#.#.#.#.#...#.#.....#.#...#.#...#...#...#...#.......#...#.#.......#.#...#...#...#.#.....#...#.....#...#.....#.#
|
||||||
|
#.#.#.#.#.#.#.###.#.#.#.#.#####.#.#.#.###.###.###.###.###.#.#.#.###.###.#######.#######.#.###.###.#.###.###.#.#.#############.###########.#.#
|
||||||
|
#.#.#...#...#.....#...#.#.#...#.............#.#.#...#.#.#.#.#.....#.#.#.......#.#.....#.#...............#...#.#.#.....#.....#.#.........#.#.#
|
||||||
|
#.#.#.###.#.#.#########.#.#.#.#####.#.#.###.#.#.###.#.#.#.#########.#.#######.###.###.#.#####.#.#.#.#.###.###.#.#.###.#.###.###.#######.###.#
|
||||||
|
#S............#.........#...#.........#...#.......#.....#.....................................#...#.#.......#.....#.....#.......#...........#
|
||||||
|
#############################################################################################################################################
|
15
day16/sample1.in
Normal file
15
day16/sample1.in
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
###############
|
||||||
|
#.......#....E#
|
||||||
|
#.#.###.#.###.#
|
||||||
|
#.....#.#...#.#
|
||||||
|
#.###.#####.#.#
|
||||||
|
#.#.#.......#.#
|
||||||
|
#.#.#####.###.#
|
||||||
|
#...........#.#
|
||||||
|
###.#.#####.#.#
|
||||||
|
#...#.....#.#.#
|
||||||
|
#.#.#.###.#.#.#
|
||||||
|
#.....#...#.#.#
|
||||||
|
#.###.#.#.#.#.#
|
||||||
|
#S..#.....#...#
|
||||||
|
###############
|
17
day16/sample3.in
Normal file
17
day16/sample3.in
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#################
|
||||||
|
#...#...#...#..E#
|
||||||
|
#.#.#.#.#.#.#.#.#
|
||||||
|
#.#.#.#...#...#.#
|
||||||
|
#.#.#.#.###.#.#.#
|
||||||
|
#...#.#.#.....#.#
|
||||||
|
#.#.#.#.#.#####.#
|
||||||
|
#.#...#.#.#.....#
|
||||||
|
#.#.#####.#.###.#
|
||||||
|
#.#.#.......#...#
|
||||||
|
#.#.###.#####.###
|
||||||
|
#.#.#...#.....#.#
|
||||||
|
#.#.#.#####.###.#
|
||||||
|
#.#.#.........#.#
|
||||||
|
#.#.#.#########.#
|
||||||
|
#S#.............#
|
||||||
|
#################
|
44
day16/sol.py
Normal file
44
day16/sol.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import astar
|
||||||
|
|
||||||
|
|
||||||
|
def solve(file):
|
||||||
|
map = [line.strip() for line in file]
|
||||||
|
for y in range(len(map)):
|
||||||
|
for x,c in enumerate(map[y]):
|
||||||
|
if c == 'S':
|
||||||
|
start = (x,y,'>')
|
||||||
|
elif c == 'E':
|
||||||
|
end = (x,y)
|
||||||
|
|
||||||
|
def isgoal(n):
|
||||||
|
x,y,_ = n
|
||||||
|
#print("isgoal", n)
|
||||||
|
return (x,y) == end
|
||||||
|
|
||||||
|
def get(x,y):
|
||||||
|
return map[y][x]
|
||||||
|
|
||||||
|
print(map)
|
||||||
|
print(start, end)
|
||||||
|
|
||||||
|
def neighbors(src):
|
||||||
|
x,y,dir = src
|
||||||
|
n = []
|
||||||
|
def push(x,y, newdir):
|
||||||
|
if 0 <= y < len(map):
|
||||||
|
if 0 <= x < len(map[y]):
|
||||||
|
if get(x,y) != '#':
|
||||||
|
cost = 1 if dir == '.' or newdir == dir else 1001
|
||||||
|
n.append((cost, (x,y,newdir)))
|
||||||
|
push(x-1, y, '<')
|
||||||
|
push(x+1,y, '>')
|
||||||
|
push(x, y-1, '^')
|
||||||
|
push(x, y+1, 'v')
|
||||||
|
return n
|
||||||
|
|
||||||
|
cost, _, nodes = astar.search(start, isgoal, neighbors)
|
||||||
|
print(cost)
|
||||||
|
print(len(set((x,y) for x,y,_ in nodes)))
|
||||||
|
|
||||||
|
solve(open('sample1.in'))
|
||||||
|
solve(open('input'))
|
Loading…
x
Reference in New Issue
Block a user