Advertisement
icarussiano

day 16

Dec 16th, 2023 (edited)
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. l = [list(x) for x in open('input').read().splitlines()]
  2. dirs = {'>': (0, 1), '<': (0, -1), '^': (-1, 0), 'v': (1, 0)}
  3. mirrors = {
  4.     '/': {'>': ['^'], '<': ['v'], '^': ['>'], 'v': ['<']},
  5.     '\\': {'>': ['v'], '<': ['^'], '^': ['<'], 'v': ['>']},
  6.     '|': {'>': ['v', '^'], '<': ['v', '^'], '^': ['^'], 'v': ['v']},
  7.     '-': {'>': ['>'], '<': ['<'], '^': ['>', '<'], 'v': ['>', '<']}}
  8. mirrors['.'] = {i: [i] for i in dirs.keys()}
  9. def energized(start=(0, 0, '>')):
  10.     ene = set()
  11.     stack = [start]
  12.     while stack:
  13.         x, y, dr = stack.pop()
  14.         if (x, y, dr) in ene:
  15.             continue
  16.         ene.add((x, y, dr))
  17.         for d in mirrors[l[x][y]][dr]:
  18.             x, y = x + dirs[d][0], y + dirs[d][1]
  19.             if x in range(len(l)) and y in range(len(l[0])):
  20.                 stack.append((x, y, d))
  21.     return len(set((x, y) for x, y, _ in ene))
  22.  
  23. starts=[]
  24. for x in range(len(l)):
  25.     starts+=[(x, 0, '>'),(x, len(l[0]) - 1, '<')]
  26. for y in range(len(l[0])):
  27.     starts+=[(0, y, 'v'),(len(l) - 1, y, '^')]
  28.    
  29. print(f"Part 1: {energized()}")
  30. print(f"Part 2: {max(energized(start) for start in starts)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement