-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2024_11.py
63 lines (45 loc) · 1.39 KB
/
AoC2024_11.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
63
#! /usr/bin/env python3
#
# Advent of Code 2024 Day 11
#
import sys
from functools import cache
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
Input = list[int]
Output1 = int
Output2 = int
TEST = """\
125 17
"""
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return list(map(int, list(input_data)[0].split()))
def solve(self, stones: list[int], blinks: int) -> int:
@cache
def count(s: int, cnt: int) -> int:
if cnt == 0:
return 1
if s == 0:
return count(1, cnt - 1)
ss = str(s)
size = len(ss)
if size % 2 == 0:
s1 = int(ss[: size // 2])
s2 = int(ss[size // 2 :]) # noqa E203
return count(s1, cnt - 1) + count(s2, cnt - 1)
return count(s * 2024, cnt - 1)
return sum(count(int(s), blinks) for s in stones)
def part_1(self, stones: Input) -> Output1:
return self.solve(stones, blinks=25)
def part_2(self, stones: Input) -> Output2:
return self.solve(stones, blinks=75)
@aoc_samples((("part_1", TEST, 55312),))
def samples(self) -> None:
pass
solution = Solution(2024, 11)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()