AdventofCode2024/Day1/solution.py
Emma Nora Theuer 7a0e3ab9ab Initial Commit
2024-12-08 21:44:52 +01:00

47 lines
1.3 KiB
Python

#!/usr/bin/env python3
from typing import List
class Solution:
def __init__(self) -> None:
self.lines: List[str] = []
self.left_array: List[int] = []
self.right_array: List[int] = []
self.result: int = 0
def read(self) -> None:
with open("input") as f:
self.lines = f.readlines()
def clean(self) -> None:
for i in range(len(self.lines)):
self.lines[i] = self.lines[i].strip()
def split_lines(self) -> None:
for line in self.lines:
elements: List[str] = line.split()
clean_element: int = int(elements[0])
self.left_array.append(clean_element)
clean_element: int = int(elements[1])
self.right_array.append(clean_element)
def sort_lists(self) -> None:
self.left_array.sort()
self.right_array.sort()
def calculate_difference(self) -> None:
for i in range(len(self.left_array)):
difference: int = abs(self.right_array[i] - self.left_array[i])
self.result += difference
def main() -> None:
solver: Solution = Solution()
solver.read()
solver.clean()
solver.split_lines()
solver.sort_lists()
solver.calculate_difference()
print(solver.result)
if __name__ == "__main__":
main()