File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ import functools
2
+ from collections import defaultdict
3
+
4
+ from . import CombinedRunner
5
+
6
+
7
+ @functools .cache
8
+ def blink_num (num : int ) -> tuple [int , ...]:
9
+ if num == 0 :
10
+ return (1 ,)
11
+
12
+ num_str = str (num )
13
+ num_len = len (num_str )
14
+
15
+ if num_len % 2 == 0 :
16
+ half = num_len // 2
17
+ return (int (num_str [:half ]), int (num_str [half :]))
18
+
19
+ return (num * 2024 ,)
20
+
21
+
22
+ def step (nums : dict [int , int ]) -> dict [int , int ]:
23
+ result = defaultdict (int )
24
+
25
+ for num , count in nums .items ():
26
+ for transformed in blink_num (num ):
27
+ result [transformed ] += count
28
+
29
+ return result
30
+
31
+
32
+ class DayRunner (CombinedRunner ):
33
+ @classmethod
34
+ def run_both (cls , input : str ) -> tuple [int , int ]:
35
+ nums = [int (val ) for val in input .strip ().split (" " )]
36
+
37
+ counts = defaultdict (int )
38
+
39
+ for num in nums :
40
+ counts [num ] += 1
41
+
42
+ for _ in range (25 ):
43
+ counts = step (counts )
44
+
45
+ part1 = sum (counts .values ())
46
+
47
+ for _ in range (50 ):
48
+ counts = step (counts )
49
+
50
+ return part1 , sum (counts .values ())
Original file line number Diff line number Diff line change
1
+ from aoc .days .day11 import DayRunner
2
+
3
+ SAMPLE = "125 17"
4
+
5
+
6
+ def test_sample_part1 () -> None :
7
+ assert DayRunner .part1 (SAMPLE ) == 55312
You can’t perform that action at this time.
0 commit comments