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
64
65
66
67
68
69
70
71
|
#!/usr/bin/python3
from sys import argv
if len(argv) != 2:
print(argv[0], "1 # za part one")
print(argv[0], "999999 # za part two")
exit()
def find_all(haystack, needle):
occur = -1
r = []
while True:
occur = haystack.find(needle, occur+1)
if occur == -1:
return r
r.append(occur)
galaxies = {}
horizontal_holes = []
columns = set([])
linelen = 0
line = 0
try:
while True:
s = input()
linelen = len(s)
cols = find_all(s, "#");
for col in cols:
galaxies[(line, col)] = True
columns |= set([col])
if len(cols) == 0:
horizontal_holes.append(line)
line += 1
except EOFError:
pass
lines = line+1
vertical_holes = []
for i in range(linelen):
if i not in columns:
vertical_holes.append(i)
horizontal_holes_sums = []
hhs = 0
for i in range(lines):
if i in horizontal_holes:
hhs += int(argv[1])
horizontal_holes_sums.append(hhs)
vertical_holes_sums = []
vhs = 0
for i in range(linelen):
if i in vertical_holes:
vhs += int(argv[1])
vertical_holes_sums.append(vhs)
corr_galaxies = {}
for galaxy in galaxies:
corr_galaxies[(galaxy[0]+horizontal_holes_sums[galaxy[0]], galaxy[1]+vertical_holes_sums[galaxy[1]])] = True
def viscorr():
for line in range(lines+hhs):
for column in range(linelen+vhs):
if (line, column) in corr_galaxies:
print("#", end="")
else:
print(".", end="")
print()
from itertools import product
pari = {}
for p in product(corr_galaxies, corr_galaxies):
if len(set(p)) == 1:
continue
if (p[0], p[1]) not in pari and (p[1], p[0]) not in pari:
pari[p] = True
s = 0
for par in pari:
s += abs(par[0][0]-par[1][0])+abs(par[0][1]-par[1][1])
print(s)
|