Advent of Code Day 2
- 2 minutes read - 234 wordsSo back to Advent of Code. And yay, Day 2 went swimmingly as well.
The first part looked hard, but then I realised that I’d made the usual “make it look harder than it is” mistake. You don’t need to compose the whole list, just keep track of how far you’ve got each time. This was pretty straightforward with some counting of offsets. Part 1 done.
I’ve often found that Part 2 of a day’s challenge is a tweak to Part 1. For Day 1,
I built a Counter object and then used it to identify the most common entry:
kount = Counter(ids)
one = (kount.most_common()[0][0])Part 2 then required the least common entries instead, which is the delightfully Pythonic:
kount = Counter(ids)
one = (kount.most_common()[-1][0])I actually just tried that1 as my first guess, and was reaching for the documentation when my code worked. I was gobsmacked.
However, today’s Part 2 didn’t relate to Part 1, and I ended up re-using the Counter object stuff instead.
Roll on Day 3.
Yes, I use the word
kountas a variable name instead of the more obviouscount. That’s becausecountwas a reserved word in dBaseIII, so my dear old Dad spelled it that way, and passed the habit on to me. Admittedly, that was in the late 1900s, but my excuse is that it’s still a reserved word in modern SQL. ↩︎