Implement 2025 day 6

This commit is contained in:
2026-02-16 11:51:21 -05:00
parent 867a7ed2df
commit 42e0c68d93

View File

@@ -1,4 +1,7 @@
from lib import get_data from lib import get_data
from typing import TypeVar
T = TypeVar('T')
data = """123 328 51 64 data = """123 328 51 64
45 64 387 23 45 64 387 23
@@ -26,4 +29,32 @@ for xs in lines:
assert False, "Unexpected op" assert False, "Unexpected op"
print(r) print(r)
def split(xs: list[T], delim: T) -> list[list[T]]:
res = [[]]
for x in xs:
if x == delim:
res.append([])
else:
res[-1].append(x)
return res
chars_trans: list[tuple[str]] = list(zip(*[line for line in data.splitlines()]))
lines_trans: list[str] = list(map(lambda line: "".join(line).strip(), chars_trans))
cols: list[list[str]] = split(lines_trans, '')
r2 = 0
for xs in cols:
x0 = xs[0]
acc, op = int(x0[:-1]), x0[-1]
for x in xs[1:]:
x = int(x)
if op == "+":
acc += x
elif op == "*":
acc *= x
else:
assert False, "Unexpected op"
r2 += acc
print(r2)