Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Review the Solution

00:00 The first line, we start a loop, and what we’re doing here is that we’re just looping over indices that run from marker_length, which means that essentially we start looking at the first, say, four-character marker in part one of the puzzle.

00:16 So we start looking from there, and then we’ll just increase character by character until the end of the stream.

00:23 Then on what’s line 7 there, we pick out the candidate that we want to check to see if it’s a marker. So we do this just by slicing into the stream based on the n number that we’re looping over.

00:35 So the first time that we’re looping, again for part one of the puzzle, and will be 4. So we’ll do 4 - 4, so we’ll start at zero and then until 4.

00:46 So that gives us the four characters.

00:49 And then we’re doing the check on the candidate. And we had this insight where we figured out that one way to check if there are repeated letters in a candidate is just by converting it to a set because that operation removes duplicates.

01:03 And if anything has been removed, then the length of the set will be different from the marker length. So in that case, it’s not the marker. If the length is equal to the marker length, that means that we have found a marker, and we can stop looping, and we stop looping here by having a break.

01:20 But we can actually now change this since we put this into a function.

01:24 I’ll change the break to be a return statement instead where I’m returning n, which is then, at this point, the marker.

01:33 Okay, now that we have the function in place, we can check now that we can call it. So I’m calling find_marker with the stream, and then I’ll, let’s be explicit here.

01:48 I’ll set marker_length equal to 4.

01:51 So if I save this, I can see that I can run this. And now with the full long input, we get the familiar number of 1538.

02:03 And then to kind of finish this up, we’ll also do the same function call, but now with marker_length to 14, which is part two of the puzzle.

02:14 So if we run this now, we can see that we get both answers, 1538 and 2315, and we can remind ourselves, if we go here to day six, that those were indeed the correct answers.

02:29 Let’s see if we get both of them on-screen here.

Become a Member to join the conversation.