Advertisement
Guest User

Untitled

a guest
Dec 16th, 2024
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | Source Code | 0 0
  1. import cmath
  2. from collections import deque
  3. from typing import Deque, Tuple, Dict, List
  4. from copy import deepcopy
  5.  
  6. # Let's try complex numbers
  7.  
  8. path = "day_16.txt"
  9. # path = "test.txt"
  10.  
  11. with open(path) as f:
  12.     grid = [l.strip() for l in f.readlines()]
  13.  
  14. ROWS = len(grid)
  15. COLS = len(grid[0])
  16. start_direction = complex(0,1)
  17. start_pos = complex(0, 0)
  18. turn_clockwise = complex(0,1)
  19. turn_ctrclockwise = complex(0,-1)
  20.  
  21. for row in range(ROWS):
  22.     for col in range(COLS):
  23.         if grid[row][col] == 'S':
  24.             start_pos = complex(row, col)
  25.             break
  26.            
  27. q: Deque[Tuple[complex, complex, int, bool, Dict, List]] = deque()
  28. q.append((start_pos, start_direction, 0, False, {}, []))
  29.  
  30. def is_valid(pos:complex, grid):
  31.     r = int(pos.real)
  32.     c = int(pos.imag)
  33.     rows = len(grid)
  34.     cols = len(grid[0])
  35.    
  36.     return 0 <= r < rows and 0<= c < cols and grid[r][c] != '#'
  37.  
  38. def is_end(pos:complex, grid):
  39.     r = int(pos.real)
  40.     c = int(pos.imag)
  41.     rows = len(grid)
  42.     cols = len(grid[0])
  43.    
  44.     return 0 <= r < rows and 0<= c < cols and grid[r][c] == 'E'
  45.  
  46.  
  47. min_score = 1_000_000_000_000_000
  48. best_path = set()
  49.  
  50. while len(q) > 0:
  51.     pos, d, score, turned, visited, path = q.popleft()
  52.  
  53.     if (pos, d) in visited and visited[(pos,d)] < score:
  54.         continue
  55.  
  56.     visited[(pos,d)] = score
  57.     path = [pos] + path
  58.     if score > min_score:
  59.         continue
  60.    
  61.     new_pos = pos + d
  62.    
  63.     if is_end(new_pos, grid):
  64.         if score + 1 == min_score:
  65.             best_path.update(path)
  66.             continue
  67.        
  68.         if score <= min_score:
  69.             min_score = min(min_score, score + 1)
  70.  
  71.             best_path.clear()
  72.             best_path.update(path)
  73.             best_path.add(new_pos)
  74.            
  75.         continue
  76.            
  77.     if not turned:
  78.         q.append((pos, d*turn_clockwise, score + 1000, True, visited, path))
  79.         q.append((pos, d*turn_ctrclockwise, score + 1000, True, visited, path))
  80.    
  81.     if is_valid(new_pos, grid):
  82.         q.append((new_pos, d, score + 1, False, visited, path))
  83.        
  84.    
  85. print("p1:",min_score)
  86. print("p2:", len(best_path))
  87.  
  88. # If you want to print something...
  89. def print_grid(grid, path):
  90.     v = set(path)
  91.     for r in range(len(grid)):
  92.         print()
  93.         for c in range(len(grid[0])):
  94.             if complex(r, c) in v:
  95.                 print("0", end="")
  96.             else:
  97.                 print(grid[r][c], end="")
  98.                
  99.     print()
  100.     print('='*len(grid[0]))
  101.     print()
  102.    
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement