summaryrefslogtreecommitdiff
path: root/2024/day11/solve1_2.py
blob: e2eef4322c2fcf986b4362cd15a1dac768d16e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)