From e6b38d54e5cff9c47262a80321f92a9b192930c0 Mon Sep 17 00:00:00 2001 From: Chris Erwin Date: Tue, 10 Dec 2024 22:01:54 -0500 Subject: [PATCH] 2024 day 1 part 2 --- advent/advent2024/day1.py | 13 ++++++++++++- tests/test_2024day1.py | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/advent/advent2024/day1.py b/advent/advent2024/day1.py index 809b1c4..3e31018 100644 --- a/advent/advent2024/day1.py +++ b/advent/advent2024/day1.py @@ -13,6 +13,10 @@ def _part1(self): self.list_b.sort() return self.get_total_distance_between_lists(self.list_a, self.list_b) + def _part2(self): + self.load_lists_from_input(self.input()) + return self.get_similarity_score(self.list_a, self.list_b) + def reset_lists(self): self.list_a = [] self.list_b = [] @@ -35,4 +39,11 @@ def get_total_distance_between_lists(self, list_a: list[int], list_b: list[int]) for i in range(0, len(list_a)): total_distance += self.get_distance(list_a[i], list_b[i]) - return total_distance \ No newline at end of file + return total_distance + + def get_similarity_score(self, list_a: list[int], list_b: list[int]) -> int: + similarity_score = 0 + for number in list_a: + similarity_score += number * list_b.count(number) + + return similarity_score \ No newline at end of file diff --git a/tests/test_2024day1.py b/tests/test_2024day1.py index d02e642..f9121c1 100644 --- a/tests/test_2024day1.py +++ b/tests/test_2024day1.py @@ -26,6 +26,11 @@ def test_get_total_distance_between_lists(self): test_list_b.sort() self.assertEqual(self.test_day.get_total_distance_between_lists(test_list_a, test_list_b), 11) + def test_get_similarity_score(self): + test_list_a = [3, 4, 2, 1, 3, 3] + test_list_b = [4, 3, 5, 3, 9, 3] + self.assertEqual(self.test_day.get_similarity_score(test_list_a, test_list_b), 31) + def test_load_lists_from_input(self): self.test_day.reset_lists() self.assertEqual(self.test_day.list_a, [])