76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
import fileinput
|
|
import math
|
|
from pprint import pprint
|
|
from itertools import pairwise
|
|
|
|
def main():
|
|
(part1, part2) = (0, 0)
|
|
|
|
# parse
|
|
inp = fileinput.input()
|
|
coords_ = []
|
|
(w, h) = (0, 0)
|
|
for (y, row) in enumerate(inp):
|
|
for (x, c) in enumerate(row):
|
|
if c == "#":
|
|
coords_.append((x, y))
|
|
w = max(w, x)
|
|
h = max(h, y)
|
|
|
|
coords = coords_.copy()
|
|
|
|
(dx, dy) = (0, 0)
|
|
for j in range(h):
|
|
if not any(y_ == j + dy for (_, y_) in coords):
|
|
print(f"inserting row at y={j}")
|
|
for i in range(len(coords)):
|
|
(x_, y_) = coords[i]
|
|
if y_ > j + dy:
|
|
coords[i] = (x_, y_ + 1)
|
|
dy += 1
|
|
if not any(x_ == j + dx for (x_, _) in coords):
|
|
print(f"inserting column at x={j}")
|
|
for i in range(len(coords)):
|
|
(x_, y_) = coords[i]
|
|
if x_ > j + dx:
|
|
coords[i] = (x_ + 1, y_)
|
|
dx += 1
|
|
|
|
part1 = 0
|
|
for ai in range(len(coords)):
|
|
for bi in range(ai + 1, len(coords)):
|
|
(ax, ay) = coords[ai]
|
|
(bx, by) = coords[bi]
|
|
part1 += abs(ax - bx) + abs(ay - by)
|
|
|
|
coords = coords_.copy()
|
|
L = 1_000_000 - 1
|
|
|
|
(dx, dy) = (0, 0)
|
|
for j in range(h):
|
|
if not any(y_ == j + dy for (_, y_) in coords):
|
|
print(f"inserting rows at y={j}")
|
|
for i in range(len(coords)):
|
|
(x_, y_) = coords[i]
|
|
if y_ > j + dy:
|
|
coords[i] = (x_, y_ + L)
|
|
dy += L
|
|
if not any(x_ == j + dx for (x_, _) in coords):
|
|
print(f"inserting columns at x={j}")
|
|
for i in range(len(coords)):
|
|
(x_, y_) = coords[i]
|
|
if x_ > j + dx:
|
|
coords[i] = (x_ + L, y_)
|
|
dx += L
|
|
|
|
part2 = 0
|
|
for ai in range(len(coords)):
|
|
for bi in range(ai + 1, len(coords)):
|
|
(ax, ay) = coords[ai]
|
|
(bx, by) = coords[bi]
|
|
part2 += abs(ax - bx) + abs(ay - by)
|
|
|
|
print(part1, part2)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|