From 5dde348cb89498006aae4bf956d504d9952cf2a9 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 30 Nov 2024 11:18:02 -0800 Subject: [PATCH 1/2] 2024 --- .gitignore | 2 ++ y_2024/day1.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 y_2024/day1.py diff --git a/.gitignore b/.gitignore index 9339f48..31253a6 100644 --- a/.gitignore +++ b/.gitignore @@ -129,3 +129,5 @@ dmypy.json .pyre/ input_day*.txt y_2022/secret_backup.py +_old_y*/* +gpt.txt diff --git a/y_2024/day1.py b/y_2024/day1.py new file mode 100644 index 0000000..6154f2c --- /dev/null +++ b/y_2024/day1.py @@ -0,0 +1,32 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass + +from common.aoc import AoCDay + + +@dataclass +class Row: + original: str + processed: Optional[str] = None + + def __post_init__(self) -> None: + self.processed = "" # self.original + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + # self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data] + print(f"{self._input_data=}") + self.__input_data = [Row(i) for i in self._input_data[0]] + + def _calculate_1(self): + for x in self.__input_data: + print(f"{x}") + return 0 + + def _calculate_2(self): + return 0 From efb3a428b44a937c1fff723cc365ce11d8ea01fd Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 30 Nov 2024 21:28:12 -0800 Subject: [PATCH 2/2] day1 --- y_2024/day1.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/y_2024/day1.py b/y_2024/day1.py index 6154f2c..7208430 100644 --- a/y_2024/day1.py +++ b/y_2024/day1.py @@ -1,3 +1,4 @@ +from collections import Counter from typing import Optional from pydantic.dataclasses import dataclass @@ -8,10 +9,10 @@ @dataclass class Row: original: str - processed: Optional[str] = None + processed: Optional[list] = None def __post_init__(self) -> None: - self.processed = "" # self.original + self.processed = self.original.split() class Day(AoCDay): @@ -20,13 +21,20 @@ def __init__(self, test=0): def _preprocess_input(self): # self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data] - print(f"{self._input_data=}") - self.__input_data = [Row(i) for i in self._input_data[0]] + # print(f"{self._input_data=}") + parsed_input = [Row(i) for i in self._input_data[0]] + self.__first_list = [int(i.processed[0]) for i in parsed_input] + self.__second_list = [int(i.processed[1]) for i in parsed_input] def _calculate_1(self): - for x in self.__input_data: - print(f"{x}") - return 0 + result = 0 + for x, y in zip(sorted(self.__first_list), sorted(self.__second_list)): + result += abs(y - x) + return result def _calculate_2(self): - return 0 + result = 0 + c = Counter(self.__second_list) + for x in self.__first_list: + result += x * c.get(x, 0) + return result