Advertisement
hhoppe

Advent of code 2024 day 9 part 2 (slow)

Dec 9th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. def day9_part2(s):
  2.   generator = enumerate(more_itertools.grouper(s.strip(), 2, 'fill', 0))
  3.   pairs: list[tuple[list[int], int]] = [([index] * int(a), int(b)) for index, (a, b) in generator]
  4.  
  5.   i1 = len(pairs) - 1
  6.   while i1:
  7.     lst, skip = pairs[i1]
  8.     n = len(lst)
  9.     for i0 in range(i1):
  10.       if pairs[i0][1] >= n:
  11.         pairs = pairs[:i0] + [(pairs[i0][0], 0), (lst, pairs[i0][1] - n)] + pairs[i0 + 1 :]
  12.         i1 += 1
  13.         pairs[i1] = [], skip + n
  14.         break
  15.     i1 -= 1
  16.  
  17.   checksum = position = 0
  18.   for lst, skip in pairs:
  19.     for index in lst:
  20.       checksum += position * index
  21.       position += 1
  22.     position += skip
  23.  
  24.   return checksum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement