Advertisement
Guest User

Untitled

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