diff options
Diffstat (limited to '2024/day11/solve1_2.py')
-rw-r--r-- | 2024/day11/solve1_2.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2024/day11/solve1_2.py b/2024/day11/solve1_2.py new file mode 100644 index 0000000..e2eef43 --- /dev/null +++ b/2024/day11/solve1_2.py @@ -0,0 +1,50 @@ +memoize = {} +class Globals: + memoize_used = 0 + +def count_digits(number: int) -> int: + if number == 0: + return 1 + + count = 0 + while number != 0: + number = number // 10 + count += 1 + + return count + +def count_after_blinks(stone: int, blink_remaining: int): + if blink_remaining == 0: + return 1 + + elif (stone, blink_remaining) in memoize: + Globals.memoize_used += 1 + return memoize[(stone, blink_remaining)] + + else: + if stone == 0: + result = count_after_blinks(1, blink_remaining - 1) + + else: + digits = count_digits(stone) + if digits % 2 == 0: + divisor = 10 ** (digits // 2) + result = count_after_blinks(stone % divisor, blink_remaining - 1) + count_after_blinks(stone // divisor, blink_remaining - 1) + + else: + result = count_after_blinks(stone * 2024, blink_remaining - 1) + + memoize[(stone, blink_remaining)] = result + return result + +data = [] +with open("input") as f: + data.extend((int(elem) for elem in f.read().strip().split())) + +count = 0 +for elem in data: + count += count_after_blinks(elem, 75) + print(f"elem {elem} done") + +print(Globals.memoize_used) +print(count)
\ No newline at end of file |