You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.7 KiB
62 lines
1.7 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
ENERGY = []
|
|
|
|
|
|
# direction given as tuple, e.g., (1,0) for right
|
|
def move_beam(x,y,d,grid):
|
|
while True:
|
|
if (x > len(grid[0])-1) or (x < 0) or (y > len(grid)-1) or (y < 0): break
|
|
if d in ENERGY[x+y*len(grid[0])]: break
|
|
ENERGY[x+y*len(grid[0])].append(d)
|
|
tile = grid[y][x]
|
|
if tile == '.':
|
|
x, y = x+d[0], y+d[1]
|
|
elif (tile == '-' and d[1] == 0) or (tile == '|' and d[0] == 0):
|
|
x,y = x+d[0], y+d[1]
|
|
elif tile == '\\':
|
|
d = (d[1],d[0])
|
|
x,y = x+d[0], y+d[1]
|
|
elif tile == '/':
|
|
d = (-d[1],-d[0])
|
|
x,y = x+d[0], y+d[1]
|
|
elif tile == '-':
|
|
move_beam(x-1, y, (-1,0), grid)
|
|
move_beam(x+1, y, (1,0), grid)
|
|
elif tile == '|':
|
|
move_beam(x, y-1, (0,-1), grid)
|
|
move_beam(x, y+1, (0,1), grid)
|
|
|
|
|
|
def energize(x,y,d,grid):
|
|
global ENERGY
|
|
ENERGY = []
|
|
for i in range(len(grid) * len(grid[0])):
|
|
ENERGY.append([])
|
|
move_beam(x,y,d,grid)
|
|
return len(ENERGY) - ENERGY.count([])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
grid = [list(line.strip('\n')) for line in open(sys.argv[1])]
|
|
res2 = 0
|
|
for i in range(len(grid)):
|
|
res2 = max(energize(0,i,(1,0),grid),res2)
|
|
for i in range(len(grid[0])):
|
|
res2 = max(energize(i,0,(0,1),grid),res2)
|
|
for i in range(len(grid)):
|
|
res2 = max(energize(len(grid)-1,i,(-1,0),grid),res2)
|
|
for i in range(len(grid[0])):
|
|
res2 = max(energize(i,len(grid[0])-1,(0,-1),grid),res2)
|
|
|
|
# challenge 1
|
|
res1 = str(energize(0,0,(1,0),grid))
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
res2 = str(res2)
|
|
print(f"challenge 2:\n{res2}\n")
|
|
|
|
|