Contender for my worst broot in all of AoC. Unwashed aside from removing unused lines
from itertools import combinations
data = open("Day 09 input.txt", "r").read().strip().split("\n")
from time import clock
print clock()
nums = []
for i in data:
nums.append(map(int, i.split(",")))
leftmost = nums[0]
near = set()
border = set()
last = nums[-1]
for num in nums:
if num[0] < leftmost[0]: leftmost = num
for x in range(min(last[0], num[0]), max(last[0], num[0])+1, 1):
for y in range(min(last[1], num[1]), max(last[1], num[1])+1, 1):
for x2 in (-1,0,1):
for y2 in (-1,0,1):
near.add((x+x2,y+y2))
border.add((x,y))
last = num
perim = set()
queue = [(leftmost[0]-1, leftmost[1])]
while len(queue):
pos = queue.pop()
if pos in perim: continue
if pos in border: continue
if not pos in near: continue
x,y = pos
perim.add((x,y))
for x2 in (-1,0,1):
for y2 in (-1,0,1):
pos = (x+x2,y+y2)
queue.append(pos)
dirs = [[1,0],[0,1],[-1,0],[0,-1]]
largest = 0
check = 0
for a, b in combinations(nums, 2):
check += 1
if check%1000 == 0: print check
size = (abs(a[0]-b[0])+1) * (abs(a[1]-b[1])+1)
if size > largest:
edges = [min(a[0], b[0]), max(a[0], b[0]), min(a[1], b[1]), max(a[1], b[1])] #left X, right X, top Y, bottom Y
fail = False
for x in xrange(edges[0], edges[1]+1, 1):
if (x,edges[2]) in perim or (x,edges[3]) in perim:
fail = True
break
if fail: continue
for y in xrange(edges[2], edges[3]+1, 1):
if (edges[0],y) in perim or (edges[1],y) in perim:
fail = True
break
if not fail:
largest = size
print largest
>was worried about this case >>107488702
>start writing a flood fill around perimeter to check for instances of that
>wait, I can just use this against the rectangles as an easy check
>pypy go brrrr
Runs in ~160 seconds with pypy on my computer. A little ashamed coordinate compression didn't even cross my mind since I've used it in previous years.