43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
def read_input(filename):
|
|
sheet = []
|
|
for line in open(filename).read().splitlines():
|
|
sheet.append(list(line))
|
|
return sheet
|
|
|
|
# filename = "day07/example_input" # 40
|
|
filename = "day07/input" # 1560
|
|
sheet = read_input(filename)
|
|
|
|
for y in range(0,len(sheet)):
|
|
for x in range(0,len(sheet[y])):
|
|
if sheet[y][x] == "S":
|
|
sheet[y][x] = 1
|
|
if sheet[y][x] == "^":
|
|
sheet[y][x] = -1
|
|
if sheet[y][x] == ".":
|
|
sheet[y][x] = 0
|
|
|
|
# "Cheat alert"; figured this out after algorithm hints on reddit
|
|
for y in range(0,len(sheet)-1): # skip last line
|
|
for x in range(0,len(sheet[y])):
|
|
|
|
# Path starts with weight one
|
|
# Just add weight to next step
|
|
# if empty (0), weight stays same
|
|
# if path is "joined", weights are joined/added
|
|
|
|
if sheet[y][x] > 0:
|
|
# Shoot ray down
|
|
if sheet[y+1][x] == -1: # split
|
|
sheet[y+1][x+1] += sheet[y][x]
|
|
sheet[y+1][x-1] += sheet[y][x]
|
|
else:
|
|
sheet[y+1][x] += sheet[y][x]
|
|
# Last line of sheet contains the added/joined paths, add them up
|
|
|
|
paths = 0
|
|
for i in sheet[-1]:
|
|
paths += i
|
|
|
|
print(f"Total number of paths: {paths}")
|