87 lines
2.0 KiB
Python
87 lines
2.0 KiB
Python
#!/bin/python3
|
|
import sys,time,re,os,copy
|
|
from pprint import pprint
|
|
sys.path.insert(0, '../../')
|
|
from fred import *
|
|
|
|
start_time = time.time()
|
|
|
|
if sys.argv[1] == 'test':
|
|
input_f = 'test'
|
|
elif sys.argv[1] == 'input':
|
|
input_f = 'input'
|
|
else:
|
|
print('No argv provided')
|
|
exit()
|
|
|
|
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
|
|
def area(cords1,cords2):
|
|
width = abs(cords1[0]-cords2[0])+1
|
|
height = abs(cords1[1]-cords2[1])+1
|
|
return width*height
|
|
|
|
def part1():
|
|
|
|
score = 0
|
|
lines = loadFile(input_f,',')
|
|
heap = []
|
|
for i in range(len(lines)):
|
|
for j in range(i+1,len(lines)):
|
|
cords1 = (list2int(lines[i]))
|
|
cords2 = (list2int(lines[j]))
|
|
a = area(cords1,cords2)
|
|
heapq.heappush(heap, (a,cords1,cords2)) #only needed for part 2
|
|
if a > score:
|
|
score = a
|
|
|
|
return score,heap # heap only for part 2
|
|
|
|
start_time = time.time()
|
|
p1,heap = part1()
|
|
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
|
|
def part2(heap):
|
|
|
|
from shapely.geometry import Polygon, box
|
|
|
|
lines = loadFile(input_f,',')
|
|
|
|
for l in range(len(lines)):
|
|
lines[l] = tuple(list2int(lines[l]))
|
|
|
|
shape = Polygon(lines)
|
|
|
|
|
|
heapq._heapify_max(heap)
|
|
|
|
score = 0
|
|
|
|
while heap:
|
|
area,cords1,cords2 = heapq._heappop_max(heap)
|
|
x1,y1 = cords1
|
|
x2,y2 = cords2
|
|
rect = box(min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
|
|
|
|
if shape.contains(rect):
|
|
score = area
|
|
break
|
|
|
|
return score
|
|
|
|
start_time = time.time()
|
|
p2 = part2(heap)
|
|
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
|
|
|