Advent of Code: Day 1

 I'm going to start off being boring in Python, but it's something! And hopefully I don't make any embarrassingly inefficient choices along the way.

2024 Day 1 Problem: https://adventofcode.com/2024/day/1#part1

Splitting the list into 2:

list_1 = []
list_2 = []
first = True
for i in number_list:
if first:
list_1.append(i)
first = False
else:
list_2.append(i)
first = True
print(list_1)
print(list_2)

Sorting the list: 

list_1.sort()
list_2.sort()

And finally, adding up the distances, using the zip function which I recently discovered:

sum = 0
for i,j in zip(list_1, list_2):
sum += abs(int(j) - int(i))
print(sum)

I might come back and do this in Haskell or some other impractical language... we'll see... but for right now, the ease of Python might just give me a few extra % on my final projects and exams, so I'll have to stick to that.

And that brings me to part 2 :) 

Here, it feels like casting the second list to a dict is the obvious choice, the problem's trivial after that.

list_2_dict = dict()
for i in list_2:
list_2_dict[i] = list_2_dict.get(i, 0) + 1

And then... we do the seemingly arbitrary similarity thing.

similarity = 0
for i in list_1:
similarity += list_2_dict.get(i, 0) * i
similarity

And... that's day 1 :) 

Answers:

2742123 and 21328497

Comments