day 17 fix cost calculation
i was accidentally using the previous coordinates to calculate the cost for each node, which means the A* search was effectively calculating the cost of the return trip instead of the forward path. doesn't affect the path at all - the start and end nodes are fixed, so it doesn't matter whether their costs are included or not - but it does mean that the cost being returned at the end was off by ((value at end point) - (value at start point)) compared to the expected value.main
parent
14ef33326e
commit
fe76e83e98
|
@ -10,7 +10,7 @@ def solve(map,part=1):
|
|||
start = (0,0,(0,0),0)
|
||||
goal = (len(map)-1,len(map[-1])-1)
|
||||
def is_goal(x):
|
||||
return (x[1],x[0]) == goal
|
||||
return x[:2] == goal
|
||||
def neighbors(state):
|
||||
y,x,dir,count = state
|
||||
next = []
|
||||
|
@ -33,7 +33,7 @@ def solve(map,part=1):
|
|||
if newcount <= count < 4:
|
||||
return
|
||||
if (0 <= newy < len(map)) and (0 <= newx < len(map[newy])):
|
||||
next.append((map[y][x], (newy, newx, (dx,dy), newcount)))
|
||||
next.append((map[newy][newx], (newy, newx, (dx,dy), newcount)))
|
||||
go(-1,0)
|
||||
go(+1,0)
|
||||
go(0,-1)
|
||||
|
@ -41,12 +41,9 @@ def solve(map,part=1):
|
|||
return next
|
||||
|
||||
cost, node, path = astar.search(start, is_goal, neighbors)
|
||||
print(cost) # TODO: off by one somehow?
|
||||
print(cost)
|
||||
print([(x,y) for (y,x,_,_) in path])
|
||||
print(''.join(dir2s(d) for (_,_,d,_) in path))
|
||||
costs = [map[y][x] for (y,x,_,_) in path[1:]]
|
||||
print(costs)
|
||||
print(sum(costs))
|
||||
|
||||
def dir2s(d):
|
||||
if d == (0,1): return "v"
|
||||
|
|
Loading…
Reference in New Issue