Last active
December 11, 2024 11:07
-
-
Save object/8445ed87f3196c2b5d514aac582d4557 to your computer and use it in GitHub Desktop.
Advent of Code 2024, day 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from itertools import combinations | |
sys.setrecursionlimit(10**6) | |
with open("./data/input11.txt") as inputFile: | |
input = inputFile.read() | |
stones = [] | |
for num in input.split(' '): | |
stones.append(int(num)) | |
def replace_stone(stone): | |
replacement = [] | |
stonestr = str(stone) | |
if stone == 0: | |
replacement.append(1) | |
elif len(stonestr) % 2 == 0: | |
halflen = len(stonestr) // 2 | |
replacement.append(int(stonestr[0:halflen:])) | |
replacement.append(int(stonestr[halflen::])) | |
else: | |
replacement.append(stone * 2024) | |
return replacement | |
acc = dict() | |
def expand_stone(stone, times): | |
if times == 0: return 1 | |
elif (stone, times) in acc.keys(): return acc[(stone, times)] | |
else: | |
stones = replace_stone(stone) | |
if stone < 100: | |
acc[(stone, 1)] = len(stones) | |
total = 0 | |
for s in stones: | |
total += expand_stone(s, times-1) | |
acc[(stone, times)] = total | |
return total | |
def get_length(stones, times): | |
total = 0 | |
for stone in stones: | |
total += expand_stone(stone, times) | |
return total | |
# Part 1 | |
print(get_length(stones, 25)) | |
# Part 2 | |
print(get_length(stones, 75)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment