Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day9_part2(s):
- generator = enumerate(more_itertools.grouper(s.strip(), 2, 'fill', 0))
- pairs: list[tuple[list[int], int]] = [([index] * int(a), int(b)) for index, (a, b) in generator]
- i1 = len(pairs) - 1
- while i1:
- lst, skip = pairs[i1]
- n = len(lst)
- for i0 in range(i1):
- if pairs[i0][1] >= n:
- pairs = pairs[:i0] + [(pairs[i0][0], 0), (lst, pairs[i0][1] - n)] + pairs[i0 + 1 :]
- i1 += 1
- pairs[i1] = [], skip + n
- break
- i1 -= 1
- checksum = position = 0
- for lst, skip in pairs:
- for index in lst:
- checksum += position * index
- position += 1
- position += skip
- return checksum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement