-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
62 lines (42 loc) · 1.18 KB
/
day11.py
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
51
52
53
54
55
56
57
58
59
60
61
62
# Advent of Code 2024, Day 11
# (c) blu3r4y
from functools import cache
from aocd.models import Puzzle
from funcy import print_calls, print_durations
@print_calls
@print_durations(unit="ms")
def part1(stones):
return sum(blink(stone, n=25) for stone in stones)
@print_calls
@print_durations(unit="ms")
def part2(stones):
return sum(blink(stone, n=75) for stone in stones)
@cache
def blink(stone, n=1):
if n == 0:
return 1
result = transform(stone)
if isinstance(result, tuple):
a, b = result
return blink(a, n - 1) + blink(b, n - 1)
return blink(result, n - 1)
@cache
def transform(stone):
if stone == 0:
return 1
text = str(stone)
if len(text) % 2 == 0:
left = int(text[: len(text) // 2])
right = int(text[len(text) // 2 :])
return left, right
return stone * 2024
def load(data):
return tuple(map(int, data.strip().split()))
if __name__ == "__main__":
puzzle = Puzzle(year=2024, day=11)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 183435
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == 218279375708592
puzzle.answer_b = ans2