Created
December 7, 2024 06:13
-
-
Save qubard/60c15077910df1474efdc1ac0ae37c61 to your computer and use it in GitHub Desktop.
aoc day 7 pt 2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| from copy import deepcopy | |
| from collections import defaultdict, deque | |
| # READ FILE | |
| FILENAME = "A" | |
| f = open(FILENAME).readlines() | |
| f = [x.strip() for x in f] | |
| ans = 0 | |
| G = defaultdict(str) | |
| r = 0 | |
| ans = 0 | |
| YY=0 | |
| memo = {} | |
| for l in f: | |
| A = ints(l) | |
| i = 0 | |
| last = None | |
| P = [] | |
| print("LEN",A,len(A)) | |
| if l == "": | |
| break | |
| if len(A) not in memo: | |
| Q = deque([['*'],['+'],['|']]) | |
| while len(Q) > 0: | |
| V = Q.popleft() | |
| if len(V) == len(A)-1: | |
| P.append(V) | |
| else: | |
| for a in ['+','*','|']: | |
| Q.append(V + [a]) | |
| memo[len(A)] = P | |
| else: | |
| P = memo[len(A)] | |
| i=0 | |
| Q = deque(P) | |
| while len(Q) > 0: | |
| j = 0 | |
| B = A[1:] | |
| x = "" | |
| last = None | |
| T = i | |
| terms = Q.popleft() | |
| q = 0 | |
| while j < len(B): | |
| op = terms[q] | |
| if last == None: | |
| last = B[j] | |
| else: | |
| if op == '|': | |
| last = int(str(last)+str(B[j])) | |
| else: | |
| if op == '+': | |
| last = last+B[j] | |
| else: | |
| last = last*B[j] | |
| q += 1 | |
| j+=1 | |
| if last != None and last == A[0]: | |
| ans+=A[0] | |
| break | |
| i+=1 | |
| YY+=1 | |
| print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment