Add 2024 day1
This commit is contained in:
parent
74a9f60b12
commit
bd9480c6a7
|
@ -5,6 +5,7 @@ def main():
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
|
|
||||||
matrix = [list(line.strip()) for line in lines]
|
matrix = [list(line.strip()) for line in lines]
|
||||||
|
print(matrix)
|
||||||
last_column = len(matrix[0]) -1
|
last_column = len(matrix[0]) -1
|
||||||
last_row = len(matrix) -1
|
last_row = len(matrix) -1
|
||||||
print(last_column, last_row)
|
print(last_column, last_row)
|
||||||
|
|
|
@ -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()
|
Loading…
Reference in New Issue