50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
|
#!/bin/python3
|
||
|
import sys,re
|
||
|
from collections import Counter
|
||
|
from pprint import pprint
|
||
|
sys.path.insert(0, '../../')
|
||
|
from fred import list2int,get_re,nprint,lprint
|
||
|
|
||
|
input_f = 'input'
|
||
|
|
||
|
part = 2
|
||
|
|
||
|
def loadList(input_f):
|
||
|
left = []
|
||
|
right = []
|
||
|
|
||
|
with open(input_f) as file:
|
||
|
for line in file:
|
||
|
l = line.rstrip().split()
|
||
|
left.append(int(l[0]))
|
||
|
right.append(int(l[1]))
|
||
|
return left,right
|
||
|
|
||
|
|
||
|
#########################################
|
||
|
# #
|
||
|
# Part 1 #
|
||
|
# #
|
||
|
#########################################
|
||
|
if part == 1:
|
||
|
sum = 0
|
||
|
left,right = loadList(input_f)
|
||
|
for i in range(len(left)):
|
||
|
sum += abs((left.pop(left.index(min(left)))-right.pop(right.index(min(right)))))
|
||
|
print(sum)
|
||
|
|
||
|
|
||
|
#########################################
|
||
|
# #
|
||
|
# Part 2 #
|
||
|
# #
|
||
|
#########################################
|
||
|
if part == 2:
|
||
|
sim = 0
|
||
|
left,right = loadList(input_f)
|
||
|
right = Counter(right)
|
||
|
for i in range(len(left)):
|
||
|
sim += (left[i] * right[left[i]])
|
||
|
print(sim)
|
||
|
|