Unable to load image

The luckiest of days, Day 13!!

Let's fricking gooo!

17
Jump in the discussion.

No email address required.

silently judging anyone who triggers longpostbot

def check(L: list | int, R: list | int) -> bool | None:
    Li, Ri = isinstance(L, int), isinstance(R, int)
    if Li and Ri: return None if L == R else L < R
    if Li: return check([L,], R)
    if Ri: return check(L, [R,])
    try:
        for l, r in zip(L, R, strict=True):
            if (output := check(l, r)) is not None:
                return output
    except ValueError:
        return len(L) < len(R)

with open('13', 'r') as file:
    raw = [x.split('\n') for x in file.read().strip().split('\n\n')]
pairs = [list(map(eval, pair)) for pair in raw]
packets = [x for mid in pairs for x in mid] + [[[2]], [[6]]]
for i in range(len(packets)):
    for j in range(len(packets)-i-1):
        if check(packets[j], packets[j+1]) is False:
            packets[j], packets[j+1] = packets[j+1], packets[j]
print(sum(i + 1 for i, pair in enumerate(pairs) if check(*pair)))
print((1 + packets.index([[6]])) * (1 + packets.index([[2]])))
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.