Bringing some days up to speed after a short break

This commit is contained in:
2024-12-09 11:27:22 -08:00
parent 9fac5539f0
commit c63736752b
4 changed files with 115 additions and 1 deletions

48
7/1.py Normal file
View File

@@ -0,0 +1,48 @@
import random
def test(expression: list[str|int]) -> int:
while len(expression) >= 3:
left, op, right = expression[0], expression[1], expression[2]
if len(expression) > 3:
expression = expression[3:]
else:
expression = []
if op == '*':
expression.insert(0, left * right)
else:
expression.insert(0, left + right)
return expression[0]
def genrate_expression(vals: list[int]) -> list[str|int]:
ops = [random.choice('*+') for _ in range(len(vals) - 1)]
expr = []
for item in zip(vals, ops):
expr.extend(item)
expr.append(vals[-1])
return expr
with open('input.data') as file:
inputs = []
for line in file:
vals = [int(val) for val in line.replace(':', '').split()]
inputs.append([vals[0], vals[1:]])
s = 0
for case in inputs:
answer, vals = case[0], case[1]
expr = genrate_expression(vals)
for i in range(20000):
expr = genrate_expression(vals)
result = test(expr)
if result == answer:
#print(expr)
s += answer
break
print(s)

9
7/sample.data Normal file
View File

@@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20