Skip to content

Commit

Permalink
2024 day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
crerwin committed Dec 11, 2024
1 parent 236f216 commit e6b38d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion advent/advent2024/day1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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
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
5 changes: 5 additions & 0 deletions tests/test_2024day1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Expand Down

0 comments on commit e6b38d5

Please sign in to comment.