2024-12-02 07:11:14 +00:00
|
|
|
from typing import List
|
2024-12-02 07:01:41 +00:00
|
|
|
|
|
|
|
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:
|
2024-12-03 07:12:29 +00:00
|
|
|
return f"Part 1 = {self._part1}, Part 2 = {self._part2}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _part1(self) -> int:
|
|
|
|
outp = 0
|
2024-12-02 07:01:41 +00:00
|
|
|
for val in self.distances():
|
2024-12-03 07:12:29 +00:00
|
|
|
outp += val
|
|
|
|
return outp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _part2(self) -> int:
|
|
|
|
outp = 0
|
2024-12-02 07:01:41 +00:00
|
|
|
for val, occ in zip(self.list1, self.occurances()):
|
2024-12-03 07:12:29 +00:00
|
|
|
outp += int(val) * occ
|
|
|
|
return outp
|
2024-12-02 07:01:41 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
2024-12-03 07:12:29 +00:00
|
|
|
if __name__ == "__main__":
|
2024-12-02 07:01:41 +00:00
|
|
|
example = SplitLists(input=EXAMPLE)
|
2024-12-03 07:12:29 +00:00
|
|
|
print("EXAMPLE:\n", example)
|
|
|
|
assert example._part1 == 11
|
|
|
|
assert example._part2 == 31
|
2024-12-02 07:01:41 +00:00
|
|
|
with open("input.txt", "r") as f:
|
2024-12-03 07:12:29 +00:00
|
|
|
print("Puzzle:\n", SplitLists(input=f.read()))
|