commit 1738bec38d6ac146389f1926b6ef835f6f0662bd (patch)
parent 3f9b71551182553d6091cb5502f1fe876daf227e
Author: Alex Karle <alex@alexkarle.com>
Date: Sat, 7 Dec 2024 08:13:19 -0500
2024 Add day 7 part 2
Wesley helped!
Diffstat:
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/2024/07/2.py b/2024/07/2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+import sys
+
+def parse():
+ cases = []
+ for l in sys.stdin:
+ target = int(l.split(":")[0])
+ nums = [int(x) for x in l.split(" ")[1:]]
+ cases.append((target, nums))
+ return cases
+
+def check(case):
+ target, nums = case
+
+ # Since we don't need to know what operators we use,
+ # we can just recursively check the patterns)
+ def rchk(run, rem):
+ if not rem:
+ return run == target
+ return rchk(run * rem[0], rem[1:]) or rchk(run + rem[0], rem[1:]) or rchk(int(str(run) + str(rem[0])), rem[1:])
+
+ if rchk(nums[0], nums[1:]):
+ return target
+ return 0
+
+if __name__ == '__main__':
+ cases = parse()
+ tot = 0
+ for c in cases:
+ tot += check(c)
+ print(tot)