Unable to load image

Day 12

Post code, nerds.

14
Jump in the discussion.

No email address required.

I had everything prepared, EVERYTHING, and still failed miserably because of bugs.

    g = networkx.DiGraph()

    def translate(c):
        if c == 'S': return ord('a')
        if c == 'E': return ord('z')
        return ord(c)

    def get(p):
        i, j = p
        if 0 <= i < len(data):
            if 0 <= j < len(data[i]):
                return translate(data[i][j])
        return -5

    start, goal = [], None

    for i, s in enumerate(data):
        for j, c in enumerate(s):
            p = (i, j)
            if c == 'S' or (c == 'a' and second):
                start.append(p)
            elif c == 'E':
                assert not goal
                goal = p

            for dir in directions4:
                p2 = addv2(p, dir)
                if get(p2) - get(p) <= 1:
                    g.add_edge(p, p2)

    found, visited = bfs_full(start, lambda x: x == goal, lambda x: g.neighbors(x))
    assert found
    p = bfs_extract_path(goal, visited)
    return len(p) - 1
Jump in the discussion.

No email address required.

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