advent_of_code/2024/day11/main.py
Daniel Weber 3e6f031781 Part two
2024-12-11 08:22:47 -05:00

48 lines
955 B
Python

from contextlib import contextmanager
import sys
import os
sys.path.insert(1, os.path.abspath('../../'))
from python_tools.aoc_utils import *
import collections as C
G = C.defaultdict(lambda: 0)
def blink_rocks(r):
if r == 0:
return [1]
elif len(str(r)) % 2 == 0:
l = int(len(str(r)) / 2)
return [int(str(r)[:l]), int(str(r)[l:])]
else:
return [r*2024]
def solve(stones, num):
G.clear()
for i in stones:
G[i] += 1
for i in range(num):
nx = []
g = G.copy()
G.clear()
for j in g:
result = blink_rocks(j)
for k in result:
G[k]+=g[j]
ans = 0
for i in G:
ans += G[i]
print(f"Stones after {num} blinks: {ans}")
def main():
f = open("input.txt", "r")
contents = list(map(int, f.read().strip().split()))
solve(contents, 25)
solve(contents, 75)
if __name__ == "__main__":
main()