191 tokens in Python for part 2 by Abbas Moosajee
Download solution
fromsysimportstdin
lines=stdin.readlines()
max_len=max(len(row)forrowinlines)
padded_sheet=[row+" "*(max_len-len(row))forrowinlines]
total_sum=0
current_problem=operation=None
forcol_idxinrange(max_len):
column=[row[col_idx]forrowinpadded_sheet]
ifall(cell==" "forcellincolumn):
total_sum+=current_problem
current_problem=operation=None
continue
digits=[cellforcellincolumnifcell.isdigit()]
operators=[cellforcellincolumnifcellin"+*"]
ifdigits:
number=int("".join(digits))
ifcurrent_problemisNone:
current_problem=number
elifoperation=="+":
current_problem+=number
elifoperation=="*":
current_problem*=number
else:
current_problem=number
ifoperators:
operation=operators[0]
print(total_sum+current_problem)