
Crédit: AI Generated image
Advent of code...
Advent of Code is restarting... It's a one time a year adventure where developers are offered 25 consecutive problems (one for each of the advent calendar day) from the 1st of December until the 25th of December. The first installment of this was in 2015, it's then the 10th time is is provided to us.
Each problem is revealed at 12PM New York time (so 6AM for me...) and is divided into 2 parts and one must complete the 1st part to be provided access to the 2nd... Each successful part is worth 1 golden star and so the holy grail of the challenge is to get those mighty 50 golden stars (yes that's it, nothing more than 50 little shiny golden stars... :D)
The appeal of such challenge for me, is just to improve yourself at logical thinking, problem simplification, algorithm writing and code efficacy improvement. There is a leaderboard based solely on the time taken to finish after the initial reveal but it's one I compete in as it's just really too soon ;-). I like the challenge of being able to find the solution by myself.
Day 01 - Historian Hysteria
The fist day is always the time where the story is presented (because, yes there is a story on which is based the problems to render it more "concrete").
So, this year, the issue is that the Chief Historian is missing and is supposed to be present, as every year, at the big Christmas sleigh Launch. Our job, if we choose to accept it, is to find him back...

So, for this day, the only information we have on the whereabouts of the Chief Historian is a piece of paper with numbers on it that resembles to the following:
13 4
24 3
32 5
41 3
53 9
63 3
Each of those are location ids and so we want to understand why 2 lists and if they are more or less...
1st part - sum of differences
So the first part is to identify the sum of the differences between the 2 lists when they are sorted...
The idea is then to:
- parse the input to get all integers:
1r = [list(map(int, line.split(" "))) for line in lines]
- get both lists (one for the left part of each row and one for the right part of each row)
1left = [a for a, _ in r]
2right = [a for _, a in r]
- sort both list
- get a list made of each "couple"
- sum the delta between the 2
Those 3 last ones are done together due to the zip
function in python which create a list of the combination of the elements at the same index in lists provided in input combined with the sorted
function which returns a copy of the list in input sorted:
1return sum([abs(a - b) for a, b in zip(sorted(left), sorted(right))])
2nd part - sum of the presence
The result of the 12st part leading to the notion that both lists are quite different, the next option is to analyze the data from another angle : what about checking how many times each element in the left list is present in the one on the rigt...
There a small change and the usage of a Counter
(from the collections module). It allows to generate a dictionary with the index being every element of an iterable and the value of each index is the number of times that element is present in the iterable...
1right = Counter([a for _, a in r])
After that, the only action is to sum the similarity score (own AOC invention) of the left list which summing for each element the result : the element * the number of time that element is seen in the right list like so:
1return sum([a * right[a] for a in left])
Conclusion
And so, I felt that it was a really simple start with no real hidden complexities, so let's enjoy and onto the next one!
The Repository
The following repository will keep in the main branch all the code of this year resolution (and also presents the ones from the previous year I already solved... I'm still missing some years and days...)