2025 06
This commit is contained in:
parent
403bd0d4e3
commit
4d49abf0ac
1 changed files with 51 additions and 0 deletions
51
Python/2025/06/main.py
Normal file
51
Python/2025/06/main.py
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
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)))
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue