From 4bdbcdbb56f5e9099fae3b81199f7982ecc5d0e8 Mon Sep 17 00:00:00 2001 From: stanislas Date: Mon, 2 Dec 2024 11:14:30 +0100 Subject: [PATCH] day 1 - part 1 --- 2024/day1/first-input | 3 +-- 2024/day1/first.py | 30 +++++++++++++++++++++++++++--- tests/2024/day1/first-data | 6 ++++++ tests/2024/day1/test_first.py | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tests/2024/day1/first-data create mode 100644 tests/2024/day1/test_first.py diff --git a/2024/day1/first-input b/2024/day1/first-input index 31b4e7a..add1159 100644 --- a/2024/day1/first-input +++ b/2024/day1/first-input @@ -997,5 +997,4 @@ 54344 21435 17846 56504 61807 42097 -93272 97487 - +93272 97487 \ No newline at end of file diff --git a/2024/day1/first.py b/2024/day1/first.py index 9369e2e..ecf9a8e 100644 --- a/2024/day1/first.py +++ b/2024/day1/first.py @@ -1,5 +1,29 @@ -def main(): - pass +from pathlib import Path -if __name__ == '__main__': + +def get_ordered_lists(data_file: Path) -> tuple[list[int], list[int]]: + list1 = list() + list2 = list() + for line in data_file.open(): + numbers = line.strip().split(" ") + list1.append(int(numbers[0])) + list2.append(int(numbers[1])) + return sorted(list1), sorted(list2) + + +def compute_distance(list1, list2) -> int: + return sum([abs(item2 - item1) for item1, item2 in zip(list1, list2)]) + + +def compute(data_file: Path) -> int: + list1, list2 = get_ordered_lists(data_file) + return compute_distance(list1, list2) + + +def main(): + file = Path(__file__).parent / "first-input" + print(compute(file)) + + +if __name__ == "__main__": main() diff --git a/tests/2024/day1/first-data b/tests/2024/day1/first-data new file mode 100644 index 0000000..dfca0b1 --- /dev/null +++ b/tests/2024/day1/first-data @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 \ No newline at end of file diff --git a/tests/2024/day1/test_first.py b/tests/2024/day1/test_first.py new file mode 100644 index 0000000..7f13fd8 --- /dev/null +++ b/tests/2024/day1/test_first.py @@ -0,0 +1,14 @@ +from pathlib import Path + +from day1 import first + +TEST_FILE = Path(__file__).parent / "first-data" + + +def test_first(): + assert first.compute(TEST_FILE) == 11 + + +def test_get_lists(): + assert first.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4] + assert first.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9]