day 11 & day 12
This commit is contained in:
52
2024/day11/day11a.py
Normal file
52
2024/day11/day11a.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from functools import cache
|
||||
from sys import argv
|
||||
|
||||
MAX_DEPTH = 75
|
||||
|
||||
|
||||
def parse(src):
|
||||
'''Convert the input to a list of integers.'''
|
||||
return [int(x) for x in src.split()]
|
||||
|
||||
|
||||
def count_digits(num):
|
||||
'''Count the digits of num in base 10'''
|
||||
total = 0
|
||||
while num > 0:
|
||||
total += 1
|
||||
num = num // 10
|
||||
return total
|
||||
|
||||
|
||||
@cache
|
||||
def expand(stone, depth=0):
|
||||
'''Traverse the 'timeline' depth-first and return the number of stones
|
||||
created by blinking at stone from depth to MAX_DEPTH blinks.'''
|
||||
while depth < MAX_DEPTH:
|
||||
if stone == 0:
|
||||
stone = 1
|
||||
depth += 1
|
||||
continue
|
||||
|
||||
digits = count_digits(stone)
|
||||
if digits % 2 == 0:
|
||||
mask = 10 ** (digits // 2)
|
||||
left = stone // mask
|
||||
right = stone % mask
|
||||
|
||||
return expand(left, depth + 1) + expand(right, depth + 1)
|
||||
|
||||
stone *= 2024
|
||||
depth += 1
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
def main(stones):
|
||||
'''The total number of stones produced after 75 blinks'''
|
||||
return sum(expand(stone) for stone in stones)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# pass the path to the puzzle input as the first argument
|
||||
print(main(parse(open('/home/bandeira/git.bandeira/aoc/2024/day11/input.day11').read())))
|
||||
@@ -0,0 +1 @@
|
||||
0 89741 316108 7641 756 9 7832357 91
|
||||
@@ -0,0 +1 @@
|
||||
125 17
|
||||
Reference in New Issue
Block a user