blob: ca97d3ea7a853545c74ee5c0fd4e8846a1220993 (
plain) (
tree)
|
|
#!/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)
|