Add 2024 day1

This commit is contained in:
Roland Thomas Jr 2024-12-01 11:33:01 -05:00
parent 74a9f60b12
commit bd9480c6a7
Signed by: roland
GPG Key ID: 7C3C2B085A4C2872
3 changed files with 1044 additions and 0 deletions

View File

@ -5,6 +5,7 @@ def main():
lines = file.readlines()
matrix = [list(line.strip()) for line in lines]
print(matrix)
last_column = len(matrix[0]) -1
last_row = len(matrix) -1
print(last_column, last_row)

1000
2024/day1 Normal file

File diff suppressed because it is too large Load Diff

43
2024/day1.py Normal file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python
from collections import Counter
def part_one(left_list: list, right_list: list) -> int:
left_list.sort()
right_list.sort()
diffs = []
for left, right in zip(left_list, right_list):
diffs.append(abs(left - right))
return sum(diffs)
def part_two(left_list: list, right_list: list) -> int:
right_counter = Counter(right_list)
similarity_score = 0
for left in left_list:
if left in right_counter:
similarity_score += left * right_counter[left]
return similarity_score
def main():
with open("day1") as file:
lines = file.readlines()
left_list = []
right_list = []
for line in lines:
left, right = line.split()
left_list.append(int(left))
right_list.append(int(right))
print(part_one(left_list, right_list))
print(part_two(left_list, right_list))
if __name__ == "__main__":
main()