AdventOfCode/Python/2025/06/main.py

52 lines
1.4 KiB
Python
Raw Normal View History

2026-01-12 14:06:44 -08:00
input = open("input", 'r').read().rstrip().split("\n")
problems = input[0].split()
for line in input[1:]:
problems = list(zip(problems,line.split()))
def evaluate(value,values,op):
if isinstance(values, tuple):
return op(value, evaluate(values[1], values[0], op))
return op(value, values)
def part1(problems):
total = []
for problem in problems:
if problem[1] == "+":
op = lambda x,y: int(x)+int(y)
total.append(evaluate(0, problem[0], op))
elif problem[1] == "*":
op = lambda x,y: int(x)*int(y)
total.append(evaluate(1, problem[0], op))
return total
print(sum(part1(problems)))
# Part 2 :sob:
text_input = [line for line in input]
corrected_problems = []
height = len(text_input)
corrected_problem = None
op_code = None
for col in range(len(text_input[0])):
if op_code == None:
op_code = text_input[height-1][col]
value = [text_input[i][col] for i in range(height-1)]
if value == [" " for _ in range(height-1)]:
corrected_problems.append((corrected_problem, op_code))
corrected_problem = None
op_code = None
continue
if corrected_problem == None:
corrected_problem = int("".join(value))
else:
corrected_problem = (corrected_problem, int("".join(value)))
corrected_problems.append((corrected_problem, op_code))
print(sum(part1(corrected_problems)))