Unable to load image

Day 12

Post code, nerds.

14
Jump in the discussion.

No email address required.

Babby's first shortest path graph algorithm. I got incredibly lucky with part 2, I only had to modify one line lol.


foo = []
with open('day12_input.txt', 'r') as inp:
    foo = inp.read().split('\n')

maze = [[0 for x in range(len(foo[0]))] for y in range(len(foo))] 
dist = [[6969 for x in range(len(foo[0]))] for y in range(len(foo))] 

dest_pos = []
for f in range(len(foo)):
    for g in range(len(foo[0])):
        if foo[f][g] == 'S': # or foo[f][g] == 'a': (for Part 2)
            maze[f][g] = 1
            dist[f][g] = 0
        elif foo[f][g] == 'E':
            dest_pos = [g, f]
            maze[f][g] = 26
        else:
            maze[f][g] = ord(foo[f][g]) - 96

def get_valid_adj(x, y):
    adj = []
    if x > 0 and maze[y][x-1]-1 <= maze[y][x]:
        adj.append([x-1, y])
    if y > 0 and maze[y-1][x]-1 <= maze[y][x]:
        adj.append([x, y-1])
    if x < len(foo[0])-1 and maze[y][x+1]-1 <= maze[y][x]:
        adj.append([x+1, y])
    if y < len(foo)-1 and maze[y+1][x]-1 <= maze[y][x]:
        adj.append([x, y+1])
    return adj

current_steps = 0
while True:
    for f in range(len(foo)):
        for g in range(len(foo[0])):
            if dist[f][g] == current_steps:
                for a in get_valid_adj(g, f):
                    if a == dest_pos:
                        print(current_steps+1)
                        quit()
                    elif dist[a[1]][a[0]] == 6969:
                        dist[a[1]][a[0]] = current_steps+1
    current_steps += 1

Jump in the discussion.

No email address required.

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