Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024 #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions advent/advent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
advent2021,
advent2022,
advent2023,
advent2024
)

logger = logging.getLogger("advent")
Expand All @@ -31,6 +32,7 @@ def __init__(self):
self.days["2021"] = advent2021.days
self.days["2022"] = advent2022.days
self.days["2023"] = advent2023.days
self.days["2024"] = advent2024.days

def run_day(self, year, day, part):
yr = self.days[str(year)]
Expand Down
3 changes: 3 additions & 0 deletions advent/advent2024/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .day1 import Day1

days = {1: Day1}
49 changes: 49 additions & 0 deletions advent/advent2024/day1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from advent.day import Day

class Day1(Day):
year = 2024
day = 1

list_a = []
list_b = []

def _part1(self):
self.load_lists_from_input(self.input())
self.list_a.sort()
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 = []

def load_lists_from_input(self, input):
for line in input.splitlines():
line_split = line.split()
self.list_a.append(int(line_split[0]))
self.list_b.append(int(line_split[1]))

def get_distance(self, a: int, b: int) -> int:
return abs(a - b)

def get_total_distance_between_lists(self, list_a: list[int], list_b: list[int]) -> int:
if len(list_a) != len(list_b):
raise ValueError("lists were not equal in length")

else:
total_distance = 0
for i in range(0, len(list_a)):
total_distance += self.get_distance(list_a[i], list_b[i])

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
Loading
Loading