From b86fb4eff5257d4f27273d746477b393afa2cb7f Mon Sep 17 00:00:00 2001 From: Emma Nora Theuer Date: Sun, 8 Dec 2024 22:04:13 +0100 Subject: [PATCH] Initial Commit --- Day1/solution2.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Day1/solution2.py diff --git a/Day1/solution2.py b/Day1/solution2.py new file mode 100644 index 0000000..7ff0027 --- /dev/null +++ b/Day1/solution2.py @@ -0,0 +1,53 @@ +#!/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()