Here we go again. New Advent of Code has started today. I plan to take on the challenge and solve all the tasks before Christmas (even if some of them are solved later than the publishing day).
I don’t care that much about my position on the leaderboard. I’m not a competitive programmer, and I don’t stand a chance with someone who practices that, even semi-professionally. That said, I wouldn’t mind getting at least one point ;)
Although I’m tempted to try some new programming language (recently Scala is on my radar) for now I decided to stick to Python. I’m not a Python programmer, so I’m pretty sure my code won’t be idiomatic. Nevertheless, I like to tinker with this language from time to time. The event is a neat way to refresh some knowledge (I didn’t have many chances to work with Python since the last AoC) and possibly learn new stuff.
Without further ado, let’s take a look at today’s solution. The task can be found here, so I won’t copy-paste it.
import fileinput
def parse():
return [int(l) for l in fileinput.input()]
def task1(input):
previous_depth = None
total_deeper = 0
for depth in input:
if previous_depth and depth > previous_depth:
total_deeper += 1
previous_depth = depth
return total_deeper
def task2(input):
previous_depth = None
total_deeper = 0
triplets = [input[i:i+3] for i in range(0, len(input))]
for triplet in triplets:
depth = sum(triplet)
if previous_depth and depth > previous_depth:
total_deeper += 1
previous_depth = depth
return total_deeper
input = parse()
print(task1(input))
print(task2(input))
Some explanations:
-
fileinput
is a neat module that I discovered last year. Makes reading from file or stdin easier. -
parse()
- at first, here I had a loop with appending to the list. List comprehension looks so much better, though. It might be even slightly faster, but I would be careful with giving such opinions. And not that it matters here. -
task1()
- solution for her first task. Very imperative and probably not very pythonic. But does the work. In the loop we need to know the previous depth, so we use theprevious_depth
variable and compare against it in the loop. When the current value is grater than theprevious_depth
, we increment the counter. When the loop is done, we’re done too. -
task2()
- task 2 is very similar to the first one, but now we operate on a three-measurement sliding window. A quick and dirty solution, that I had in the first place, is to iterate overrange(len(input))
and suminput[i] + input[i + 1] + input[i + 2]
. The caveat here is that we can get index out of range, so we need to protect against it. I didn’t like this solution that much and changed it to the list comprehension. It looks much cleaner, but might be slightly harder to grasp by non-python developers.
Today’s ranks: 2694/2855. I didn’t read carefully and made an off-by-one error in the first part. Waiting for the whole minute is a pain, and I think it has significantly worsened my rank.
Let’s see what tomorrow brings!
Top comments (0)