40 lines
843 B
Python
40 lines
843 B
Python
|
|
import re
|
|
|
|
def operate(operator,a,b):
|
|
|
|
if operator == "*":
|
|
return int(a) * int(b)
|
|
if operator == "+":
|
|
return int(a) + int(b)
|
|
|
|
return None
|
|
|
|
def read_input(filename):
|
|
|
|
sheet = []
|
|
for line in open(filename).read().splitlines():
|
|
|
|
line = re.sub('\s{2,}', ' ', line.strip()) # remove multiple spaces with a single one for correct splitting
|
|
row = line.split(" ")
|
|
sheet.append(row)
|
|
|
|
return sheet
|
|
|
|
# filename = "day06/example_input" # 4277556
|
|
filename = "day06/input" # 4719804927602
|
|
sheet = read_input(filename)
|
|
|
|
total = 0
|
|
for col in range(0,len(sheet[0])):
|
|
|
|
operator = sheet[-1][col]
|
|
row_res = sheet[0][col]
|
|
for row in range(1, len(sheet)-1):
|
|
row_res = operate(operator,row_res,sheet[row][col])
|
|
|
|
total += row_res
|
|
|
|
|
|
print(f"Total of sheet operations: {total}")
|