Loop Over Candidates
00:00 Okay, so now what we need to do is find a way that we can loop over our stream. And in this case, there are a couple of different ways we can do this, but we want to keep track of some indices while we’re doing this.
and then we’ll just loop through the end of the stream that we have. And then we’ll pick out the candidates. Now I can do this as starting at
n-marker_length and looping until
n, or sorry, slicing until
n. I was just about to ask why you start with
marker_length and not with
0, but the idea is that you start at
marker_length and then look the, now in this case, four characters or four positions that come before the point where you are in the loop right now.
These are mostly equivalent. I chose the first one because the number that we’re looking for is the end of the sequence. So if the first four characters had been a marker, the answer would’ve been
And also to quickly point this out, in the
for loop where you start at
0, you subtracted the
marker_length from the length of the stream, because once you reach the end of the stream minus the
marker_length, you don’t need to look forward.
02:38 Like, there aren’t enough characters anymore to make this work, and the other thing is that you don’t want to have the index out of bound there. Exactly. Now, typically the Advent of Code puzzles, they’re all well formed in the sense that there will be an answer.
So if I would kind of just trust that, I wouldn’t really care too much about subtracting the
marker_length, because I know that I will find a marker before then, but it’s still good practice to not willfully run into indexing errors.
Become a Member to join the conversation.