from collections import Counter with open('input.txt', 'r') as f: lines = [tuple(map(int, line.strip().split())) for line in f.readlines()] list1 = [line[0] for line in lines] list2 = [line[1] for line in lines] list1.sort() list2.sort() differences = [abs(a-b) for a, b in zip(list1, list2)] print(f'Part 1: {sum(differences)}') counts = Counter(list2) scores = [x * counts.get(x, 0) for x in list1] print(f'Part 2: {sum(scores)}')