Advent of Code 2024 - Day 1

# Part 1

 1def get_data(path):
 2    list_1 = []
 3    list_2 = []
 4    with open(path) as f:
 5        for line in f:
 6            item_1, item_2 = line.split()
 7            list_1.append(int(item_1))
 8            list_2.append(int(item_2))
 9    return list_1, list_2
10
11def main(list_1, list_2):
12    output = 0
13    for element_1, element_2 in zip(sorted(list_1), sorted(list_2)):
14        output += abs(element_2 - element_1)
15    return output
16
17if __name__ == "__main__":
18    print(main(*get_data("day_1_input.txt")))

# Part 2

 1import collections
 2
 3def get_data(path):
 4    list_1 = []
 5    list_2 = []
 6    with open(path) as f:
 7        for line in f:
 8            item_1, item_2 = line.split()
 9            list_1.append(int(item_1))
10            list_2.append(int(item_2))
11    return list_1, list_2
12
13def main(list_1, list_2):
14    counter = collections.Counter(list_2)
15
16    output = 0
17    for element_1 in list_1:
18        if element_1 in counter:
19            output += element_1 * counter[element_1]
20    return output
21
22if __name__ == "__main__":
23    print(main(*get_data("day_1_input.txt")))