149 lines
3.8 KiB
Python
149 lines
3.8 KiB
Python
#!/bin/python3
|
|
import sys,time,re,os,copy
|
|
from pprint import pprint
|
|
sys.path.insert(0, '../../')
|
|
from fred import *
|
|
|
|
import heapq,math
|
|
|
|
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 part1():
|
|
score = 1
|
|
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])
|
|
dist = math.dist(cords1, cords2)
|
|
heapq.heappush(heap, (dist, tuple(cords1), tuple(cords2)))
|
|
|
|
if sys.argv[1] == 'test':
|
|
limit = 10
|
|
else:
|
|
limit = 1000
|
|
|
|
junctions = []
|
|
|
|
while limit > 0:
|
|
|
|
box = heapq.heappop(heap)
|
|
left = tuple(box[1])
|
|
right = tuple(box[2])
|
|
|
|
left_junction = None
|
|
right_junction = None
|
|
|
|
for j in junctions:
|
|
if left in j:
|
|
left_junction = j
|
|
if right in j:
|
|
right_junction = j
|
|
|
|
if left_junction != right_junction and (left_junction != None and right_junction != None):
|
|
left_junction += right_junction
|
|
junctions.remove(right_junction) #forgot to remove the right junction
|
|
|
|
if left_junction is not None and right_junction is None:
|
|
left_junction.append(right)
|
|
|
|
if left_junction is None and right_junction is not None:
|
|
right_junction.append(left)
|
|
|
|
if left_junction is None and right_junction is None:
|
|
junctions.append([left,right])
|
|
|
|
limit -= 1
|
|
|
|
out = sorted(junctions, key=lambda item: len(item), reverse=True)
|
|
|
|
for i in range(0,3):
|
|
score *= len(out[i])
|
|
|
|
return score
|
|
|
|
start_time = time.time()
|
|
p1 = part1()
|
|
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
|
|
def part2():
|
|
score = 1
|
|
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])
|
|
dist = math.dist(cords1, cords2)
|
|
heapq.heappush(heap, (dist, tuple(cords1), tuple(cords2)))
|
|
|
|
if sys.argv[1] == 'test':
|
|
limit = 10
|
|
else:
|
|
limit = 1000
|
|
|
|
junctions = []
|
|
|
|
while limit > 0:
|
|
|
|
box = heapq.heappop(heap)
|
|
|
|
left = tuple(box[1])
|
|
right = tuple(box[2])
|
|
|
|
left_junction = None
|
|
right_junction = None
|
|
|
|
for j in junctions:
|
|
if left in j:
|
|
left_junction = j
|
|
if right in j:
|
|
right_junction = j
|
|
|
|
if left_junction != right_junction and (left_junction != None and right_junction != None):
|
|
left_junction += right_junction
|
|
junctions.remove(right_junction) #forgot to remove the right junction
|
|
|
|
if left_junction is not None and right_junction is None:
|
|
left_junction.append(right)
|
|
|
|
if left_junction is None and right_junction is not None:
|
|
right_junction.append(left)
|
|
|
|
if left_junction is None and right_junction is None:
|
|
junctions.append([left,right])
|
|
|
|
if len(junctions[0]) == len(lines):
|
|
break
|
|
|
|
return left[0]*right[0]
|
|
|
|
start_time = time.time()
|
|
p2 = part2()
|
|
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
|
|
|