···11+import fileinput
22+from collections import Counter
33+44+from utils import Point, DIRS_4, parse_nums
55+66+77+COORDS = []
88+99+for line in fileinput.input():
1010+ x, y = parse_nums(line)
1111+ COORDS.append((x, y))
1212+1313+X, Y = zip(*COORDS)
1414+1515+ASSIGNS = {}
1616+REGIONS = Counter()
1717+SAFETY = Counter()
1818+1919+for y in range(min(Y), max(Y)):
2020+ for x in range(min(X), max(X)):
2121+ p = Point(x, y)
2222+ total_distance = 0
2323+ to_point = {}
2424+2525+ for (xx, yy) in COORDS:
2626+ q = Point(xx, yy)
2727+ dist = p.to_manhattan(q)
2828+ to_point[q] = dist
2929+ total_distance += dist
3030+3131+ SAFETY[p] = total_distance
3232+3333+ dists = list(sorted(to_point.items(), key=lambda x: x[1]))
3434+ if dists[0][1] < dists[1][1]:
3535+ REGIONS[dists[0][0]] += 1
3636+ ASSIGNS[p] = dists[0][0]
3737+ else:
3838+ ASSIGNS[p] = Point(-1, -1)
3939+4040+for p, n in REGIONS.most_common():
4141+ for d in DIRS_4:
4242+ q = p + d
4343+ while q in ASSIGNS:
4444+ if ASSIGNS[q] != p:
4545+ # Not infinite
4646+ break
4747+4848+ q += d
4949+ else:
5050+ # Confirmed infinite
5151+ break
5252+5353+ else:
5454+ # This is the largest non-infinite region
5555+ print "Size of Part 1 region:", n
5656+ break
5757+5858+5959+print "Size of Part 2 region:", sum(n < 10000 for n in SAFETY.values())