Guest User

AoC 2024 day 11

a guest
Dec 11th, 2024
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | Source Code | 0 0
  1. from collections import defaultdict
  2.  
  3.  
  4. def get_stones(file):
  5.     values = [int(x) for x in open(file).read().strip().split(' ')]
  6.     dictionary = defaultdict(int)
  7.  
  8.     for i in range(len(values)):
  9.         dictionary[values[i]] = 1
  10.  
  11.     return dictionary
  12.  
  13.  
  14. def evolve(stones):
  15.     output = defaultdict(int)
  16.  
  17.     for mark, quantity in stones.items():
  18.         if mark == 0:  # rule 1
  19.             output[1] += quantity
  20.  
  21.         elif len(str(mark)) % 2 == 0:  # rule 2
  22.  
  23.             midpoint = len(str(mark)) // 2
  24.  
  25.             left = int(str(mark)[:midpoint])
  26.             right = int(str(mark)[midpoint:])
  27.  
  28.             output[left] += quantity
  29.             output[right] += quantity
  30.  
  31.         else:  # rule 3
  32.             output[mark * 2024] += quantity
  33.  
  34.     return output
  35.  
  36.  
  37. def main(file, times):
  38.     stones = get_stones(file)
  39.     for i in range(times):
  40.         stones = evolve(stones)
  41.  
  42.     return sum(stones.values())
  43.  
  44.  
  45. if __name__ == '__main__':
  46.     print(main('input.txt', 25))
  47.     print(main('input.txt', 75))
  48.  
Advertisement
Add Comment
Please, Sign In to add comment