45 lines
1,000 B
Python
45 lines
1,000 B
Python
from typing import List, Dict
|
|
|
|
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:
|
|
part1 = 0
|
|
for val in self.distances():
|
|
part1 += val
|
|
part2 = 0
|
|
for val, occ in zip(self.list1, self.occurances()):
|
|
part2 += int(val) * occ
|
|
return f"Part1: {part1}\nPart2: {part2}"
|
|
|
|
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]
|
|
|
|
|
|
def main(*args, **kwargs) -> None:
|
|
example = SplitLists(input=EXAMPLE)
|
|
print(example)
|
|
with open("input.txt", "r") as f:
|
|
print(SplitLists(input=f.read()))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|