1+ import  re 
2+ from  itertools  import  permutations 
3+ 
4+ with  open ("input" ) as  f :
5+     inp  =  f .read ().strip ().split ("\n " )
6+ 
7+ nodes  =  []
8+ for  line  in  inp :
9+     if  line [0 ] !=  "/" :
10+         continue 
11+     nodes .append (tuple (map (int , re .findall ("\d+" , line ))))
12+ 
13+ # Part 1 
14+ viable_pairs  =  0 
15+ for  nodea , nodeb  in  permutations (nodes , 2 ):
16+     _ , _ , _ , useda , availa , _  =  nodea 
17+     _ , _ , _ , usedb , availb , _  =  nodeb 
18+     if  useda  >  0  and  useda  <=  availb :
19+         viable_pairs  +=  1 
20+ 
21+ print (viable_pairs )
22+ 
23+ # Part 2 
24+ 
25+ # Our grid is 38x38 so the node we have access to is at (0,0) and the node which data we want to access is at (37,0). 
26+ # Data blocks between (12,6) and (12,37) are large (490-498), and they can never fit into smaller sized nodes,  
27+ # so they're stuck. This means we can only move data between smaller sized nodes (85-94). Since the smaller data blocks 
28+ # themselves are between 64-73 these can only be moved into an empty node. We note that all small-sized data blocks can fit 
29+ # into all nodes. 
30+ # Since there is only one empty node, we are essentially swapping data between the empty node and its neighbors. 
31+ # 
32+ # Our grid looks like this: 
33+ # 
34+ # a . . . . . . . . . . . . d 
35+ # . . . . . . . . . . . . . . 
36+ # . . . . . . . . . . . . . . 
37+ # . . . . # # # # # # # # # # 
38+ # . . . . . . . . . . . . . . 
39+ # . . . . . . . . . . . . . . 
40+ # . . . . . . . . . . . 0 . . 
41+ # 
42+ # where  
43+ # - # denotes the large nodes which can't be moved (12,6), ..., (12,37) 
44+ # - d denotes the data we want access to (37,0) 
45+ # - a the node we have access to (0,0) 
46+ # - 0 the empty node (16,23) 
47+ # 
48+ # The goal is to move d to a. This can be achieved in two steps: 
49+ # 1. Move the empty node up next to d, so the grid looks like this: 
50+ # 
51+ # a . . . . . . . . . . . 0 d 
52+ # . . . . . . . . . . . . . . 
53+ # . . . . . . . . . . . . . . 
54+ # . . . . # # # # # # # # # # 
55+ # . . . . . . . . . . . . . . 
56+ # . . . . . . . . . . . . . . 
57+ # . . . . . . . . . . . . . . 
58+ #  
59+ # This can be done via this path: (16,23) -> (5,23) -> (5,0) -> (36,0) which 
60+ # takes (16 - 5) + (23 - 0) + (36 - 5) steps 
61+ #  
62+ # 2. Then we move d to left one step and then we need to move the 0 around d so it 
63+ # again is in front requiring 4 steps. This would look like this: 
64+ # 
65+ # a . . . . . . . . . . . d 0 
66+ # . . . . . . . . . . . . . . 
67+ # . . . . . . . . . . . . . . 
68+ # . . . . # # # # # # # # # # 
69+ # . . . . . . . . . . . . . . 
70+ # . . . . . . . . . . . . . . 
71+ # . . . . . . . . . . . . . . 
72+ #  
73+ # a . . . . . . . . . . 0 d . 
74+ # . . . . . . . . . . . . . . 
75+ # . . . . . . . . . . . . . . 
76+ # . . . . # # # # # # # # # # 
77+ # . . . . . . . . . . . . . . 
78+ # . . . . . . . . . . . . . . 
79+ # . . . . . . . . . . . . . . 
80+ #  
81+ # Each step to the left requires 5 steps and we end up with 0 in front af d. After 36*5 steps 
82+ # d is positioned at (1,0) with 0 at (0,0). After one additional step d has been moved to (0,0). 
83+ print ((16  -  5 ) +  (23  -  0 ) +  (36  -  5 ) +  36 * 5  +  1 )
0 commit comments