This commit is contained in:
parent
c2a7bc4769
commit
111f332b6b
1 changed files with 35 additions and 0 deletions
35
2024/7/7_2.py
Normal file
35
2024/7/7_2.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#!/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))
|
Loading…
Reference in a new issue