[Advent of Code 2024](https://adventofcode.com/2024)
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

day01-1.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435
  1. # https://adventofcode.com/2024/day/1
  2. # Calculate the sum of the distances between the smallest values in each
  3. # list, then the second-smallest, then the third-smallest, and so on.
  4. # The lists are the same length.
  5. FILENAME = "input01.txt"
  6. def main():
  7. with open(FILENAME, "r") as file:
  8. lines = [line.strip().split() for line in file.readlines()]
  9. first_list, second_list = [], []
  10. for line in lines:
  11. first_list.append(int(line[0]))
  12. second_list.append(int(line[1]))
  13. sorted_first = sorted(first_list)
  14. sorted_second = sorted(second_list)
  15. print(f"""First list: {first_list[:10]}...
  16. First list sorted: {sorted_first[:10]}...
  17. Second list: {second_list[:10]}...
  18. Second list sorted: {sorted_second[:10]}...
  19. """)
  20. sum_dists = 0
  21. for i, el in enumerate(sorted_first):
  22. s_el = sorted_second[i]
  23. dist = abs(el-s_el)
  24. sum_dists += dist
  25. if i%5 == 0:
  26. print(f"Current locations {el} and {s_el}, current distance {dist}, accumulated distance {sum_dists}.")
  27. print(f"Total accumulated distance: {sum_dists}")
  28. return sum_dists
  29. if __name__ == "__main__":
  30. main()