diff --git a/7/1.py b/7/1.py index 7522213..856d5f1 100644 --- a/7/1.py +++ b/7/1.py @@ -1,5 +1,4 @@ from itertools import product -import random def test(expression: list[str|int]) -> int: while len(expression) >= 3: @@ -22,7 +21,7 @@ def genrate_expression(vals: list[int], ops: list[str]) -> list[str|int]: return expr -with open('input.data') as file: +with open('sample.data') as file: inputs = [] for line in file: vals = [int(val) for val in line.replace(':', '').split()] diff --git a/7/2.py b/7/2.py new file mode 100644 index 0000000..286f314 --- /dev/null +++ b/7/2.py @@ -0,0 +1,41 @@ +from itertools import product + +def tokenize(vals: list[int], ops: list[str]) -> list[str|int]: + tok = [] + for item in zip(vals, ops): + tok.extend(item) + tok.append(vals[-1]) + return tok + +def compute(tokens: list[str|int]) -> int: + # Case where we have 1 || 2 ==> 12 + while len(tokens) >= 3: + # Pop off the first 3 items + left, op, right = tokens[:3] + tokens = tokens[3:] if len(tokens) > 3 else [] + # Now we do some computation and prepend the list with that result + if op == '*': + tokens.insert(0, left * right) + elif op == '+': + tokens.insert(0, left + right) + # the coalesce case + else: + tokens.insert(0, int(f'{left}{right}')) + return tokens[0] + + +with open('input.data') as file: + cases = [] + for line in file: + line = line.replace(':', '').split() + cases.append([int(val) for val in line]) + s = 0 + for case in cases: + answer, vals = case[0], case[1:] + results = set() + for ops in list(product(['*', '+', '||'], repeat=len(vals)-1)): + tokens = tokenize(vals, ops) + results.add(compute(tokens)) + if answer in results: + s += answer + print(s)