Advertisement
hhoppe

Advent of code 2024 day 9 part 1 (7 ms)

Dec 9th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. def day9_part1(s):
  2.   values = list(map(int, list(s.strip())))
  3.   last_index = len(values) // 2
  4.   checksum = position = index = i = 0
  5.  
  6.   while i < len(values):
  7.     for _ in range(values[i]):
  8.       checksum += position * index
  9.       position += 1
  10.     i += 1
  11.     index += 1
  12.     if i < len(values):
  13.       for _ in range(values[i]):
  14.         while i + 1 < len(values):
  15.           if values[-1]:
  16.             checksum += position * last_index
  17.             position += 1
  18.             values[-1] -= 1
  19.             break          
  20.           values.pop(), values.pop()
  21.           last_index -= 1
  22.       i += 1
  23.  
  24.   return checksum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement