advent_of_code/2024/day11/main.py

48 lines
955 B
Python
Raw Normal View History

2024-12-11 07:02:57 -05:00
from contextlib import contextmanager
import sys
import os
sys.path.insert(1, os.path.abspath('../../'))
from python_tools.aoc_utils import *
2024-12-11 08:22:47 -05:00
import collections as C
G = C.defaultdict(lambda: 0)
2024-12-11 07:02:57 -05:00
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]
2024-12-11 08:22:47 -05:00
def solve(stones, num):
G.clear()
for i in stones:
G[i] += 1
2024-12-11 07:02:57 -05:00
2024-12-11 08:22:47 -05:00
for i in range(num):
2024-12-11 07:02:57 -05:00
nx = []
2024-12-11 08:22:47 -05:00
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}")
2024-12-11 07:02:57 -05:00
def main():
f = open("input.txt", "r")
contents = list(map(int, f.read().strip().split()))
2024-12-11 08:22:47 -05:00
solve(contents, 25)
solve(contents, 75)
2024-12-11 07:02:57 -05:00
if __name__ == "__main__":
main()