Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- l = [list(x) for x in open('input').read().splitlines()]
- dirs = {'>': (0, 1), '<': (0, -1), '^': (-1, 0), 'v': (1, 0)}
- mirrors = {
- '/': {'>': ['^'], '<': ['v'], '^': ['>'], 'v': ['<']},
- '\\': {'>': ['v'], '<': ['^'], '^': ['<'], 'v': ['>']},
- '|': {'>': ['v', '^'], '<': ['v', '^'], '^': ['^'], 'v': ['v']},
- '-': {'>': ['>'], '<': ['<'], '^': ['>', '<'], 'v': ['>', '<']}}
- mirrors['.'] = {i: [i] for i in dirs.keys()}
- def energized(start=(0, 0, '>')):
- ene = set()
- stack = [start]
- while stack:
- x, y, dr = stack.pop()
- if (x, y, dr) in ene:
- continue
- ene.add((x, y, dr))
- for d in mirrors[l[x][y]][dr]:
- x, y = x + dirs[d][0], y + dirs[d][1]
- if x in range(len(l)) and y in range(len(l[0])):
- stack.append((x, y, d))
- return len(set((x, y) for x, y, _ in ene))
- starts=[]
- for x in range(len(l)):
- starts+=[(x, 0, '>'),(x, len(l[0]) - 1, '<')]
- for y in range(len(l[0])):
- starts+=[(0, y, 'v'),(len(l) - 1, y, '^')]
- print(f"Part 1: {energized()}")
- print(f"Part 2: {max(energized(start) for start in starts)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement