@@ -18,16 +18,64 @@ def expand_universe(uni):
18
18
for i in expand_row_idxs :
19
19
uni = np .insert (uni , i , "." , axis = 0 )
20
20
21
- return uni
21
+ return uni , expand_col_idxs , expand_row_idxs
22
22
23
- def min_dist (uni , source , )
23
+ def min_dist (uni , source , dest ):
24
+ source_idx = np .where (uni == source )
25
+ dest_idx = np .where (uni == dest )
26
+
27
+ row_range = [source_idx [0 ], dest_idx [0 ]]
28
+ col_range = [source_idx [1 ], dest_idx [1 ]]
29
+
30
+ row_range .sort ()
31
+ col_range .sort ()
32
+
33
+ dist = abs (source_idx [0 ]- dest_idx [0 ])+ abs (source_idx [1 ]- dest_idx [1 ])
34
+ #print(source)
35
+ return int (dist ), row_range , col_range
36
+
24
37
25
38
def main ():
26
- with open ("inputs/11_test .txt" , "r" ) as file :
39
+ with open ("inputs/11 .txt" , "r" ) as file :
27
40
lines = file .readlines ()
28
- universe = np .array ([[e for e in line .strip ("\n " )] for line in lines ])
41
+ universe = np .array ([[e for e in line .strip ("\n " )] for line in lines ], dtype = 'U25' )
42
+ star_count = 1
43
+ for row in range (universe .shape [0 ]):
44
+ for col in range (universe .shape [1 ]):
45
+
46
+ if universe [row , col ] == "#" :
47
+ universe [row ,col ] = str (star_count )
48
+ star_count += 1
49
+
50
+
51
+ expanded_universe , expanded_cols , expanded_rows = expand_universe (universe )
52
+ print (universe )
53
+ dists = []
54
+ # Part 1
55
+ # for i in range(star_count):
56
+ # for j in range(i+1,star_count):
57
+ # dist, _, _ = min_dist(expanded_universe, f"{i+1}", f"{j}")
58
+ # dists.append(dist)
59
+ # print(sum(dists))
60
+
61
+ # Part 2
62
+ dists2 = []
63
+ for i in range (star_count ):
64
+ for j in range (i + 1 ,star_count ):
65
+ dist , row_range , col_range = min_dist (universe , f"{ i + 1 } " , f"{ j } " )
66
+
67
+ for m in expanded_cols :
68
+ if m < col_range [1 ] and m > col_range [0 ]:
69
+ dist += 999999
70
+ for m in expanded_rows :
71
+ if m < row_range [1 ] and m > row_range [0 ]:
72
+ dist += 999999
73
+ dists2 .append (dist )
74
+
75
+ print (sum (dists2 ))
76
+
77
+ #print(min_dist(universe, "5", "9"))
29
78
30
- universe = expand_universe (universe )
31
79
32
80
33
81
if __name__ == "__main__" :
0 commit comments