kupuguy

Advent of Code 2024 Day 11

Dec 11th, 2024
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | Source Code | 0 0
  1. from pathlib import Path
  2. from collections import Counter, defaultdict
  3.  
  4. TEST = """125 17"""
  5.  
  6.  
  7. def blink(n: str) -> list[str]:
  8.     if n == "0":
  9.         return ["1"]
  10.     length = len(n)
  11.     if length % 2 == 0:
  12.         return [n[: length // 2], str(int(n[length // 2 :]))]
  13.     return [f"{int(n)*2024}"]
  14.  
  15.  
  16. def part1(input: str, blinks: int = 25) -> int:
  17.     stones = Counter(input.split())
  18.     for gen in range(blinks):
  19.         new_stones = defaultdict(int)
  20.         for stone, count in stones.items():
  21.             for blinked in blink(stone):
  22.                 new_stones[blinked] += count
  23.         stones = new_stones
  24.     return sum(stones.values())
  25.  
  26.  
  27. assert part1(TEST, 6) == 22
  28. assert part1(TEST) == 55312
  29.  
  30. INPUT = Path("input/day11.txt").read_text()
  31. part1_total = part1(INPUT)
  32. print(f"{part1_total=:,}")  # 198089
  33.  
  34. part2_total = part1(INPUT, 75)
  35. print(f"{part2_total=:,}")  # 236,302,670,835,517
  36.  
Advertisement
Add Comment
Please, Sign In to add comment