59 lines
1.2 KiB
Python
59 lines
1.2 KiB
Python
import math
|
|
|
|
INPUT_FILE = "input.txt"
|
|
|
|
problems: list[list[int]] = []
|
|
|
|
with open(INPUT_FILE, 'r') as file:
|
|
lines: list[str] = file.readlines()
|
|
|
|
raw: list[str] = []
|
|
for line in lines[:-1]:
|
|
raw.append(line[:-1])
|
|
|
|
problems = [ [] for _ in range(len(lines[0].split())) ]
|
|
|
|
problem_i = len(problems) - 1
|
|
# Prepare all problems
|
|
for x in range(len(raw[0])):
|
|
column_divider = True
|
|
n = ''
|
|
for y in range(len(raw)):
|
|
c = raw[y][x]
|
|
|
|
if c != ' ':
|
|
column_divider = False
|
|
n += c
|
|
|
|
if n:
|
|
problems[problem_i].append(int(n))
|
|
|
|
if column_divider:
|
|
problem_i -= 1
|
|
|
|
|
|
|
|
# for line in lines[:-1]:
|
|
# for c in reversed(line):
|
|
# for i, n in enumerate([ int(n.strip()) for n in line.split() ]):
|
|
# problems[i].append(n)
|
|
|
|
operations: list[str] = list(reversed(lines[-1].split()))
|
|
|
|
total = 0
|
|
for i, problem in enumerate(problems):
|
|
operation: str = operations[i]
|
|
|
|
if operation == '+':
|
|
print(f'{problem} {operation} = {sum(problem)}')
|
|
|
|
total += sum(problem)
|
|
elif operation == '*':
|
|
print(f'{problem} {operation} = {math.prod(problem)}')
|
|
total += math.prod(problem)
|
|
|
|
print(f'Total Value: {total}')
|
|
|
|
|
|
|