AdventofCode2024/Day1/solution2.py

54 lines
1.5 KiB
Python
Raw Permalink Normal View History

2024-12-08 22:04:13 +01:00
#!/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.difference_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 calculate_occurences(self) -> None:
for element in self.left_array:
tmp: int = 0
for i in range(len(self.right_array)):
if element == self.right_array[i]:
tmp += 1
self.difference_array.append(element * tmp)
def calculate_total_similary(self) -> int:
for i in range(len(self.difference_array)):
self.result += self.difference_array[i]
return self.result
def main() -> None:
solver: Solution = Solution()
solver.read()
solver.clean()
solver.split_lines()
solver.calculate_occurences()
solution: int = solver.calculate_total_similary()
print(solution)
if __name__ == "__main__":
main()