Advertisement
Guest User

Advent of Code Day 17 part 2

a guest
Dec 17th, 2023
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. with open("2023_17input.txt", 'r') as file:
  2.     my_input = file.read()
  3.  
  4. raw_data = my_input
  5.  
  6. grid = raw_data.split('\n')
  7.  
  8. start_position = (0, 0)
  9. end_position = (len(grid[0]) - 1, len(grid) - 1)
  10.  
  11. ways = {
  12.     'r': (1, 0),
  13.     'd': (0, 1),
  14.     'l': (-1, 0),
  15.     'u': (0, -1),
  16.     }
  17.  
  18. min_cooling = len(grid) * len(grid[0]) * 9
  19.  
  20. def add_pos(a, b):
  21.     return tuple([x+y for x, y in zip(a, b)])
  22.    
  23. def find_cooling(x, y):
  24.     return int(grid[y][x])
  25.  
  26. def vet_way(way, pos, lastmoves):
  27.     # possible objections:
  28.     # - being the eleventh in a row that's the same
  29.     if lastmoves:
  30.         if len(lastmoves) == 10 and lastmoves[-1] == way:
  31.             return False
  32.     # - not having gone four moves in the same direction yet
  33.         elif len(lastmoves) < 4 and lastmoves[-1] != way:
  34.             return False
  35.     # - going back on yourself
  36.         if add_pos(ways[way], ways[lastmoves[-1]]) == (0, 0):
  37.             return False
  38.     new_pos = add_pos(ways[way], pos)
  39.     # - going out of bounds
  40.     if new_pos[0] < 0 or new_pos[0] > len(grid[0]) - 1:
  41.         return False
  42.     if new_pos[1] < 0 or new_pos[1] > len(grid) - 1:
  43.         return False
  44.     return True
  45.  
  46. # make massive dictionary of every possible travel state at every position
  47. records = {}
  48.  for y in range(len(grid)):
  49.     for x in range(len(grid[0])):
  50.         for i in range(1, 11):
  51.             for way in ways:
  52.                 records[((x, y), way*i)] = min_cooling
  53.                
  54.  
  55. records[((start_position), "")] = 0
  56.  
  57. # start at the starting point with count 0 and no moves made
  58. changes = {(start_position, "")}
  59.  
  60. while changes:
  61.     print(len(changes))
  62.     new_changes = set()
  63.     for pos, lastmoves in changes:
  64.         if pos == end_position:
  65.             pass
  66.         else:
  67.             for way in ways:
  68.                 if vet_way(way, pos, lastmoves):
  69.                     new_pos = add_pos(pos, ways[way])
  70.                     newcooling = records[(pos, lastmoves)] + find_cooling(*new_pos)
  71.                     if way in lastmoves:
  72.                         next_moves = lastmoves + way
  73.                     else:
  74.                         next_moves = way
  75.                     if records[(new_pos, next_moves)] > newcooling:
  76.                         new_changes.add((new_pos, next_moves))
  77.                         records[(new_pos, next_moves)] = newcooling
  78.     changes = new_changes
  79.  
  80. for record in records:
  81.     # remember to make sure you've done at least four moves
  82.     # in the current direction, otherwise test input 2 fails
  83.     if record[0] == end_position and len(record[1]) > 3:
  84.         if records[record] < min_cooling:
  85.             min_cooling = records[record]
  86. print(min_cooling)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement