|
1234567891011121314151617181920212223242526272829303132333435 |
- # https://adventofcode.com/2024/day/1
- # Calculate the sum of the distances between the smallest values in each
- # list, then the second-smallest, then the third-smallest, and so on.
- # The lists are the same length.
-
- FILENAME = "input01.txt"
-
- def main():
- with open(FILENAME, "r") as file:
- lines = [line.strip().split() for line in file.readlines()]
- first_list, second_list = [], []
- for line in lines:
- first_list.append(int(line[0]))
- second_list.append(int(line[1]))
- sorted_first = sorted(first_list)
- sorted_second = sorted(second_list)
- print(f"""First list: {first_list[:10]}...
- First list sorted: {sorted_first[:10]}...
- Second list: {second_list[:10]}...
- Second list sorted: {sorted_second[:10]}...
- """)
- sum_dists = 0
- for i, el in enumerate(sorted_first):
- s_el = sorted_second[i]
- dist = abs(el-s_el)
- sum_dists += dist
- if i%5 == 0:
- print(f"Current locations {el} and {s_el}, current distance {dist}, accumulated distance {sum_dists}.")
- print(f"Total accumulated distance: {sum_dists}")
- return sum_dists
-
-
-
- if __name__ == "__main__":
- main()
|