Unable to load image

Day 12

Post code, nerds.

14
Jump in the discussion.

No email address required.

Implemented my own totally faggy version of a-star. Edit: Included both variants in my result

with open('input12.txt') as file:
    lines = [[ord(c)-ord('a') for c in list(line.strip())] for line in file] # -14 == S # -28 == E
    
class Node():
    def __init__(self,x ,y, steps, cost) -> None:
        self.x = x
        self.y = y
        self.height = lines[y][x]
        self.steps = steps
        if self.height == -28:
            self.height = 25
        elif self.height == -14:
            self.height = 0
        self.cost = self.steps + self.height
        
def check_neighbours(node_next_x, node_next_y, node, x_axis=True, reverse=False):
    if (x_axis and 0 <= node_next_x < len(lines[node_next_y])) or (not x_axis and 0 <= node_next_y < len(lines)):
        if reverse:
            height_diff = node.height-lines[node_next_y][node_next_x]
        else:
            height_diff = lines[node_next_y][node_next_x]-node.height
        if "{}/{}/{}/{}".format(node_next_x,node_next_y,node.x,node.y) not in looked_set and height_diff<=1:
            new_node = Node(node_next_x, node_next_y, node.steps+1, node.cost)
            node_list.append(new_node)
            looked_set.add("{}/{}/{}/{}".format(node_next_x,node_next_y,node.x,node.y))
    
part_a=True # False for part b
        
node_list = []
looked_set = set()
if part_a:
    start = Node(0,20,0,0)
    goal = -28
    pre_goal_height = 25
else:
    start = Node(40,20,0,0)
    goal = 0
    pre_goal_height = 1
node_list.append(start)
index = 0
while True:
    if len(node_list) == 0:
        print("Nothing found")
        break
    node = node_list.pop()
    if index%1000 == 0:
        print("here", node.steps, len(node_list))
    if node.height == pre_goal_height:
        if goal in [lines[node.y+1][node.x],lines[node.y-1][node.x],lines[node.y][node.x+1],lines[node.y][node.x-1]]:
            print("Found! Steps:", node.steps+1)
            break
    check_neighbours(node.x+1, node.y, node, True, not part_a)
    check_neighbours(node.x-1, node.y, node, True, not part_a)
    check_neighbours(node.x, node.y+1, node, False, not part_a)
    check_neighbours(node.x, node.y-1, node, False, not part_a)
    node_list.sort(key=lambda n: n.cost, reverse=True)
    index += 1
Jump in the discussion.

No email address required.

Are you feeling okay bud?

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.