AOC 2020 - Day 3
Well, it’s day 3. I’m trying not to hit (more than the minimum amount of) trees with my toboggan. I did this challenge during the day, and I did a little better: 18:27 (I’m adding a couple minutes because I started the timer after I read the problem by accident). I wonder how much of the time difference is just being a little less tired? Hard to say. I wish I had the energy to stay up and work on the problems at midnight, but I think that would just lead to me being frustrated and burnt out on the whole thing.
I could have done much better; I lost about 5 minutes to a bug I introduced when I refactored for the second part of the puzzle. I forgot that str.splitlines()
removes the line separators, and file.readlines()
doesn’t, which caused my regressions to fail. It took me a bit to figure out why, but I got it eventually, after holding down ctrl-z and ctrl-y for a bit. Maybe I should have jumped straight to pdb? But that cost me a ton of time on the last puzzle. Pre-emptively, I could’ve not changed from file.read()
and a later data.splitlines()
, but I thought there was no difference at the time. Moral: Do Not Change The Working Code Lest Ye Be Damned? Print Out Some Intermediate Data And Maybe You’ll Calm Down?
The puzzles are still simple enough that there’s not a lot of design choices to explain. Basically, the whole problem reduces to, “Find the number of hashes on a given integer slope in the map.” The only wrinkle is that the map is infinitely repeating on the positive x axis, leading to the modulo on line 8 of the function. Initially, I wrote this as a script, then refactored it into a function when the second part of the puzzle required running multiple slopes on the same map, then finding the product.
def check_slope(slope, lines):
nlines = len(lines)
lenlines = len(lines[0])
y = 0
x = 0
ntrees = 0
while y < len(lines) - 1:
x = (x + slope[0]) % lenlines
y += slope[1]
ntrees += int(lines[y][x] == '#')
return ntrees
This is all pretty idiomatic python, I probably could have named things a bit better though. I write stuff like this all the time when doing image processing, so I didn’t mess up the indices, incrementing by the slope properly, or anything silly like that this time.
Heeeere’s YAGL (Yet Another Github Link). Tune in next time for more silly mistakes with Nathan.