# Advent of Code 2025 | Day 1 and 2 | JavaScript / TypeScript

## Метаданные

- **Канал:** Coding Garden
- **YouTube:** https://www.youtube.com/watch?v=TD_FwAIEpqI

## Содержание

### [0:00](https://www.youtube.com/watch?v=TD_FwAIEpqI) Segment 1 (00:00 - 05:00)

Hello friends. Welcome to Coding Garden. Uh today we're going to do some Advent of Code. What's up Dynamic Voyage? Good to see you. Um this is a rare late night stream. And we're not exactly doing the challenges as they're released either because I haven't even seen day one yet. Uh but the plan for today is to do the Advent of Code challenges here. And if you didn't know, this year they only have 12 challenges instead of 25. Um, but we're going to do days one and two. Each one should have two parts to it if it's the same as usual. And these are just algorithmic challenges. That should be pretty fun to do. So, that's the plan. Um, there's just a few of you here and that's okay because it's late at night. But, welcome in. I'm curious, is anyone else doing Advent of Code or have you tried yet? Are you working on day two? cuz day two just released 3 minutes ago. They release at midnight Eastern time every day, I believe. Uh, what's up, Bucha? Long night, long time no see. Itakito says they did the first one. Nice. I'm trying to find if I have Yeah, I do have an Advent of Code directory. You also did what? Day one. Cool. All right, let's without further ado, let's get right into it. Uh so I have my directory here. Uh first I'll give an intro of what this is and then uh we'll do the thing. So this is advent of code. Uh they release a new challenge every single day. It's usually a multi-part challenge and you can use any programming language you want to solve these problems. Uh you could even solve it by hand if you want to. You could use Excel. Uh basically these problems aren't programming language specific and they do it every year. Um so can we go to a previous year? Yeah. So if we look at like 2024, you can see here are all the challenges from last year. Um and it goes way back but we of course are on uh 2025. So without further ado, let's jump right in. Advent of code day one challenge one. Day one secret entrance. The elves have good news and bad news. The good news is that they've discovered project management. This has given them the tools they need to prevent their usual Christmas emergency. For example, they know they now know that the North Pole decorations need to be finished soon so that other critical tasks can start on time. Does this link to Wikipedia? Yeah, this is the Wikipedia link for project management. Um, I have a feeling that like all of this description here is like kind of setting us up for advent of code and then the real problem is down here. But uh I I'll still read all of this. Uh the bad news is that they've realized they have a different emergency. According to their resource planning, none of them have any time left to decorate the North Pole. To save Christmas, the elves need you to finish decorating the North Pole by December 12th. Collect stars by solving puzzles. Two puzzles will be made available on each day. The second puzzle is unlocked when you complete the first. Each puzzle grants one star. Yeah, so this is advent of code. Two puzzles a day. You arrive at the secret entrance to the North Pole base, ready to start decorating. Unfortunately, the password seems to have changed so you can't get in. A document taped to the wall hopefully explains, "Due to new security or helpfully explains, due to new security protocols, the password is locked in the safe below. Please see the attached document for the new combination. The safe has a dial with only an arrow on it. Around the dial are the numbers 0 through 99 in order. As you turn the dial, it makes a small click noise as it reaches each number. The attached document, your puzzle input, contains a sequence of rotations, one per line, which tell you how to open the safe. Rotation starts with an L or an R, which indicates whether the rotation should be to the left, towards the lower numbers, or to the right, or the higher numbers. Then the rotation has a distance value, which indicates how many clicks the dial should be rotated in that direction. So if the dial were pointing at 11, a rotation of R8 would cause the dial to point to 19. So it go right 8. And that rotation of L19 would cause it to point to zero. So go back to zero. Okay. Because the dial is a circle, turning the dial left from zero one click makes it point to 99. Yeah. So it goes back around. Okay. Similarly, turning the dial right from 99 one click makes it point to zero. That makes sense. So, it's a circle, a circle of numbers. So, if the dial were pointing at five, a rotation of left 10 would be 95 because it goes backwards five and zero is on there. So, it would cause it to point to 95. After that, a rotation of R5 would cause it to point back to zero. Okay, we get it. This is a dial. The dial starts by pointing at 50. So, given our puzzle input starting at 50, where are we going

### [5:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=300s) Segment 2 (05:00 - 10:00)

to end up? And this is an example that's going to end at a certain point. We need to make sure that our code gets gives us that example point. You could follow the instructions, but your recent required official North Pole secret entrance security training seminar taught you that the safe is actually a decoy. The actual password is the number of times the dial is left pointing at zero after any rotation in the sequence. For example, suppose the attached document contain the following rotations. Okay, so these are the rotations. It starts at 50. L68 puts it at 82 50. Left is backwards though, right? Yeah. So that would go back past zero to 82. Left 30 52 right 40 zero. So that's one. Uh left five goes to 95. Right 60 55. L5 goes to zero. L1 points to 99. L99 points to zero. R14 goes to 14. L82 goes to 32. So it went to zero three times. So the password is three. Let's write some code. Okay. So I need to get my uh my directory set up and I am going to init it with pnpm and then I'm going to install uh types for Node. js. So we can just use Node But we'll be able to use TypeScript if we really want to. Um, and then I need TypeScript. Cool. Can I do init? Will that give me a Is it d-init give me a TS config? Great. We have a TS config now. Okay. Uh, I'm going to open this up with VS Code. I'm going to pause my break timer because it keeps interrupting us. Okay. So, here is our directory. Um, I believe I'm going to create a new folder for each day. So, we're going to do 01 for day one and um 01. ts TS for problem one, day one. Is that going to get confusing? I don't know. Yeah, that is the point of it. What's up, Andrew? I'll say hello to everybody really quick before we get into it. Saw quite a few more people pop in. Um I don't know. I just want to get this stuff done. Um yeah, what's it? Good to see you, Arvin. Yeah, and you're very welcome. Thanks for being here. And hello, Andrew. Good to see you as well. Yeah, I agree. Itito actually I think I believe I used Dino last year but node has gotten to the point where you can just use typescript directly with node. So here's what we need to do. We will download our puzzle input. So when you're solving these you'll see get your puzzle input. This is just a single file that's specific to you. So you can't copy paste the answer from somebody else because every single person that logs into the site has a unique input. So, this is going to be our day one input. And I'm going to create a new file called input. txt. Throw that in there. Get rid of the new line on the end. And then I'm going to have a sample input. That's this one. And with this sample input, we're going to try to write a program that gets us to the number three so we know that it's working. And then we'll run it on the real input. So, let's call this sample input. txt. And now we need to read it in. So, I'm going to bring in FS from FS promises. Um, I'm going to set the type here to module. And do I need to do anything special to get Typescript to recognize this? I think so. I did install types node. We need to do this in our TS config so that it knows we're using Node. js. And now yes, we have access to FS. So, uh, our input is going to be we'll just do uh read file. We want to read in the sample input.

### [10:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=600s) Segment 3 (10:00 - 15:00)

And we'll read this in as UTF8. And then for now, let's just log it out to make sure that our code works. And then we'll give it a try. So we'll say run 0101. ts. Everything is running relative to the root. That's okay. We'll go into the 01 directory and then we can say run the file spin it up. Okay. So, we can read the sample input. Uh let's create a function called part one that takes in the input. Um we're going to what are we going to call these? These are rotations. We're going to call this rotations, which is just an array of string. And we're going to read this in down here and then pass it into part one. So we'll do input. split on the new line. And then if we log these out, we should now I get log anymore. How do I console log? It's been so long. uh and we log out these rotations. Now we should get an array. Wonderful. Okay, so we have the array. Um we know that the starting point is 50. I'm going to put this out here. So I'm going to say the um start is 50. The uh min is zero and the max is 99. We're going to have the current location and that starts off as start. And then we're going to iterate over each rotation and change what current is. So I'm just going to use a for each on this. This is going to give us each rotation and then we need to parse it. So it's going to be a direction and then a number. So we'll split it. We'll get the direction and the value. This will just be rotation. I guess I don't really need to split it. We could do a slice. So if we go from zero to one, that gives us the direction. And if we go from one that should give us the value like so I run my codes now we should have it parsed. Fantastic. Let's go ahead and uh turn this into a number. Cool. And now we do some maths. we will now overwrite current to be current plus value. But oh well if it's left or if it's right. So if it's right then it's going to be plus. If it's left minus. So we'll say if dur is equal to right do this. Else if the dur is equal to l do this. So write is going to be plus l minus. And now we have to do some modulus. So if we're going to the right, we'll do current plus value mod max, I think, and then we'll do current minus value. And if it's a negative number, we take the absolute value and subtract that from max, right? I think um yeah. So right here we'll say if current is less than zero then we'll say current is max minus current I think. Um let's just log

### [15:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=900s) Segment 4 (15:00 - 20:00)

current every time to make sure we're getting the right value. And if we are we'll count up the zeros. So we should be expecting uh 82 52 0 etc. Unless we really mess something up. We really messed something up. So, um, did I go the wrong direction? Left should be toward lower number. So, should subtract the value. And I guess we don't need min. We might have to do modulus max plus one. Yeah, I'm not so I do see suggestions in the chat, but I'm not going to read the chat until I figure it out. Um because I do want to figure it out myself, but if I can't figure it out, I will read the chat, but I'm going to try to figure this out myself. Um I just want to get the first one working. So I know that the first value we get should be uh 82 because we read in L68. Let's keep logging the dur and the value. So with a left value of 68 when we started at 50 - 68 is a -8 and 99us 18 is 81 but we need to get 82 and we are getting 117. That's not good. Um Oh, because we're overwriting it. Okay. Yeah. And I think we want max to be 100 to give us the right value because if we get 100 - 18 that gives us 82 which is the value that we're expecting. Okay. So current would be 50 value would be 68. Current minus 6 68 would give us a8. So -18 is less than zero. So we then do we want to do absolute value. So we could just do plus because we know that it's negative. That should give us the right value there. Okay. So 82 52 0 95 55 0. This is looking good. Let's double check. So 82 52 0 95 55 0 99 0432. Okay, we got there. It took me a second, but we got there. And so now all we need to do is calculate all of the zeros. So we'll say let the password starts off as zero. And if the current value is equal to zero, then password plus equals 1. And then we'll log the password at the end. Okay, passwords three. Fantastic. Seems simple enough. I could have messed something up, but it seems simple enough. Now we're going to run it on the real input and hope that we did everything right. So all the code should be exactly the same except now instead of running it on just 10 lines, we're going to run it on this which is a few thousand lines. So, we will hope for the best. The password we got was 991. Drum roll, please. That's not the right answer. My answer is too low. Dang it. Okay. Um, let's keep track of that. 991 too low. Yeah. Oh, fascinating. Andrew was saying their answer was 992. Yeah. And we mentioned it a little bit ago. Everyone's going to get a different answer for every problem because everyone has a different input. So, yeah, I'm doing something wrong. I'm guessing it has to do it. It's some kind of rounding error. Okay. So, it works

### [20:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=1200s) Segment 5 (20:00 - 25:00)

for the sample input. Um I think the issue potentially is okay yeah let's say we're at we need to handle like the overflow in the other direction right um so let's think about If we're at 50, which is like here, and we go minus 68, that's going to put us here. And then if we go left again, that puts us at 52. Okay. I'm curious, would we get the right answer if we keep going left? And I guess we could just test it out. Like if we're at two and we do an L of three, what do we get? Let's try it on a second sample input. Actually, we can just hardcode it. Um, so if we start off, well, we we're always going to start off at 50. So if we do an L of 51, that should get us to zero, right? It got us to 99. I think that's that might be our issue, right? Because if we're at 50 and we do an L of 51. An L of 50 should get us to zero. 51 would get us to 99. So that's right. An L of 50 when we start at 50 would get us to zero. A right at of 49 when we start at 50 gets us to 99. A right of 51 should get us to one. It does. Okay. Yeah. I guess the next thing to try is like really big numbers. So if we start at 50 and we go right 100, what is that going to do for us? 50 + 50 would get us to zero plus another 50 would get us to 50. So that's right. But can we go backwards 100? Yes. So we start at 50, we go minus 50, which gets us to I guess are we handling saying if it's less than zero? Yeah. Okay. I'm actually curious if we log all of the uh values from the input, are we going to get anything that's not between 0 and 99? And if we do, then that's our potential issue. Um, so if let's just run it on everything and then just inspect the output. Are there any weird numbers in here that are not from 0 to 99? Yes, we have negative numbers. This is bad. This is our issue. We need There's a scenario that we're not accounting for that gives us very large negative numbers. And I think it has to do with this. I believe we need to mod this with the max as well. Um after applying the maths. So we should do current minus value mod the max.

### [25:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=1500s) Segment 6 (25:00 - 30:00)

Okay, that got rid of our negative numbers. We do have negative 0, but that's okay. Negative 0 is the same as zero. Okay, I think that was our issue. We just weren't modding this one. That that's pretty easy to figure out. Uh, if we run it on the sample input again, we should get three still. We do. Okay, so our answer is 1152. That's the issue. Yes, that was a pretty simple fix. We just forgot to do the modulus for uh when we're doing subtraction. Okay, I'm pretty happy with this solution. Let's move on to part two and I'll catch up on the chat if anybody has uh popped in since I started. Yeah, what's up? Hello chicken. Good to see you. Uh part two always comes with a twist. Yeah, we'll see if our code is set up in a way that will handle part two. What's up, child? Hello. Hi. Uh, work prevents me from solving early. We'll just solve day two tomorrow. Yeah, exactly. Well, that's the nice thing, honestly, about this year is there is no global leaderboard. There used to be a global leaderboard and everybody was like competing to be the first one to solve it. Now, you can create like a private leaderboard. Well, they had private leaderboards last time, but now there are only private leaderboards. So, I feel like there's less pressure to get it done right when it's released. Um, and what's up, uh, Ander, uh, Minashtra from Spain? I've been studying software development at university for about four months now, and it makes me really excited to think that soon I'll be able to tackle challenges like this on my own. Nice. I'm slowly starting to understand more and more, and I'm sure I'll get to that level. Yeah, I'm sure you will, too. Just keep at it. Uh, Slipner says, "Shouldn't be max plus current because it'll be negative. " Yes, we figured that out. Um before we had minus so it was minus a minus which became well no that was plus but this is plus and negative that made minus a negative made it plus a positive so yes we figured that out. Um Lander trade says I mod it with 100 instead. Yeah we figured that out cuz I was modding with 99 but we needed to take into account the fact that zero is included so we mod with 100. Yeah, exactly. Sam, we figured it out. Uh, what's up, funny boy? It's been a long time. Um, yeah, and negative zero in JavaScript. So, do you know how the answer validation works? Because sometimes it says that my answer is too low, but other times it doesn't say anything. Just wondering what's the criteria for that message to appear. I believe that they just have server side code that generated our input. So, there is a serverside process that generates a unique input for every user. And there's already a solution on the server side that given and it's a valid solution that given this input calculate the right answer. So I think I mean I don't know if it's open source and we could there's probably some documentation out there on how they do it but my guess is for every user generate a unique input calculate what the answer should be based on a known good solution and store that in a database somewhere so that we can just compare it to it. So then it's a simple matter of less than or greater than for whatever they submit because they already know what the answer should be. That's my guess. That's how I would implement it anyways. All right, let's get on to part two. Um, and boy says, I think there's 10 inputs that are randomly chosen per user. Do you mean like they just kind of randomize the overall input here so it's like mostly the same for everyone? That would be curious. Okay, part two. You're sure that's the right password, but the door won't open. You knock, but nobody answers. You build a snowman while you think. As you're rolling the snowballs for your snowman, you find another security document that must have fallen into the snow. Due to the newer security protocols, please use password method 0x434C49434B until further notice. You remember from the training seminar that this method means you're actually supposed to count the number of times any click causes the dial to point to zero regardless of whether it happens during a rotation or at the end of one. Ah so anytime we pass over zero. Okay. Uh following the same rotations as in the above example. The dial points at zero a few extra times during its rotations. Um the dial starts by pointing at 50. The dial is rotated to L68 at point 82. During its rotation, it hits zero once. Yeah. So, we need to account for how many times it goes back around. Ah, this hex decodes to click. That's fun. Okay, this is going to be tricky. I don't think our existing

### [30:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=1800s) Segment 7 (30:00 - 35:00)

solution and I'm just going to copy paste it because uh I want to keep the codes that I have written for this. So I'm just going to create a new function called part two. And this will now have to keep track of if it goes past zero at all. Okay. Um for now I am going to debug the direction and the value and I think instead of doing mod we could do division to see how many times Right. I think because basically this value that we calculate right here, we need to see how many times that would go past zero instead of doing the modulus. So is there another mathematical thing we can do instead of just modulus? Because in the first example, we're going from 50 left 68. So it goes past 0 and it would be 50 - 68 which is um8 mod 100 is8 I believe or is it no it's zero Yeah, it's8. It's the value itself, which means it's not larger than max because 101 mod 100 gives us one. Wraps around. Cool. What's up, Crunchaw? Good to see you. Okay. So if we did -18 / 100, it would have passed zero. Um, well, if it's negative, we know that it has to go Wait, do past zero? Yes. If it's negative, it went backwards past zero at least once. I'm going to keep a note of take a note of that. Um, if negative, went backwards past zero at least once. We know that for sure. But if it was -18, that means it made one full resolution plus one to get past zero from where it was before. Is that making sense? So I think we can divide by 100 and do uh math ceiling to get how many times it went past zero. That is my guess. yeah I think I'm going to get this without AI pronal. I think I can do it. So here's my thought. So we create a variable called next. It's going to be current plus value. And we will say uh if next is greater than 99 then we're going to increase the password by uh max divided by next and we'll do math. Ceiling there. So

### [35:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=2100s) Segment 8 (35:00 - 40:00)

how many times did it go past zero? And then we'll use next here. Okay, this is my thought here. So, same thing. Um, if it was negative, then it went password then it went past zero at least once. So I'm going to do the same thing. I'm going to store it in a value called next. And so this really only accounts for here if it's negative. But uh this will be current minus value. And If next is less than zero. No. Okay, this is getting weird here because we're using different variables. I think I'll leave it as is because this was already working. But we know that if it's negative, it went past. but we need to hold on to it. So, we will store we will store it in a variable. But our if condition will stay the same. So, if it's less than zero, we then will say password plus equals Uh next or max divided by or what? How were we doing it? No. Next divided by max and we'll do math. absolute value of that and math. That how many times did it go past zero? I think that's it. Um, and we may or may not have to check if current is zero. Let's see if we skip the right answer. We got a value of 10 and we are looking for six. So, we got that completely wrong. Yeah. And hello, uh, best dev codor. Good to see you as well. Okay. Um, let's do some debugging to figure out what we're increasing the password by here. Um, we'll call this past zero times. We say times past zero. So, times past zero is that. And we'll log it out as well. do need to take the absolute value now if I can't figure this out this way with like maths. It would be very easy to just iterate like we could have an array with the values from 0 to 99 and then just uh iterate through the array uh and count every time we get back to the beginning of the array. That would solve it. It would be a pretty silly solution, but that also in my mind is easier to think about than trying to figure out the math way. But uh let's see what this gives us. Okay. So, uh times past zero is one when we go to 68 and that is correct. Okay.

### [40:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=2400s) Segment 9 (40:00 - 45:00)

And then when we go to 48 times past zero is one. Okay. So, what I think is we don't need to do this check anymore. But now we're off by one. Okay. But if we're not doing the check to see if it's zero anymore, let's look at our overall logic. So, um from 50 to 68 goes past zero once. Great. Um, that brings us to 82. From 82 left 30, 52. It doesn't go past zero. From 52 right to 48, it's hits zero. So that's once. Um, from zero left to 95. Oh, here's the issue. If it starts at zero, we don't include it because in this case, it started at zero and went back five, but we don't count it because we started there. That was the issue in this case. That's why we're off by one. And then if we're at 95 and we go right 60. Now if we're This says we went past zero twice and it says we only went past once. Okay. Oh, thank you, Mitsick. And good to see you. Who says uh just watch the pizza video? Nice. Okay, so we're getting two here when we shouldn't. Should only be one. And then from 55 left 55, it goes to zero. But then we do need to count that one. Okay. And then if we're at zero, we go left one. And it goes past zero once to get to 99. Wait, if it starts at zero, we don't count it. Okay. Um I think we'll just subtract it by one in this case. So we'll say um if current was zero that means we would have increased the password. So subtract one. Same thing over here before we overwrite current. Okay, that still gets us to seven. Um, I mean, this gets us Well, no, that doesn't get us the right answer if we're not counting it anymore. This is tricky. I am leaning towards creating uh like a circle like a list and then just iterating through it because um trying to do it the maths way is very hard for my brain. I guess figuring out how many times it goes past zero like my logic here isn't exactly right. Because yeah, that goes past zero once here.

### [45:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=2700s) Segment 10 (45:00 - 50:00)

I think I want to count for this because on this one when we when he when we go from um 52 right 48 it ends on zero and we don't want to say times past zero is one because the next value 52 2 + 48 is 100. Um, and I think I'm just going to say if time pass zero. No. If next is max. If next mod max does not equal zero then increase the pass the password or yeah I think because 100 mod 100 would give us zero so it wouldn't increase it Well, no, cuz if we had two No, if we had 200, that's also going to give us zero. We don't want that because that can rotate multiple times around. That will not account for it. Huh? Okay, I am going to read the chat, see if anybody has any hints. Otherwise, I think um I might just go with like a list version. Yeah. Uh uh quat quatpies. Yeah. In order to do this, you have to go to the website. So if you go to advent ofcode. com, you can first you'll need to log in and then you can click on the days to see the problem. So if you click on one, you're going to see the day one problem description. And for every problem, you're always going to see sample input, an example of how it works, and then an example answer that you should get. Um, and that'll be part one. And once you solve part one, that'll unlock part two. I'm currently working on part two. Uh, but for everyone, you also get your own puzzle input. So once you log in, when you go to the problem, click on puzzle input, and this is what you need to write your code against because everyone's puzzle input is different. So no one can cheat and just share answers. So, you need to download this and that's what you'll be writing your code against. Um, and so that's what I'm doing here. So, I currently have this file which is my unique input and I'm writing my code against that input to solve it. Yeah. Oh, thank you Pablo and good to see you. Welcome in. Yeah. And Panjal says in both left and right shifts, should we check start first? If it is zero, then update it. I don't know what you mean by that. Cool. Yeah. You can watch me struggle coding. You don't and you don't have to struggle it yourself. Uh because this one is really this is one this one's given it's taking me for um a loop. I guess it's uh I can't wrap my mind around it to figure out when it goes past zero cuz I thought my codes here was good enough like do the division to see how many times it goes past zero but we're getting extra times now. Yeah, I'm going to run the code one more time and then just compare back and forth. So, starts at 50, goes left to 68. That

### [50:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=3000s) Segment 11 (50:00 - 55:00)

means we went past zero once. Starts at 82, goes left by 30, gives us to 52. Starts at 52, goes right by 48, that gets us to zero. Um, starts at zero, goes left by five. This should not be counted. So, let's fix that. If we're going left by five, um, if current equals zero, no, I don't want to do that. I'll say um, I'll do it here. So if we need the current start because if that started at zero we will not count once. Um I need another variable for that because we're overwriting current. So I can just say if current start is equal to zero then we're going to subtract times past 0 minus one by one that Oh, we'll log that here. Yeah. So, that gets rid of that time pass zero. So, that fixes that bug. And from 95, we go right by 60. Um 95 right 60 to 255 that went past zero once and we're not accounting it for it here. So let's figure that out. Um, I don't think I want to do this. If the current is zero, subtract from the No, we're not going to do that. We're going to do it better because here, so 95 right 60 goes past zero once and we need to account for that and we're not accounting for that. Um, so the current is 95. We're adding 60. That gets us to 155. Oh, we're not logging it. That's why we need to log it. Okay. And then here's the issue. It says we went past zero twice. Um, so maybe it's when we go right, we don't want to do math. ceiling. Do we want mathf flooror when we go right? I guess that's closer. Let's go with it for now. And then 55 left 55 it goes to zero. So, do we still have the code that increases it by one if current is zero? Yes. Okay. And then if we're at zero and we go left by one that goes past zero technically, but it started at zero. So if it starts at zero, we don't need to count it. Um I Yeah. And I thought we were accounting for that here.

### [55:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=3300s) Segment 12 (55:00 - 60:00)

Oh, I guess we are cuz we said times past zero zero. Cool. So, we're accounting for that. Uh then it's at 99. It goes left 99 points to zero increase by one from zero goes right to 14. left to 82 goes past it once to land on 32. So why do we end up with seven and not six? Go. One, two, three, four, five, six. Why is password seven? What am I doing with password? Password starts at zero. Okay, we went past zero once. So, password is one. Nothing happened here. Password stays Pass well. Oh, this is the issue. We went past zero, but we landed on one, so it increased it twice. Yeah, I think this logic needs to be conditional somewhere, but we need to figure out where. But that I think that's the issue because then pass zero once, lands on zero, increase by one, pass zero, no lands on zero, increase, pass zero once. So this is good. But right here, that's too much. When we land on zero, we're increasing it twice. So, um, yeah, I was I've considered this. We we're there are multiple times I said I was going to stop and just do it the dumb way like with a circle or like a list of numbers that we just manually count through instead of trying to do like math ceiling and math. Floor, but we are so close. We're only off by one, which honestly seems fine for the sample input, which mean would probably mean we're going to be off by a huge amount for the larger input. But we do I do know where our bug is. Our bug is if it lands on zero. So the situation goes right 48. If it lands on zero and it went past zero to get there, then we're increasing it twice. We don't want to do that. So when we did right to 48. Um if next is greater than 99 and next does not equal zero, will that solve it? No. Oh, because if it's greater than 99, it's not going to be um

### [1:00:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=3600s) Segment 13 (60:00 - 65:00)

I think Here maybe we add this back in. So if the current is equal to zero, we subtract one from the password. Is that crazy? It gets us the right answer. H. Okay, I'm at the point where I we're getting the right answer. So I'm just going to try it on the normal input. But this is some funky logic. This is bad. I almost feel like I need to solve it the simpler way. But let's just run it on the normal input and see if this is the right answer cuz our logic is funky. It does not It's smelly. I hate it. Okay, that's not the right answer. So, um I think we're going to do it with just like a list of numbers that we iterate over and over again with pointers. That's how I'm Yeah, that's how I'm going to do this. Um, so I'm going to call this part two, try one. And, uh, we're going to do it in a brand new way where we just have a list of numbers that we iterate over to find the current one. And every time it touches zero, we increase uh, some counter. So, part two, try two. We're going to run it on the sample input. Um, I need a list. First of all, I'm going to I guess we'll the parsing will stay the same. All of the stuff inside is going to be way different. Okay. Um, we are going to have a variable called the wheel. And this is going to be an array that has the values from zero all the way up to 99 inside of it. So this is our wheel. It has 0 to 99 in it. Um our current pointer is at start. So we will have a loop. that starts off at current and it needs to go up to uh current. If we're going right then it's going to be plus value. So while I is less than or equal to current plus value on each iteration we increment I by one. Okay. So we start where we are. We go all the way up to current plus value because we're going right. Um cool. And we're going to be iterating over the wheel. So we'll do wheel at current and we'll say uh if wheel at current mod wheel. length length. I think that's the way to go, right? I guess we'll do I if that is equal to zero, increase the password. We start at current. Let's do current plus one because we don't want to count itself. So start at the next one. Keep going all the way until to the

### [1:05:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=3900s) Segment 14 (65:00 - 70:00)

right. The modulus should work, I think. And we're going to do the same thing but going left. So we'll start at current minus one. While I is greater than or equal to current minus value subtract I on each iteration. Um, and how do we do the maths here? We did current minus value modulus max, right? But then um I think we're going to have to do some special stuffs here. H. So, wherever we're currently at. Oh, we need to override current wherever we land, right? Because we're not doing that. Well, no. Yes. because we're we're changing the what the current should be. Um I think we need similar logic to the to how we did it in part one. So like this is the next one So, I guess we could just overwrite it afterwards after the loop. Yeah, I don't like this. I don't like this solution either. This is This feels so messy as well. But I think it'll be fine. I think it's going to be because after all of this iteration we overwrite current as well. Um, I kind of don't like this for loop though because this logic is messing with me because we start at current minus one. So if we're going left, let's say we start at 50 and we just did left 50. We should end up at zero. So we start at 49. We want to go while I is greater than or equal um 50 minus 50 which is zero. So 49 48 47 goes all the way to zero. That's fine. What happens when I becomes negative? So let's say we do we start at 50 and we do L60. So when it gets to negative one negative 1 so current is 50 minus 60 will be -10. So1 -234 that's still greater than -10. Seems fine. Let's run it. That works. We got a password of six. Okay. What was the uh does it tell me what value I submitted last time? Because I don't even remember what value I submitted last time. Um but the fact that we got it first try gives me confidence. So, let's ship it. This feels like the same number that we

### [1:10:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=4200s) Segment 15 (70:00 - 75:00)

got last time. Dynamic Voyer says 6102. Okay. So then this is different than that. It might have been this. Let's see. We'll try it again and then we'll keep track if it's wrong. We got it. Nice. Okay. Man, I don't like this solution at all. I'm going to have to um think about this a bit and come up with a more optimal solution cuz like this is fine, but it's like we're doing too much work here. Um we're literally iterating to find every time it hits zero again, which is fine, but there's got like this solution where we were trying to do it with maths. There's got to be a way and that isn't as messy as this. U but we figured it out. So great, day one done. Let's keep it moving to day two, which is today. Okay, gift shop. You get inside and take the elevator to its only other stop, the gift shop. Thank you for visiting the North Pole, gleefully exclaims a nearby sign. You aren't sure who is even allowed to visit the North Pole, but you know you can access the lobby through here. And from there, you can access the rest of the North Pole base. As you make your way through the surprisingly extensive selection, one of the clerks recognizes you and asks for help. As it turns out, one of the younger elves was playing on a gift shop computer and managed to add a whole bunch of invalid product IDs to their gift shop database. Surely, it would be no trouble for you to identify the invalid product IDs for them, right? Um, ask chat GPT for the solution to part one um for this problem. Yeah. All right. We We'll see you, Andrew. Uh, good luck on day two as well. Okay. They even checked most of the product ID ranges already. They only have a few product ID ranges, your puzzle input, that you'll need to check. For example, 11 to 22, 95 to 115, etc. The ID ranges are wrapped here for legibility. In your input, they appear on a single long line. The ranges are separated by commas. Each range gives its first ID and last ID, separated by a dash. Okay. Since the young elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So 55 64 and 1 2 3 would all be invalid IDs. None of the numbers have leading zeros. Your job is to find all the invalid IDs that appear in the given ranges in the above example. Ah okay. So we're trying to find repeating numbers. So given a set of ranges for each range find any repeating number in that range. I already don't feel good about this because how do I determine if numbers are repeating within a number? Like this is really tricky because you need to see 11885 is repeated 1185. O um like how do you know how many digits to check for repeating? I guess it's only two repeating numbers. So you would just divide it by two. So divide it in half and see if those two parts equal. That should be simple enough, right? Because one, two, three, four, five, six. So adding up. Oh, and then we need to add up the invalidities. Yeah, I think that's all you have to do is split it in half and then see if the two halves equal. I do know regular expressions, but a regular expression doesn't speak to me for this because they'll be of like varying lengths. I think that's how I'm going to do it. So, first of all, let's parse our sample input and uh we'll get day two set up. So, or part two set up. No, day two. Um, I realized we just put them in the same file now. So, I'm going to call this index. We're going to create our sample input. Um, it's supposed to all be on one line though, I believe. So, you read it in. It's a single line. we look at the regular puzzle input. Yeah, this is also just a single line. So, we'll also create our input

### [1:15:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=4500s) Segment 16 (75:00 - 80:00)

on a single line. That's Great. I'm going to copy in my parsing codes. So pull this in and um read it in first. We'll read in the sample input. Create a function for part one. Call this the ranges. This is going to be a say a list of strings, but we could have a list of start which is a number and end which is a number. We really want to parse it that way. We'll parse it after we get it into this. So this will just be a uh list of strings that we will further parse. Call this the range strings. Okay. So we call part one by splitting that input on commas. That's going to give us each of our ranges. And then for each of those ranges, we need to find the invalid ids. So um we're going to have the invalid ids starts off as an empty list. And then for each of our range strings, we're going to calculate that range. And um we will do this. We'll say start and end. We'll grab the range, which is just a string. we'll split it on the dash and then map that to a number. So each the number on both sides become integers and then we should now have our start and our end as integers and we can iterate with those in mind. So if we go to two and run it. Beautiful. We know how to parse the codes. So now I need a loop that goes from the start to the end. So while I is less than or equal to the end I ++. So we're going to look at every number in this range. start I less than or equal to end. Why? Why I is possibly undefined? Oh. Um, but it's not. Can I force it? Number or undefined, man. Can I do this here? No, I have to do it over here to tell it that they're not undefined. Okay. So um from the start to the end increment on each one for each one we need to check is it an invalid ID. I'm going to create a separate function called uh is invalid ID. This will take in some value which is a number. Um I will turn it into a string split it in half and see if the two equals if this the two parts equal each other. Um so we'll say value to string uh value string. We'll say the left is value string sliced from zero up to the length divided by two. And the right is from we will return does the left equal the right. So here we will say if invalid is invalid ID with I invalid ids push in I. Okay. We're going to log out our invalid ids at the end to see if we did it right. Uh we need to tell it that this is an array of number. Okay

### [1:20:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=4800s) Segment 17 (80:00 - 85:00)

moment of truth. Easy. First try. Um 11 22 99 10 22 2 Are we missing any 446 35 Cool. Add up all the invalid IDs produces that number. What do you get if you add up all the invalid IDs? Okay, so we just need to do a quick little reduce. And we should get that number. Great. So now if we run it on the initial input, we get this number and we get it right on the first try. Yes. Okay. Part two. The clerk quickly discovers that there are still invalid IDs in the ranges in your list. Maybe the young elf was doing another silly pattern as well. Now an ID is invalid if it is made only of some sequence of digits repeated at least twice. This is what I'm not prepared for. Okay, so 1 2 3 4 two times 1 2 3 1 2 3 times 1 2 1 2 1 2 5 times and 11111 times are all invalid IDs. So any repeating number not just uh the first and the second half. 111 is also invalid. 999 is also invalid. We would have caught 10 before. that before and that and that. Um, but this is repeating 824 824 and this is repeating 21 21. Okay. Yeah, Lemure is saying in the chat we should we reax is good here. I'm what I'm thinking is we rewrite this is invalid ID function. So as is works perfectly fine but if it's not invalid in this way then we'll check if it's invalid in the other way. And could we write a regular expression that checks to see if the first two digits are repeating or the first three digits are repeating? Well, I guess if it's odd length is when we need to care about this, right? Because if it's even length, checking both halves is always going to work. But if it's of odd length because four five six seven eight. Yeah. So this is length nine. So we can't split it in half and check it. So if the length is odd, we need to do something special. So we're going to write our part two. It's going to be very similar. And thank you Dynamic Voyage. Thank you very much for the um the resub. I'm not getting alerts. Why am I not getting alerts? Oh, because my overlay isn't running. Let me run it. That's my fault. I'm sorry. I'm sorry if multiple people have resubed. Let me fix that really quick. Um Okay. Alerts are on now. Um, let me see. Does Streamlabs tell me if I missed anything? It doesn't I'm going to thank everyone because I probably missed a few. Yeah. So, I missed earlier. Pablo, thank you for that resub. Thank you for everybody that's followed so far. And Dynamic Voyage, thank you for the 21-month resub. That's fantastic. Um, and Lakshman, I see you. I do remember you. Yeah, I sent you that keyboard back in the day. Um, in the middle of work just dropped by to say hello. Thank you for dropping in. Good to see you. Um, uh, Sit Synlitic is saying reax was my way as well. My brain just isn't thinking in reax. I think what I'm going to do, so first of all, uh, I duplicated the function, but now we're going to

### [1:25:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=5100s) Segment 18 (85:00 - 90:00)

call it part two. actually um it's the exact same code except it's checking for other invalid IDs, right? Yeah. So, um what I'm going to do is we'll pass in a boolean here that says part two and that is false. We'll just pass it in as true on the second run. Okay, so this is part one and this is part two. We just pass in true. And so if part two is true then we need to check for the length. So uh if part two is true and value stringlength is odd. We need to check if the len if the length is odd then um we'll check for repeating values. Excuse me. Okay. Um, and how do we check if it's odd? If the length mod 2 does not equal zero, then it's an odd length. Okay. I think we'll also just do this afterwards. So, we'll do it here. store this in a variable. Um, exclamation mark theme should get you a link to my theme. So, I'll say if um if it is not and then we just return is invalid this. Okay. So, if the left equals the right, and we already know it's invalid, don't even try doing this other stuff. But if the left doesn't equal the right, and we're check testing for part two, and the length of the string is odd, then we need our new rules. And in this case, I want to check to see is it the first number repeating or the first two numbers three numbers repeating etc. Um, and we need to do that up to up until like three repeating. You couldn't have four repeating on a string length that is nine because you'd have one extra digit. um we need some kind of like repeating variable that starts off as one. So like is there one number that is repeating? Um and we want to check for every number going up. Um, I guess we'll just say while that is less than repeat times 2 is less than the length of the string. Because if we repeat it four and the length of the string and I guess we'll just subtract one here. Length of string minus one. Um is if repeat is three 3 * 2 is 6. If the length is nine, great. If it matches, we'll break out. If repeat is four, four * 2 is eight and that is not less than length 9 - one. This seems fine. This won't run into an infinite loop, will it? Um so we'll say uh if um value string matches a new regular expression

### [1:30:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=5400s) Segment 19 (90:00 - 95:00)

which is um how many characters are we repeating? So we'll have just value to or uh part to repeat. And actually I don't think we'll even use a regular expression for this. The part to repeat is just going to be the value string. We want to slice it from zero up to what we're repeating. And then I want to see is that repeated part repeated in the string itself. So I guess we could use a regular expression for that. How would we check that? Yeah. What's up Beyonce? Uh we're doing advent of code day two part two. And a quick recap part one we needed to find any repeating number in these ranges. So like 1 22 or 1185 1185. We found all of those. But now in part two we need to not only find repeating numbers that are on both halves. We need to repi find repeating numbers that are maybe a single digit repeating or three digits repeating like here or two digits repeating. I mean, I guess I could also do um take the string length, divide it by the amount that we're repeating. So, let's say the string length is nine. We divide it by one. That gives us nine. And if we say the parts to repeat repeated that many times if that is equal to the value string itself then return true. Okay let's say it's not then uh we will increase repeat by one. So now repeat is two. If the length is nine, um, 9 divided by two is like a fractional number and you couldn't have something repeating with two that is odd, right? So, how long is this? 1 2 3 4 5 6 7 8 9 10. That would be length 10. But that would work here because if the repeat is two, the length is 10, 10 divid by two is five. We would then um repeat 21 five times to give us the input string. Um this is weird that we're not using regular expressions, but honestly, this seems fine. Let's see what this gives us. Oh, that took a long time to compute and we got the wrong number. Great. Oh, but because we did it on the input. What if we do it on the sample input? That's also the wrong number. We need this number. Um, let's log our invalid IDs. June 99. I think we're missing 824. 824 824. Oh, we got that one. Um, and that is 1 2 3 4 5 6 7. Yeah. So that's of odd length. We did get this one. Nice. But we're missing 999 and 111. Okay. Um, we'll store this in a variable.

### [1:35:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=5700s) Segment 20 (95:00 - 100:00)

Oh, yeah. I was thinking like we do need we're really only checking this for oddlength strings because if it's not oddlength strings, then well, let's not do it for that. because it could be three digit number repeated twice. No, I think odd odd is fine. Um, I want to log what we're calculating here as the repeated number. Okay, this is not running for lower numbers. Like I want to see it run for these values and it's not doing that. Why is it not doing that? Oh, if is invalid. That's why. Yeah. Okay. Great. Right. No. Oh, no, no. If it's not. So, yeah. So, if we didn't if the left and the right don't equal each other. I'm actually curious what we get for left and right when it is odd length. Yeah. So the left and the right would be um different lengths. Okay, so part two in this case would be true because we're passing it in as true. And then the length of the string, let's say if it's three, 3 mod 2 does not equal zero, right? That means it's odd. Yeah, it's one. So if the length of the string is three, then this is failing. So um well repeat is one. Repeat * 1 is two. I think maybe we don't need the minus one here. That's the issue. Yeah. So 11 22 9911 999. That was the problem. Um, oh, what are we missing? I mean, it was it's close. Don't clap just yet because we fixed one part, but we haven't fixed all of it yet. So we got 1122 999 1111 9999 1010 1185 11885 22 44 46 446 3859. We are missing 565656. We're missing this one and this one. 56 56 1 2 3 4 5 6 Oh, it's not odd. It's not of an odd length. Okay

### [1:40:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=6000s) Segment 21 (100:00 - 105:00)

we don't need to check for this. We just try it. Yeah. Easy. Just just stop checking for weird stuff. Um yeah, that's the answer. 4174379265. Okay, we got there. Um, I was just my thought was we only need to check this if it's of odd length, but that doesn't make sense because in this case the length is even, but it's just two numbers repeated three times. 56 56. Okay, let's hope for the best because I really want to be done for tonight. So, let's swap it out for the input. Give it a go. All right, we got a big old number. Yeah, nice dynamic voyage. Exactly. Just stop checking for weird stuff. Please be right. Yes. Yes, it's correct. All right, day one and two in the bag. This is tricky. I am not very practiced on algorithmic problems and um I'm not exactly happy with this solution. It's fine. I think it's cool that we didn't use regular expressions, right? We and it's not optimal. You can see that for all of that input. Look how long it takes a split second before it's done because I'm literally calculating every possible combination. Uh but thank you uh Zenibbe. Thank you for the congrats. Thank you dynamic voyage. Here here's the regular expression for comparison. Um so starts with a digit one or more digit. Um, hey Zero Turner. Good to see you, too. Uh, one, what's the slash one mean in regular expressions? End of string. So, find starts with one or more digit. Does this mean repeating first group? Oh, that's great. So, yeah, a group is the parenthesis. So, find one or more digit and then that group repeated one or more times. End of input. Okay. Uh let's see how fast it runs with just this regular expression. Well, that's literally the That's literally all you have to do, isn't it? So, we just return value. ring match. Uh this and match returns an array. If I do exec, we'll just return true or false. Um exec also returns an array. Test. Test is what we want. Thank you everyone. Um test slightly faster. That's cool. I've never used that in a regular expression before. So, that's good to know. Basically, you can reference the capture. group within the regular expression itself. Very nice. Cool. Um yeah, put the number in a set and if the length of the set is half the original, then we know the number is repeated. Put the digits in the number in a set. Yeah, thank you. And again, thank you dynamic. We figured it out. You can even name it. Oh, I have seen uh named groups in JavaScript regular expressions cuz that's a newer feature in uh regular expressions in JavaScript is you can have multiple named groups. You used to only be able to do one named group. Um, so

### [1:45:00](https://www.youtube.com/watch?v=TD_FwAIEpqI&t=6300s) Segment 22 (105:00 - 107:00)

yeah. So like title, you can name the group. Um, but it's only a recent addition that you can have the same group more than once. I think that's the new thing. And part one was the same but with plus without the plus at the end. Oh yeah, because this is one or more, but in part one you just want this group to match itself. Oh, that's fascinating. So, I mean, I'm pretty even though this is a more roundabout solution, I'm still pretty proud of it because I figured it out, you know, like I basically calculated all of the repeats. So, what do we want to repeat? How many times repeat it? Let's try every possible repetition and see if it's in there. Yeah, it's not horrible. Not bad. Okay, I'm going to get out of here. But, uh, I do plan on streaming again on probably Thursday night. So, Thursday night we'll do day three and four is the plan. Uh, Desu did it in integer maths. Fascinating. Yeah. Good night everyone. Thanks for tuning in. Um, and if you're new here, I appreciate you. Thanks for dropping in. I also have a YouTube. You can check me out there. And I also um I have two YouTube channels. So, I have my personal YouTube channel called Coding Garden and then I also upload videos over on Syntax. Um, I think maybe Thursday night maybe I might go live on Syntax to solve these. We'll see. Syntax has a uh a Twitch as well if you want to follow there. Okay, but that's all I got. Thanks everybody for dropping in. Have a wonderful night. Stick around. We will raid someone. Um, yeah, we'll raid somebody. So, be well and I'll see you soon wherever you are in the world. Have a wonderful morning, afternoon, evening, or night. And until next time, here's this

---
*Источник: https://ekstraktznaniy.ru/video/37417*