Unable to load image

Advent of Code Day 24: Blizzard Basin

How well have you all been keeping up this year? :marseycodecellove:

![](/images/16719077414259827.webp)

6
Jump in the discussion.

No email address required.

I enjoyed today's challenge, even if it was just a dynamic BFS. For part 2 I copy-pasted the last section of code two more times and changed the reachable list and end conditions accordingly.

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

grid, blizzards, dir = [], [], [[0,1],[1,0],[0,-1],[-1,0]]

for f in range(len(foo)):
    for g in range(len(foo[f])):
        if foo[f][g] == '>':
            blizzards.append([f, g, 0])
        elif foo[f][g] == 'v':
            blizzards.append([f, g, 1])
        elif foo[f][g] == '<':
            blizzards.append([f, g, 2])
        elif foo[f][g] == '^':
            blizzards.append([f, g, 3])
    grid.append([*foo[f].replace('>', '.').replace('<','.').replace('^', '.').replace('v', '.')])

def update_blizzards():
    for b in range(len(blizzards)):
        new_pos = [(blizzards[b][0] + dir[blizzards[b][2]][0])%len(grid), (blizzards[b][1] + dir[blizzards[b][2]][1])%len(grid[0])]
        while grid[new_pos[0]][new_pos[1]] == '#':
            new_pos = [(new_pos[0] + dir[blizzards[b][2]][0])%len(grid), (new_pos[1] + dir[blizzards[b][2]][1])%len(grid[0])]
        blizzards[b] = new_pos + [blizzards[b][2]]

reachable, turns = [(0,1)], 0
while (len(grid)-1, len(grid[0])-2) not in reachable:
    turns += 1
    update_blizzards()
    current_blizz, new_state = [(b[0], b[1]) for b in blizzards], []
    for r in reachable:
        if r not in current_blizz:
            new_state.append(r)
        for n in [(r[0], r[1]),(r[0], r[1]+1),(r[0], r[1]-1),(r[0]+1, r[1]), (r[0]-1, r[1])]:
            if 0 <= n[0] < len(grid) and 0 <= n[1] < len(grid[0]) and grid[n[0]][n[1]] != '#' and n not in current_blizz and n not in new_state and n not in reachable:
                new_state.append(n)
    reachable = new_state
print(turns)
Jump in the discussion.

No email address required.

If only you could put that energy into your relationships

Jump in the discussion.

No email address required.

  .-""-.
 /,..___\
() {_____}
  (/-@-@-\)
  {`-=^=-'}
  {  `-'  } Merry Fistmas!
   {     }
    `---'
Jump in the discussion.

No email address required.

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