You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
if __name__ == '__main__':
|
|
universe = [list(line.strip('\n')) for line in open(sys.argv[1])]
|
|
row_ex = [not '#' in row for row in universe]
|
|
col_ex = [True] * len(universe[0])
|
|
stars, c = {}, 1
|
|
for j, row in enumerate(universe):
|
|
for i in range(len(row)):
|
|
col_ex[i] = col_ex[i] and row[i] == '.'
|
|
if row[i] == '#':
|
|
stars[c] = (i,j)
|
|
c += 1
|
|
|
|
res1, res2 = 0, 0
|
|
for s in range(1, c-1):
|
|
for s2 in range(s+1, c):
|
|
x0,y0 = stars[s2]
|
|
x1,y1 = stars[s]
|
|
y = y0 if y0 <= y1 else y1
|
|
y1 = y1 if y1 > y0 else y0
|
|
x = x0 if x0 <= x1 else x1
|
|
x1 = x1 if x1 > x0 else x0
|
|
dist = x1-x + y1-y
|
|
res1 += dist + sum(row_ex[y:y1]) + sum(col_ex[x:x1])
|
|
res2 += dist + 999999 * sum(row_ex[y:y1]) + 999999 * sum(col_ex[x:x1])
|
|
|
|
# challenge 1
|
|
res1 = str(res1)
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
res2 = str(res2)
|
|
print(f"challenge 2:\n{res2}\n")
|
|
|
|
|