The Problem
We’re still in the relatively easy phase. In this problem, we’re given a map with trees marked in, and on a straight-line path, we have to count how many trees we encounter. The only thing to account for is that the map repeats horizontally, we have to replicate it as many times as needed till we reach the bottom. We do the same in part 2, for different lines.
My Solution
I thought this was pretty straightforward; it took me about ten minutes for each part. However, when I looked at other solutions…
Algorithm
Not really an algorithm, but I was kicking myself about this. I physically (is that the right term?) replicated the map as many times as needed (even did a calculation to figure this out upfront). Turns out all I needed to do is use the mod
operator!
Implementation
- The last input in part 2 required going down two rows at a time. I wrapped myself up in knots doing this, but all that was needed was a
[::2]
- Lots of opportunities for (list) comprehensions, I need to start recognizing these.
- Check this out:
trees = sum(forest[i * sr][(i * sc) % cols] == '#' for i in range(rows // sr))