33 lines
830 B
Python
33 lines
830 B
Python
from functools import reduce # Required in Python 3
|
|
import operator
|
|
|
|
|
|
def prod(iterable):
|
|
return reduce(operator.mul, iterable, 1)
|
|
|
|
|
|
with open("example", "+r") as file:
|
|
f = file.readlines()
|
|
arr = [x.split() for x in f]
|
|
operations = arr[-1]
|
|
numbers = 0
|
|
for i, op in enumerate(operations):
|
|
values = [int(x[i]) for x in arr if x[i].isdigit()]
|
|
if op == "+":
|
|
numbers += sum(values)
|
|
if op == "*":
|
|
numbers += prod(values)
|
|
|
|
print("part1", numbers)
|
|
|
|
for i, op in enumerate(operations):
|
|
values: list = []
|
|
for x in arr:
|
|
if x[i].isdigit():
|
|
length = len(x[i])
|
|
values.append(
|
|
list(reversed([x[i][z : z + 1] for z in range(0, len(x[i]), 1)]))
|
|
)
|
|
|
|
print(values)
|