From 111f332b6ba4b44b160951212b7bfbd747775afe Mon Sep 17 00:00:00 2001 From: kleph Date: Sat, 7 Dec 2024 16:05:28 +0100 Subject: [PATCH] 2024 - Day 7 part 2 --- 2024/7/7_2.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2024/7/7_2.py diff --git a/2024/7/7_2.py b/2024/7/7_2.py new file mode 100644 index 0000000..20473dd --- /dev/null +++ b/2024/7/7_2.py @@ -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))