from typing import List EXAMPLE = """ 3 4 4 3 2 5 1 3 3 9 3 3 """ class SplitLists: def __init__(self, input: str): self.list1, self.list2 = zip(*[val.split() for val in input.split("\n") if val]) def __str__(self) -> str: return f"Part 1 = {self._part1}, Part 2 = {self._part2}" @property def _part1(self) -> int: outp = 0 for val in self.distances(): outp += val return outp @property def _part2(self) -> int: outp = 0 for val, occ in zip(self.list1, self.occurances()): outp += int(val) * occ return outp def distances(self) -> List[int]: return [ abs(int(val1) - int(val2)) for val1, val2 in zip(sorted(self.list1), sorted(self.list2)) ] def occurances(self) -> List[int]: return [self.list2.count(key) for key in self.list1] if __name__ == "__main__": example = SplitLists(input=EXAMPLE) print("EXAMPLE:\n", example) assert example._part1 == 11 assert example._part2 == 31 with open("input.txt", "r") as f: print("Puzzle:\n", SplitLists(input=f.read()))