solve day 1, no time for pretty code
This commit is contained in:
parent
aa505e5bca
commit
40bc8ca23e
4 changed files with 1057 additions and 0 deletions
38
day_01/program.py
Executable file
38
day_01/program.py
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# https://adventofcode.com/2024/day/1
|
||||
|
||||
def get_lines(filename: str) -> list:
|
||||
with open(filename, "r") as file:
|
||||
return [line.strip() for line in file.readlines()]
|
||||
|
||||
|
||||
def main():
|
||||
# lines = get_lines("sample-input.txt")
|
||||
lines = get_lines("input.txt")
|
||||
list_a = []
|
||||
list_b = []
|
||||
for line in lines:
|
||||
(a, b) = line.split(" ")
|
||||
list_a.append(int(a))
|
||||
list_b.append(int(b))
|
||||
sorted_a = sorted(list_a)
|
||||
sorted_b = sorted(list_b)
|
||||
# print(sorted_a)
|
||||
# print(sorted_b)
|
||||
|
||||
total_distance = 0
|
||||
for i in range(len(sorted_a)):
|
||||
total_distance += abs(sorted_b[i] - sorted_a[i])
|
||||
print("Part 1: Total distance is", total_distance)
|
||||
|
||||
similarity_score = 0
|
||||
for i in range(len(list_a)):
|
||||
search_val = list_a[i]
|
||||
appearances = [j for j, x in enumerate(list_b) if x == search_val]
|
||||
similarity_score += search_val * len(appearances)
|
||||
print("Part 2: Similarity score is", similarity_score)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue