Skip to content

Commit 7504c71

Browse files
committed
Day 11 solved.
Made it harder than necessary for myself, but what better time to screw around with Numpy? Results are surprisingly ugly, though.
1 parent bccdd3c commit 7504c71

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

solutions/solution_11.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import sys
2+
sys.path.append("..")
3+
4+
from aoclib import util
5+
import numpy as np
6+
7+
# Expand the universe, then find the length of the shortest path
8+
# between every pair of galaxies. What is the sum of these lengths?
9+
def solve_11(data):
10+
# Expand the universe...
11+
universe = np.char.array(data)
12+
# ...by inserting empty lines
13+
inserted = 0
14+
for i, galaxycount in enumerate(universe.count('#')):
15+
if galaxycount == 0:
16+
universe = np.insert(universe, i+inserted, '.' * len(universe[i]))
17+
inserted += 1
18+
19+
# ...and columns
20+
universe = np.rot90(universe.view('U1').reshape(universe.size,-1))
21+
inserted = 0
22+
for i, line in enumerate(universe):
23+
chr,counts = np.unique(line, return_counts=True)
24+
counts = dict(zip(chr, counts))
25+
if not '#' in counts:
26+
# The axis thing because we now have a 2D array of char...
27+
universe = np.insert(universe,i+inserted, line, axis=0)
28+
inserted += 1
29+
30+
tmp = np.where(universe == '#')
31+
positions = list(zip(tmp[0],tmp[1]))
32+
33+
distances = np.zeros((len(positions),len(positions)),dtype=np.int32)
34+
# quadratic time, but is there any other way?
35+
for i in range(0,len(positions)):
36+
for j in range(i+1,len(positions)):
37+
# Manhattan distance
38+
distances[i][j] = abs(positions[i][0] - positions[j][0]) + abs(positions[i][1] - positions[j][1])
39+
40+
41+
print(positions)
42+
print(distances)
43+
44+
print(universe)
45+
print(universe.shape)
46+
return sum(sum(distances))
47+
48+
# As the first part, but now expand empty rows/columns by one million...
49+
def solve_11b(data, factor=1000000):
50+
# Expand the universe...
51+
universe = np.char.array(data)
52+
# ...by inserting empty lines
53+
inserted = 0
54+
for i, galaxycount in enumerate(universe.count('#')):
55+
if galaxycount == 0:
56+
universe = np.insert(universe, i+inserted, '.' * len(universe[i]))
57+
inserted += 1
58+
59+
# ...and columns
60+
universe = np.rot90(universe.view('U1').reshape(universe.size,-1))
61+
inserted = 0
62+
for i, line in enumerate(universe):
63+
chr,counts = np.unique(line, return_counts=True)
64+
counts = dict(zip(chr, counts))
65+
if not '#' in counts:
66+
# The axis thing because we now have a 2D array of char...
67+
universe = np.insert(universe,i+inserted, line, axis=0)
68+
inserted += 1
69+
70+
tmp = np.where(universe == '#')
71+
positions = list(zip(tmp[0],tmp[1]))
72+
73+
distances = np.zeros((len(positions),len(positions)),dtype=np.int32)
74+
# quadratic time, but is there any other way?
75+
for i in range(0,len(positions)):
76+
for j in range(i+1,len(positions)):
77+
# Manhattan distance
78+
distances[i][j] = abs(positions[i][0] - positions[j][0]) + abs(positions[i][1] - positions[j][1])
79+
80+
81+
print(positions)
82+
print(distances)
83+
84+
print(universe)
85+
print(universe.shape)
86+
return sum(sum(distances))
87+
88+
89+
if __name__ == '__main__':
90+
data = util.getInput("../inputs/11.txt")
91+
print( solve_11(data) )
92+
print( solve_11b(data) )

tests/test_11.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
sys.path.append("..")
3+
4+
from solutions import solution_11
5+
6+
data = ["...#......",
7+
".......#..",
8+
"#.........",
9+
"..........",
10+
"......#...",
11+
".#........",
12+
".........#",
13+
"..........",
14+
".......#..",
15+
"#...#....."]
16+
17+
18+
def test_11():
19+
assert solution_11.solve_11(data) == 374
20+
21+
def test_11b():
22+
assert solution_11.solve_11b(data, 10) == 1030
23+
assert solution_11.solve_11b(data, 100) == 8410
24+

0 commit comments

Comments
 (0)