From b844fafd7824b9c600a0608eb539fac352865294 Mon Sep 17 00:00:00 2001 From: stanislas Date: Mon, 2 Dec 2024 11:16:39 +0100 Subject: [PATCH 1/3] day 1 - part 1 - refactoring --- 2024/day1/{first-input => input-data} | 0 2024/day1/part1.py | 8 ++------ tests/2024/day1/test_first.py | 14 -------------- tests/2024/day1/test_part1.py | 14 ++++++++++++++ 4 files changed, 16 insertions(+), 20 deletions(-) rename 2024/day1/{first-input => input-data} (100%) create mode 100644 tests/2024/day1/test_part1.py diff --git a/2024/day1/first-input b/2024/day1/input-data similarity index 100% rename from 2024/day1/first-input rename to 2024/day1/input-data diff --git a/2024/day1/part1.py b/2024/day1/part1.py index 839e4ed..2a7dd99 100644 --- a/2024/day1/part1.py +++ b/2024/day1/part1.py @@ -15,14 +15,10 @@ 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 / "input-data" - print(compute(file)) + list1, list2 = get_ordered_lists(file) + print(compute_distance(list1, list2)) if __name__ == "__main__": diff --git a/tests/2024/day1/test_first.py b/tests/2024/day1/test_first.py index 90c0408..e69de29 100644 --- a/tests/2024/day1/test_first.py +++ b/tests/2024/day1/test_first.py @@ -1,14 +0,0 @@ -from pathlib import Path - -from day1 import part1 - -TEST_FILE = Path(__file__).parent / "test-data" - - -def test_first(): - assert part1.compute(TEST_FILE) == 11 - - -def test_get_lists(): - assert part1.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4] - assert part1.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9] diff --git a/tests/2024/day1/test_part1.py b/tests/2024/day1/test_part1.py new file mode 100644 index 0000000..90c0408 --- /dev/null +++ b/tests/2024/day1/test_part1.py @@ -0,0 +1,14 @@ +from pathlib import Path + +from day1 import part1 + +TEST_FILE = Path(__file__).parent / "test-data" + + +def test_first(): + assert part1.compute(TEST_FILE) == 11 + + +def test_get_lists(): + assert part1.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4] + assert part1.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9] From 5f492b3f6635e096669d6b359472cf1c82c257f2 Mon Sep 17 00:00:00 2001 From: stanislas Date: Mon, 2 Dec 2024 16:16:06 +0100 Subject: [PATCH 2/3] day 1 - part 2 --- 2024/day1/common.py | 16 ++++++++++++++ 2024/day1/part1.py | 22 +++++++------------- 2024/day1/part2.py | 38 ++++++++++++++++++++++++++++++++++ tests/2024/day1/test_common.py | 15 ++++++++++++++ tests/2024/day1/test_part1.py | 16 +++++++------- tests/2024/day1/test_part2.py | 14 +++++++++++++ 6 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 2024/day1/common.py create mode 100644 2024/day1/part2.py create mode 100644 tests/2024/day1/test_common.py create mode 100644 tests/2024/day1/test_part2.py diff --git a/2024/day1/common.py b/2024/day1/common.py new file mode 100644 index 0000000..eb62d14 --- /dev/null +++ b/2024/day1/common.py @@ -0,0 +1,16 @@ +from pathlib import Path + + +def get_lists(data_file: Path) -> tuple[list[int], list[int]]: + list1 = [] + list2 = [] + for line in data_file.open(): + numbers = line.strip().split(" ") + list1.append(int(numbers[0])) + list2.append(int(numbers[1])) + return list1, list2 + + +def get_ordered_lists(data_file: Path) -> tuple[list[int], list[int]]: + list1, list2 = get_lists(data_file) + return sorted(list1), sorted(list2) diff --git a/2024/day1/part1.py b/2024/day1/part1.py index 2a7dd99..26e86f6 100644 --- a/2024/day1/part1.py +++ b/2024/day1/part1.py @@ -1,25 +1,19 @@ from pathlib import Path - -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) +from day1.common import get_ordered_lists def compute_distance(list1, list2) -> int: return sum([abs(item2 - item1) for item1, item2 in zip(list1, list2)]) -def main(): - file = Path(__file__).parent / "input-data" - list1, list2 = get_ordered_lists(file) - print(compute_distance(list1, list2)) +def main(data_file: Path): + list1, list2 = get_ordered_lists(data_file) + distance = compute_distance(list1, list2) + print(distance) + return distance if __name__ == "__main__": - main() + file = Path(__file__).parent / "input-data" + main(file) diff --git a/2024/day1/part2.py b/2024/day1/part2.py new file mode 100644 index 0000000..3184e5e --- /dev/null +++ b/2024/day1/part2.py @@ -0,0 +1,38 @@ +from pathlib import Path + +from day1.common import get_ordered_lists + + +def get_elem_amount(data: list[int]) -> dict[int, int]: + data_set = set(data) + amount = {} + for element in data_set: + nb_elem = data.count(element) + data.remove(element) + amount[element] = nb_elem + return amount + + +def compute_similarity_score( + elem_amount1: dict[int, int], elem_amount2: dict[int, int] +) -> int: + score = 0 + for k1, v1 in elem_amount1.items(): + if k1 in elem_amount2.keys(): + score += k1 * v1 * elem_amount2[k1] + return score + + +def main(data_file: Path): + list1, list2 = get_ordered_lists(data_file) + elem_amount1 = get_elem_amount(list1) + elem_amount2 = get_elem_amount(list2) + + similarity_score = compute_similarity_score(elem_amount1, elem_amount2) + print(similarity_score) + return similarity_score + + +if __name__ == "__main__": + file = Path(__file__).parent / "input-data" + main(file) diff --git a/tests/2024/day1/test_common.py b/tests/2024/day1/test_common.py new file mode 100644 index 0000000..0d85d7f --- /dev/null +++ b/tests/2024/day1/test_common.py @@ -0,0 +1,15 @@ +from pathlib import Path + +from day1 import common + +TEST_FILE = Path(__file__).parent / "test-data" + + +def test_get_lists(): + assert common.get_lists(TEST_FILE)[0] == [3, 4, 2, 1, 3, 3] + assert common.get_lists(TEST_FILE)[1] == [4, 3, 5, 3, 9, 3] + + +def test_get_sorted_lists(): + assert common.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4] + assert common.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9] diff --git a/tests/2024/day1/test_part1.py b/tests/2024/day1/test_part1.py index 90c0408..183ce32 100644 --- a/tests/2024/day1/test_part1.py +++ b/tests/2024/day1/test_part1.py @@ -2,13 +2,13 @@ from pathlib import Path from day1 import part1 -TEST_FILE = Path(__file__).parent / "test-data" + +def test_compute_distance(): + list1 = [1, 2, 3, 3, 3, 4] + list2 = [3, 3, 3, 4, 5, 9] + assert part1.compute_distance(list1, list2) == 11 -def test_first(): - assert part1.compute(TEST_FILE) == 11 - - -def test_get_lists(): - assert part1.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4] - assert part1.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9] +def test_main(): + test_file = Path(__file__).parent / "test-data" + assert part1.main(test_file) == 11 diff --git a/tests/2024/day1/test_part2.py b/tests/2024/day1/test_part2.py new file mode 100644 index 0000000..c62e007 --- /dev/null +++ b/tests/2024/day1/test_part2.py @@ -0,0 +1,14 @@ +from pathlib import Path + +from day1 import part2 + + +def test_compute_similarity_score(): + dict1 = {1: 1, 2: 1, 3: 3, 4: 1} + dict2 = {3: 3, 4: 1, 5: 1, 9: 1} + assert part2.compute_similarity_score(dict1, dict2) == 31 + + +def test_main(): + test_file = Path(__file__).parent / "test-data" + assert part2.main(test_file) == 31 From eac54767b570e1577887cd95fa1127491061bf09 Mon Sep 17 00:00:00 2001 From: stanislas Date: Mon, 2 Dec 2024 16:21:56 +0100 Subject: [PATCH 3/3] day 1 - part 2 - refactoring --- 2024/pyproject.toml => pyproject.toml | 0 {2024 => src/aoc_2024}/day1/common.py | 0 {2024 => src/aoc_2024}/day1/input-data | 0 {2024 => src/aoc_2024}/day1/part1.py | 2 +- {2024 => src/aoc_2024}/day1/part2.py | 2 +- tests/{2024 => aoc_2024}/day1/test-data | 0 tests/{2024 => aoc_2024}/day1/test_common.py | 2 +- tests/{2024 => aoc_2024}/day1/test_first.py | 0 tests/{2024 => aoc_2024}/day1/test_part1.py | 2 +- tests/{2024 => aoc_2024}/day1/test_part2.py | 2 +- 10 files changed, 5 insertions(+), 5 deletions(-) rename 2024/pyproject.toml => pyproject.toml (100%) rename {2024 => src/aoc_2024}/day1/common.py (100%) rename {2024 => src/aoc_2024}/day1/input-data (100%) rename {2024 => src/aoc_2024}/day1/part1.py (88%) rename {2024 => src/aoc_2024}/day1/part2.py (94%) rename tests/{2024 => aoc_2024}/day1/test-data (100%) rename tests/{2024 => aoc_2024}/day1/test_common.py (92%) rename tests/{2024 => aoc_2024}/day1/test_first.py (100%) rename tests/{2024 => aoc_2024}/day1/test_part1.py (89%) rename tests/{2024 => aoc_2024}/day1/test_part2.py (90%) diff --git a/2024/pyproject.toml b/pyproject.toml similarity index 100% rename from 2024/pyproject.toml rename to pyproject.toml diff --git a/2024/day1/common.py b/src/aoc_2024/day1/common.py similarity index 100% rename from 2024/day1/common.py rename to src/aoc_2024/day1/common.py diff --git a/2024/day1/input-data b/src/aoc_2024/day1/input-data similarity index 100% rename from 2024/day1/input-data rename to src/aoc_2024/day1/input-data diff --git a/2024/day1/part1.py b/src/aoc_2024/day1/part1.py similarity index 88% rename from 2024/day1/part1.py rename to src/aoc_2024/day1/part1.py index 26e86f6..9d0653e 100644 --- a/2024/day1/part1.py +++ b/src/aoc_2024/day1/part1.py @@ -1,6 +1,6 @@ from pathlib import Path -from day1.common import get_ordered_lists +from aoc_2024.day1.common import get_ordered_lists def compute_distance(list1, list2) -> int: diff --git a/2024/day1/part2.py b/src/aoc_2024/day1/part2.py similarity index 94% rename from 2024/day1/part2.py rename to src/aoc_2024/day1/part2.py index 3184e5e..693a376 100644 --- a/2024/day1/part2.py +++ b/src/aoc_2024/day1/part2.py @@ -1,6 +1,6 @@ from pathlib import Path -from day1.common import get_ordered_lists +from aoc_2024.day1.common import get_ordered_lists def get_elem_amount(data: list[int]) -> dict[int, int]: diff --git a/tests/2024/day1/test-data b/tests/aoc_2024/day1/test-data similarity index 100% rename from tests/2024/day1/test-data rename to tests/aoc_2024/day1/test-data diff --git a/tests/2024/day1/test_common.py b/tests/aoc_2024/day1/test_common.py similarity index 92% rename from tests/2024/day1/test_common.py rename to tests/aoc_2024/day1/test_common.py index 0d85d7f..a84b559 100644 --- a/tests/2024/day1/test_common.py +++ b/tests/aoc_2024/day1/test_common.py @@ -1,6 +1,6 @@ from pathlib import Path -from day1 import common +from aoc_2024.day1 import common TEST_FILE = Path(__file__).parent / "test-data" diff --git a/tests/2024/day1/test_first.py b/tests/aoc_2024/day1/test_first.py similarity index 100% rename from tests/2024/day1/test_first.py rename to tests/aoc_2024/day1/test_first.py diff --git a/tests/2024/day1/test_part1.py b/tests/aoc_2024/day1/test_part1.py similarity index 89% rename from tests/2024/day1/test_part1.py rename to tests/aoc_2024/day1/test_part1.py index 183ce32..4dcf957 100644 --- a/tests/2024/day1/test_part1.py +++ b/tests/aoc_2024/day1/test_part1.py @@ -1,6 +1,6 @@ from pathlib import Path -from day1 import part1 +from aoc_2024.day1 import part1 def test_compute_distance(): diff --git a/tests/2024/day1/test_part2.py b/tests/aoc_2024/day1/test_part2.py similarity index 90% rename from tests/2024/day1/test_part2.py rename to tests/aoc_2024/day1/test_part2.py index c62e007..1278dff 100644 --- a/tests/2024/day1/test_part2.py +++ b/tests/aoc_2024/day1/test_part2.py @@ -1,6 +1,6 @@ from pathlib import Path -from day1 import part2 +from aoc_2024.day1 import part2 def test_compute_similarity_score():