Advertisement
alexandrajay2002

Advent of Code 2024 day 11 part 1

Dec 13th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | Source Code | 0 0
  1. from sys import argv
  2.  
  3.  
  4. def parse(src):
  5.     '''Convert the input to a list of integers.'''
  6.     return [int(x) for x in src.split()]
  7.  
  8.  
  9. def step(stones):
  10.     '''Perform one blink.'''
  11.     new_stones = list()
  12.     for stone in stones:
  13.         if stone == 0:
  14.             new_stones.append(1)
  15.             continue
  16.  
  17.         stone_str = str(stone)
  18.         if len(stone_str) % 2 == 0:
  19.             middle = len(stone_str) // 2
  20.             new_stones += [int(stone_str[:middle]), int(stone_str[middle:])]
  21.  
  22.         else:
  23.             new_stones.append(stone * 2024)
  24.  
  25.     return new_stones
  26.  
  27.  
  28. def main(stones):
  29.     '''Perform 25 blinks and return the total number of stones.'''
  30.     for _ in range(25):
  31.         stones = step(stones)
  32.  
  33.     return len(stones)
  34.  
  35.  
  36. if __name__ == '__main__':
  37.     # pass the path to the puzzle input as the first argument
  38.     print(main(parse(open(argv[1]).read())))
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement