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]