Last active
December 6, 2025 18:25
-
-
Save jul/089dc2c7302d30627ef352c71b61ba1a to your computer and use it in GitHub Desktop.
advent6.1
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
| input="""111 222 33 44~~ | |
| 111 222 33 44~~ | |
| 111 222 33 44~~ | |
| 123 328 51 64~ | |
| 45 64 387 23~ | |
| 6 98 215 314 | |
| 1 11 111 111 | |
| * + * +""" | |
| import re | |
| from functools import reduce | |
| all= input.split("\n") | |
| X = len(all) | |
| Y=len(re.split(r"\s+", all[0].strip())) | |
| Z=len(all[0]) | |
| C=Z//Y | |
| print(f"{X} {Y} {Z} {C}") | |
| stack=[ ["",] * (Y+1 ) for x in range(Y) ] | |
| print(input) | |
| def mysplit(l): | |
| fs=True | |
| accu="" | |
| for i in l: | |
| if " " == i: | |
| if fs: | |
| yield accu | |
| fs=False | |
| else: | |
| accu += " " | |
| else: | |
| accu+=i | |
| fs=True | |
| for x,l in enumerate(reversed(all)): | |
| for y,col in enumerate(list(mysplit(l))[::-1]): | |
| if col in {"*", "+" }: | |
| col.strip() | |
| stack[Y-y-1][-1]=col | |
| else: | |
| print("<%s>" % col) | |
| col = l[y*(C):y*C+C-1] | |
| print("<%s>" % col) | |
| col=col.replace(" ", "0") | |
| #print(col) | |
| for index,digit in enumerate(col): | |
| # print(f"{x},{y}, {col}, {index}, {digit}") | |
| #print(index) | |
| stack[Y-y-1][index] = digit + stack[Y-y-1][index] | |
| #stack[y][index] += int(digit)*(10**(x-1)) | |
| # print(stack) | |
| total=0 | |
| print(stack) | |
| print() | |
| for l in stack: | |
| print(list(map(lambda s:s.strip("0"),l))) | |
| assert l[-1] in { '*', '+' } | |
| total += reduce([int.__mul__, int.__add__][l[-1] == '+'],map(lambda s:int(s.strip("0") or "0") , l[0:-1])) | |
| print( total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment