advent_of_code/2024/7/7_2.py

36 lines
898 B
Python
Raw Permalink Normal View History

2024-12-07 16:05:28 +01:00
#!/usr/bin/env python
# 2024 - Advent Of Code 7 - part2
# file = 'input_example.txt'
file = 'input.txt'
# pylint: disable=consider-using-with
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
def next_op(a, i, t):
if i < len(nums):
r1 = next_op(a + nums[i], i + 1, t)
r2 = next_op(a * nums[i], i + 1, t)
r3 = next_op(int(str(a) + str(nums[i])), i + 1, t)
return r1 or r2 or r3
# print(f'end at {i}, value {a}')
if a == target:
return True
return False
good_values = []
for line in input_lines:
target, numbers = line.split(':')
target = int(target)
nums = [int(x) for x in numbers.split(' ') if x != '']
# print(f'{target=}')
# print(f'{nums=}')
index = 1
if next_op(nums[0], index, target):
# print(f'{target} OK')
good_values.append(target)
print(sum(good_values))