Advent of Code 2025 | Day 3 and 4 | JavaScript / TypeScript

Advent of Code 2025 | Day 3 and 4 | JavaScript / TypeScript

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI

Оглавление (23 сегментов)

Segment 1 (00:00 - 05:00)

Hello friends, welcome to Coding Garden. Uh, welcome to another late night stream. We are going to be solving Advent of Code today. That's the plan. There's only a few of you in here and that's okay cuz it's pretty late. Um, but good to see you Dynamic Voyage and Funny Boy and SPD and uh, Mark Boots. Thank you for the tier one. And hello SMC. May I boge um a very a good very early morning from the Netherlands. Nice. Um and Mark boots with the 24 months. Thank you very much for that tier one resub. So the plan for today is to solve advent of code day three and four. So day four unlocks in 26 minutes and I have not seen read or tried day three yet. So, the plan is we're going to give uh day three a try and um we'll see how long it takes me and then when we're done with that, ideally uh day four will be unlocked and we'll solve that one as well. That's the plan anyways. And what's up, Iito? Good to see you. Um oh yeah, they actually have a I think the Sentry Discord has a Debug December channel, but I'm going to be doing Debbug December next week. I'll probably do some live streams. I think I'll probably do a live stream on Friday. Probably a during the day live stream where I do like the first five debug December challenges cuz I haven't even looked at those challenges yet. SPDN says day three isn't too hard. That's I'm glad to hear that cuz day one and two took me like two hours to solve in total. Uh but debug December, Debbug December is a coding challenge thing from Sentry and uh instead of solving al algorithmic problems, they give you code that has a bug in it or has an issue with it and you need to fix the bug or fix the issue. So this is another fun advent of code style thing if you haven't heard of it. Yeah, I heard about the uh the React vulnerability is crazy out there. the uh React server components like everything is vulnerable. Um yeah, I heard about it. It's crazy. Uh it was cool to see that. So in the world of security, there's this idea of uh responsible disclosure and that is like a security researcher, let's say they find a bug or an exploit or vulnerability or something like that. um instead of telling the whole world about it, they first tell the vendor, in this case Meta or React, and let them know, hey, you need to fix this and uh before telling the world that this thing exists. And so they responsibly disclosed it. And then from there, there was a uh an effort from all of the hosting providers around the web. So, Versel, Cloudflare, Netlefi, Dino, like Dino Deploy, um there are several others. All of them implemented a like router routing network level fix uh that prevents the vulnerability from being exploited. So if you have your apps deployed on any one of those major providers, oh I think um was it like railway, most of the major providers were alerted of this uh before anyone else knew about it and they implemented fixes at the network level. So if they see traffic coming in that um has particular headers in it, they automatically block it. So, it's nice to know that if your React code if your yeah if your React server code is deployed to any one of those providers, you're automatically um not vulnerable even if you don't upgrade to the latest version, but you should version. So, that's a thing. Uh but anybody that was like self-hosting or not on any of the major providers, uh they are still vulnerable. Yeah. Uh Virtual Treehouse, what's up? Uh holiday hack challenge through Sans is also cool if you're trying to up your security game. That could be fun. I'm just I just don't have much time. I'm just so busy. I'll look it up though. Uh free high quality super fun hands-on cyber security challenges. It seems fun, but I'm just I have so much other stuff going on. But yeah, that React vulnerability absolutely crazy. Uh Ana, good to see you. And everybody codes as AOC but for November. Is this a similar thing like security but in November? There's too much stuff going on. Okay. Uh but I'm going to get right into it. I'm going to start solving some challenges or start solving day three. So, if you're not familiar with Advent of Code, they release a new

Segment 2 (05:00 - 10:00)

challenge every single day in the month of December. This year, they're only doing 12 days. Usually, they do 25 days. Um, but today is day four, but we're going to do day three. Yeah, not really an advent calendar. It's true, I guess, cuz it's November, but yeah, good to see you, uh, Duprius. All right, let's jump right in. Day three. Uh, I'm going to go ahead and get my local code set up. Make a directory for day three. Go open up VS Code in here. Create a new directory. Well, yeah, we have the directory. We'll create our file. I could probably create a script that does all of this for me. So, we'll have our sample input. ext and our input. ext. Doesn't take me that long to do it by hand. Um, guessing this is our sample input. A says Windows 11 woke me up due to a restart after auto applying an update. Did you hear like the Windows sound and that woke you up? It's like an alarm. That's crazy. Uh, and then we have our puzzle input here. Okay. Because of the way this stuff looks, I have a hunch this is going to be a grid of some kind. And we're going to have like XY coordinates in here. That's my hunch. We'll see. I don't I'm not usually a fan of that kind of stuff, but we'll see. Absolute wall of text. Yes, it is. Okay. Um [snorts] I'm always hesitant to just jump into solving versus like reading the problem. It's okay. Okay, Mark Boot said, "I have a bad hunch. " Okay, so but I am going to stop looking at chat because I don't want any hints. I'm going to solve this with my own brain and I'm going to read it just for the sake of it for anybody at home that is using this as an ASMR stream. Day three lobby. You descend a short staircase under the surprisingly vast lobby and are quickly cleared by the security checkpoint. When you get to the main elevators, however, you discover that each one has a red light above it. They're all offline. Sorry about that. at enough apologize apologizes as she tinkers with the nearby control panel. Some kind of electrical surge seems to have fried them. I'll try to get them online soon. You explain the need to get further underground. Well, you could at least take the escalator down to the printing department. Not that you'd get much further than without than that without the elevators working. That is, you could if the ele escalator weren't also offline. But don't worry, it's not fried. It just needs power. Maybe you can get it running while I keep working on the elevators. [snorts] There are batteries nearby that can simply that can supply emergency power to the escalator for just such an occasion. The batteries are each labeled with their joltage. This is a call back to uh Advent of Code 2020 uh rating a value from 1 to 9. You make note of their joltage ratings your puzzle input. So each one of these is a joltage rating. The batteries are arranged into banks. Each line of digits in your input corresponds to a single bank of batteries within each bank. So each line is a bank. So one bank, two bank, three bank, four bank. Okay. Within each bank, you need to turn on exactly two batteries. The joltage that the bank produces is equal to the number formed by the digits on the batteries you've turned on. For example, if you have a bank like 1 2 3 4 5 and you turn on batteries 2 and four, the bank would produce 24 jolts. You cannot rearrange the batteries. You'll need to find the largest possible joltage each bank can produce. In the above example, you can make the largest joltage possible 98 by turning on the first two batteries. Oh, just find the two largest numbers. That's easy. Okay. So, in this one, um, turning on the batteries labeled eight and nine producing 89 in here. 78. Okay. And it's just find the two largest numbers and they're always going to be in the order from left to right. It seems the total output jolt is the sum of the maximum jolt from each bank. So in this example, the total output joltage would be 98 89 78 92 357. Okay. Yeah, that's easy peasy. Um let me copy our FS read codes. We'll grab that. Import FS. We will create our part one function.

Segment 3 (10:00 - 15:00)

It's going to take in the battery banks which is going to be an array of string. And then we're going to call part one with input split on the new line because each line is a different battery bank. And then for each battery bank, um, we're going to need the sum. Start that off at zero. I could do a reduce. I'm not going to do a reduce. So, we're just going to iterate over the battery banks. So, for each bank, um, we want to find the two largest numbers from, and again, it has to be from left to right. Um, I'm tempted to just spread it into math. mmax, but that wouldn't give it to us left to right. Well, we we'll do it the oldfashioned way. Um let's say we have our um yeah all I was thinking is like iterate from left to right finding the two largest numbers. Um, so yeah, I'm just going to have a equal to um and b equal to negative infinity. And then we take the bank, split it. That's going to give us each individual number. And then we'll iterate over each of the values in the bank. And I want it to be a number. So I'm just going to um map this into the number function. So each value here should be a number. Uh for now, I'm just going to log it out, make sure we haven't gotten too far without anything working, and give it a try. So if we go into 03 run file, we get a bunch of numbers. That's what we want to see. Um but we're going to run it on the sample input first. Okay. So for each value, we'll say um if value is greater than a then a is equal to value. else if value is greater than B equals that value. Um, and then we'll log out A and B. Is that it? Yeah, I know how to code. So, we've got 98. Um, no. This one should be 89, which is wrong. 78, which is also wrong. And this one should be 92, which is also wrong. Oh, okay. So, it's a little bit different. So, find, we have to find the largest one and then the largest one after that. So, do we do it in two passes? Yeah, I think passes. That's going to be my algorithm is like we do one pass to find the largest number and then we keep track of its index and then we find the largest number after that index. That's what we have to do. Okay. Um so um let's actually throw this into a variable. There's an obvious says there's an algorithm to do it in one pass. I think we can pass twice. So, it's not nested. It's still technically order in, but it's going to be two in, but I shouldn't be looking at chat because I got to do this myself. Okay. So, um we're going to call this the values and instead of a for each um I'm going to have a for loop while I is less than values. length I ++ um and we want a variable for the first and second index. Okay, so we're going to iterate the first time and we'll say if values at I is less than or sorry is greater than um

Segment 4 (15:00 - 20:00)

values at first index possibly undefined number array. I mean that's fine. I don't care if it's undefined. Um then the first index is going to be negative 1 or sorry it's going to be I cool. Um then we do it again starting from the first index plus one and we do it for the second index instead. Cool. The one edge case is if um we actually I guess the edge case here is we for the first loop we just will go to length um and we need well i is greater than no well i is less than length minus one. So that will not go to the last one, which means it'll find the largest one, which is second to last. And then this one will potentially already be the last one, and then it'll just set it. Uh, seems fine. Let's try it. So if we grab the values at uh first index and values at second index, did it work? Great. Undefined. Um, I guess we'll just start these off as zero and one. There we go. That's what we needed. Okay. So, we got 98 89 78 92. We done it. So um the overall value is just going to be the first one * 10 plus the second one. Uh joltage is this time 10 plus this. Cool. And then we need the overall sum. So we'll just say sum plus equal that joltage and then we'll log out the sum at the end. 357 and we were looking for 357. Amazing. So if we pass in the actual puzzle input, the value that we get is 17077. Is it right on the first try? No, it's not. It's too high. Dang it. Um, wrong. Too high. Okay, let's log out what our joltage is when we're running it on the main input. Oh, what's up, Mendoza? Good to see you. — [sighs and gasps] — Okay. So, there's nothing weird in the output here, but it's possible we have made a mistake in our assumptions. Um, because we're basically always starting off first index + one. I did get the example correct. I got 357. Um yeah. So if we run it on the sample tree 57 um and let's just double check each of our values was right. So like 98, 89, 78, 92. Cool. SP says, "I'm already predicting what you'll think of part two. " Dang it, that doesn't sound good. And what's up, BJ Sebastian? Uh, finally able to pop into your stream. Are you doing a private leaderboard? We do have one. Yeah. Uh I'll link it to you. The thing is I'm not competitive in it. Uh because all basically uh I started this leaderboard

Segment 5 (20:00 - 25:00)

years ago and so all the like Larry is like so good at this. Like some of the people on my leaderboard are so good. Uh, so it's very hard to be competitive on my leaderboard unless you show up right at midnight Eastern and are solving it as fast as possible. Uh, cuz even me, I'm in 38th place on my own leaderboard. Yeah. Um, okay. What could be going wrong? I think in order to [snorts] debug this, I'm going to log the values and the um and the voltage. So that way we can just scan it line by line. It should be fairly easy to scan line by line um to find the two the two largest um [clears throat] okay it's not going to be that easy but like in this example we're scanning. We find the number nine. Great. We're scanning. Cool. And then after that, the number eight is here. Is there any number larger than eight? No. What's up, Moose? Um, if you go to adventofcode. com, we are doing day number three on Advent of Code. Basically, we're given a bunch of lines. For each line, we need to find the two largest numbers in that line from left to right. Um, so in this line it's 98. In this it's 89. In this it's 78. And in this one it's 92. So you find the largest number from left to right. And then after that you find the largest number after the one that you just found. Yeah, I have no idea how to debug this cuz um I believe it's just two digits. Yes. So, you need to turn on exactly two batteries. So my second loop, so my first loop starts at zero. Well, technically the first index. And we're kind of just assuming, we're going to assume that the first two values are the um that um oh, I need to overwrite the second index before the next loop runs. Before the next group runs, second index needs to be first index plus one. That's my issue. Yeah, because before it was going back to the beginning of the array. That's that's the that's it. Okay. That's crazy. We were off by three. We're off by three. Okay. This that's the answer. Come on. There we go. Okay. Part two. All right. The escalator doesn't move. The elf explains that it probably needs more joltage to overcome the static friction of the system and hits the big red joltage limit safety override button. You lose count of the number of times she needs to confirm yes, I'm sure. And decorate the lobby a bit while you wait. Now you need to make the largest jolt by turning on exactly 12 batteries within each bank. uh the dotage output for the bank is still the number formed by the digits of the batteries. Consider again the following example. Um okay we can do this and by basically turning this into a function that accepts the starting index and the number of values that we need. Okay, but this is very doable very doable. So, what I'm going to do is just copy our part one function and call it part two. And uh we'll call it part two instead. But this bit of logic here needs to be refactored into a function. Um so we'll say something like find next index. We'll get the start index which is a number and then we'll always split the values first. We'll throw this loop in here.

Segment 6 (25:00 - 30:00)

So it goes from the start index, we'll say plus one. Um I'm going to comment everything else out so we don't have any uh weird variable references. So, um, and then let's also have the bound index be the start index and then we'll return the found index. So now we'll have uh let's call it the digits and this is going to be an array of number and we'll just say while the digits length is less than 12 um we'll say the next index will be find next index and Um, we'll call this the current index. Set it to zero. That's what we will overwrite. We pass that in and then we push that digit into it. So, values at current index. Um, yes, this could be undefined, but I don't think it will be. And then after all that we should get our digits and we'll try it on the sample input first. All right, first try. No, that's very wrong. Um, let's see. We want to do current index plus one, I think, I guess, because that's going to be our starting index. We'll do start index minus one to be the one before it, I guess. Maybe. Nope. Okay. Are we reassigning? We are reassigning current index. Oh, we don't want to do values. length. length minus one. We definitely want to go. We do. And then um Oh, this is going to get tricky. Okay. Uh let let's just see if we can get um the first 11 digits. So let's say we're taking a look at this one. The first digit we're going to find is nine. That's going to be the largest number. So we have our current index set off start off starting off at zero. Um we call find next index with one. So the start index is going to be one and our found zero. So if values at one is greater than values at zero overwrite it and then we return it. Um we need to push it in before we overwrite it. Okay. And this does need to be start index. If we look at our previous function, the next one is first index + one. So I think we do want this to be start index plus one. And then we can just assign this to be the start index. Is that the answer? Yes. Sort of. We got a weird one here, but let's we're getting closer. Okay. Uh so for the first one 987654 32111. Okay. So let's go to 12 like we were doing.

Segment 7 (30:00 - 35:00)

So we then get uh 987 65432111. Beautiful. The next one is 81111 and the last number should be nine. Okay, I think what we'll do is just find the first 11 and then after this while loop, if the current index is the length minus one, just set it to the last index. Um, I guess we Let's Let's try it with 12 going all the way to the end. Does that do it? No. Um, okay. So, that gets us closer. Just doesn't get the last value. So, after this, we'll say if the current index um is equal to values. length length minus one, meaning the last largest value was the second to last one. Then we'll just push in the last value. Otherwise we find the current index. What does that do for us? That does not give us the right last value on this one. Let's take a look at um the current index right before the last one runs. So our length is or 0 1 2 3 4 5 6 7 8 9 10 11. Yeah. Would it be length minus one or would it just be length? How long are these inputs? They're length 15. So because this only goes to values. length length minus one. My thought is um if the current index is 14, which would be 15 minus one, then we just push that last one in. Otherwise, we still find the next one and then push it in. Uh the issue is we're getting the wrong last value here. Like it doesn't find the nine here. Oh, I was about to say like is it not finding the largest value? Um, here. Yeah, I'm I might I'm kind of I might rethink my algorithm here. I thought it would be easy enough to just rework our solution for the first one, but now that I'm thinking about it, like what if we I was thinking we do like a math dot max to find the digit and then we slice the list after the index of that digit and then do a math. mmax on that again. — [snorts] [sighs] — I'm going to go down that path because this is not working. This is not working and it's messy. We have too many plus ones, minus ones. I don't like it. So that this is what I'm going to try. Okay. Um so let's say we have our digits. This is an array of numbers.

Segment 8 (35:00 - 40:00)

And then uh while digits. length length is less than 12. Keep doing the thing. Um we'll have a function that says uh find next max. It takes in uh remaining which is a list of number and then um I'm going to do it the lazy way in terms of we'll just say the max is uh math domax of all of the numbers and then The uh max index is just uh remaining. find find index which will find the first index. So the value is equal to at max and then we will return remaining sliced with everything except for that max index. No, I guess we'll just return both. We'll return the return. We'll do this outside of this. So return max and max index. So we'll then say this is find next max index with the values. We will then say digits. push that max And uh we are going to have a new variable that we can overwrite. Um I'm going to call it remaining. It's going to be confusing because we're shadowing the other variable, but this will be fine. So make a copy of the values array. Use it there. push in the max and then overwrite it. So we'll say uh remaining is going to be remaining slice with max index. So this is a lot easier to read. Basically just find the max, chop off everything or chop off everything before that max, find the max, chop off everything, etc. So, this should do it. Let's see. Negative infinity. Wonderful. So, uh it worked for this one. Um let's see, I think. Yeah. So, it worked for number one, but for the second one, oh, we can't look at the last digit. That's the main issue. We can't because it looked at the last digit and then it couldn't find anything larger than it. So, we have to do everything except the last digit. Um, I think if we do one and then just look for a length of 11, does values. s slice work with negative indices? So everything except the last value I guess we could do it here. We could do remaining slice with everything except the last value and only find 11 of them. Is it like this? Okay, closer. Um, cool. So, that got us there. Then we need to just look at the last digit. I mean, what if we do this? Will we get an infinite loop? No. So, this one works. I'm wondering if we just do the logic like we did on the last one. I want to pull the variables out so I can use them afterwards.

Segment 9 (40:00 - 45:00)

And then I think I can do this. So that will override them. So they'll still exist afterwards. And then here we'll say um if max index is equal to values. length minus one. If the length is 15 and the max I guess it would be minus two, then we will just push in. Maybe that's what I got wrong last time. It was length minus two. We'll push in um values at max index. Otherwise, we do this again. Last value is killing me here. Uh thank you, STY. Thank you for the resub. I'm really curious. Well, we're going to do some debugging. Um, oh, I think I need I I'll have to put this in a variable. Let remaining without last. I think we need to do this and then we'll use it in both places. Okay, that still doesn't fix it. Um, let's log it. Yeah, I'm curious about this one. So, yeah, it's all of it remaining without last. But then, um, let's also log what happens here. because um I don't know which one of these is executing. Oh, the issue is uh we shouldn't slice it when we call it the last time. Um maybe we pass in uh a variable. So like slice last is false. That's the issue. We were doing it every time. Okay, that should give us nine. It fixed it here. Wonderful. Okay, we're getting closer. Um, okay. So, we got the first two right. [snorts] The next one is completely wrong. Um. Oh, wait. Would it be crazy to just reverse the list and then find the max and then reverse the answer because then it would find eight and then seven. Because that's the issue is like right now when it finds the max um finds eight and then there's nothing after it. So we could have a guard that says find the max uh where there are enough remaining digits

Segment 10 (45:00 - 50:00)

to yeah where the index is less than the number of remaining digits to give us 12 total values. Crazy. Okay, here's my thought though. What if we reverse it? use our existing method and then uh reverse it again to get the final result, would that actually uh fix it? So if we do this reverse and then after we get all of our digits, reverse them again. No, it only worked for this one. Okay, but uh we know we know the issue now. Uh the issue is we need to find the first max who has enough values after it to get the remaining amount. Oh, this is crazy. This is crazy, right? Yeah, I feel like we're also solving a different problem here. Like I'm, you know, I'm finding the max, but this is almost like given a set of digits, find the maximum combined digits. Yeah. Itakito says you're solving part three. What does that mean? I mean I'm in day three part two. Oh yeah. Day three. I am solving day three. Yes. Yeah. So it's working for these two. But we need some conditions to check for finding the max on the next one. So um yeah I get but what would that logic look like? So find the maximum who has 12 at least 12 values after it. Yeah, it's almost like we need to slice the array, slice off the end here as we iterate each time. Okay, I think we'll pass in one more variable to our function. Um, which is the um, and Mr. Devk, thank you for the primary sub. I'm going to say the last index, which is a number. Okay. So, um here the last index is just going to be um the previous max index. No, it's just going to be the length values. length minus one. No, values. length because then because it's exclusive. But here it's going to be um the length of the values minus the current number of digits that we have. Not that we have minus 12 because that's what we're looking for minus the number that we've already found I think. And [snorts] so that last index will then give us a value that we pass into uh slice. So we'll say slice and then our last index. I think this is going to get us closer, but we might have to tweak the values. Okay. Well, it still works for the first two, which is good. But, um, my idea here is when this runs the first time for, [snorts] um

Segment 11 (50:00 - 55:00)

in this example, uh, we need to Oh, and it's not going to be is it going to be 12? will be 11. Basically, 1 2 3 4 5 6 7 8 9 10 11. We need to make sure there are at least 11 values to the right. So that would be length minus 11. So 15 minus 11 gives us four. So 0 1 2 3 4. This would be the last index it would be looking for in the max there. So total number of values minus 11 minus the number of digits we've found so far. Uh, I am curious what we get here though. Okay, let's look at this one. remaining. So 2 3 4. Yeah. Remaining without the last is wrong. That's too many values. We want to see just that array right there. 2 3 42. Um, let's also log the last index. And while I'm thinking about it, we can isolate our tests. Like we want to just test for this value specifically. So, um, down here, I'm just going to pass in a single value. Is that the one we're looking for? Oh, yeah. Not input. split. Yes. Okay. last index four remaining without last zero is okay. So we're passing in the right value. It's just not slicing it in the right way. Oh, I did it on the wrong one. Okay. So 4 3 4 last index four last index. Okay. So it's going in the wrong direction. Um do we add it instead of subtracting digits. length? Yes, sort of. Um, so 2 3 4 278 is um Oh, no, that's the array. But the values we're finding are four. The next iteration. This remaining without last is wrong. The starting index is wrong here. Like we shouldn't have two three. Nice dynamic voyage. Who's to say that the answer isn't infinity? Uh it tells me what the answer should be

Segment 12 (55:00 - 60:00)

which is this here. Um okay, let's double check. So we find the next max we overwrite remaining with max index + one. So we should be getting rid of the previous value values that were in there, right? See what this does. 234 2342 34278 34278. Okay, that's right. And it should be 2 3 4278. Yep. Okay. But our length is definitely not right. I think that has to be digits. length. Let's also log uh what digits we have. So we look at what the answer should be. Um 434 234 and we have 443. Oh, when we find the where are we doing find index? This index is completely wrong because we're changing the length of the array. We need to find that index in the original list. That's our main issue here. I think Let's try it. So this should be remaining not remaining without last. Four. I'm at a loss here. I am at a loss. I'm tempted to do the reverse solution again. So like start searching from the right. Why did we abandon I think yeah I guess we abandoned this solution because of all the pluses and the minuses. But now that we're changing the length of the array, um, it's not good. All right, I'm going to go back in time. I'm just going to command Z a bunch. Actually, I'm going to I'll save this solution, but I am going to command Z back to before we started messing around with slicing the list that we were searching cuz slicing the list changes the index. That's the main issue or one of the main issues.

Segment 13 (60:00 - 65:00)

Okay, so we [clears throat] should be back to getting the first two right. Great. Um, I'm going to put this codes down here just in case we need it at some point. Uh, slicing array to only find within available length is what this does. But um my thought of reversing and then reversing again I feel like it'll work right because um we're always I guess going to be starting from the right. No, because on this one, we wouldn't start from the right. Cuz if you start from the right, the first the the um largest one you're going to find is going to be nine, which is this. And then, yeah, that doesn't work. I'm just going to think for a second because I've just been talking for what, an hour now. So, I'm just going to sit here and think. I'm definitely stumped. So, I'm going to think for a second. Okay. [clears throat] Is this a combin combinatorial thing? Like can we find every 12digit number in this number from left to right and return the largest one? That's also guess what we're trying to solve here. But uh would that be too much calculation? Um cuz I could see it happening like find this 12-digit number and then this 12-digit number um it's always 12-digit numbers from left to right. Yeah, Desu says yes, but not within a lifetime of the universe. I don't know though. I mean, it's it's a finite length. What's up? Uh, Kalisiki, good to see you. [snorts] Yeah, that's not infinite. Right. Um, yeah. So, that we are we are trying to write some code to solve this problem. Yeah. I guess I just need a different algorithm, right? Like my algorithm only works if there are enough numbers after the first max one that was found, right? Um that's the only way my algorithm works in this scenario. [clears throat] On the first pass, we find the number eight and then we're done because that we we can't. So, I mean, that's what I was getting at with my previous solution, um, that I just undid, which was, okay, find the maximum one without including the rest because we're going to need those. Um, huh. I I'm wondering if we could also like short try to do some like short circuiting, right? So if [snorts] we look at [clears throat]

Segment 14 (65:00 - 70:00)

um all the values before the last 11. So yeah, this is okay. So a different algorithm here. Let me let me pseudo code this. Um so it's going to be something like um so we need 12 total digits in an in length uh number. So on the first pass we'll find the largest digit in the first in minus 11 digits, right? So, if it's length 20, um, we subtract 11 from 20, which gives us nine and we want to find the maximum value in the first nine. Great. Um, cool. After that we then find the largest digit within. So starting at after the previous found digit and up to n minus 10. Oh in the n minus 10 remaining digits. Is that right? Let's write an example. So um in this example, we're trying to find the largest number here. So [snorts] find the largest digit in the first n minus 11 digits. Um this means we go 1 2 3 4 5 6 7 8 9 10 11. So we're finding in here. Okay, this gives us four whose index is three. On the next iteration, we want to find the largest digit um in the remaining n minus 10 digits. So a in n minus 10 digits after the previous. So, we're going to say find in here 1 2 3 4 5 6 7 8 9 10. And this gives us three, which is at index two in the remaining. Is three the next number we were looking for? It was. Okay. So, our algorithm is working. Um, and then we repeat. So find the largest digit after the previous found digit and in minus 9 remaining digits. Um so after the three would be here and we do 1 2 3 4 5 6 7 8 9 and it's literally just one. It's just that cool. um and then we could almost like short circuit it. So if the number of remaining digits is equal, just return it. Um yeah, because if we did this again, it would basically say um wait, did I cut it? I didn't mean to cut it here. Um then 1 2 3 4 5 6 7 8 9. Uh I guess here we would short circuit if the number of remaining digits equals n minus 9 return all the remaining digits in order. Okay. Um is this algorithm good? Like would it work um in the case of this? So find the largest digit in the first n minus 11 digits. So we would go 1 2 3 4

Segment 15 (70:00 - 75:00)

5 6 7 8 9 10 11. The largest digit here is 8. Okay. And then we would say find the largest digit in 1 2 3 4 5 6 7 8 9 10. The largest is uh I think it said eight, but it was nine. Okay, great. And then 1 2 3 4 5 6 7 8 and then it's seven. Yeah. And the rest of the array is going to be longer in this case because uh they're basically in descending order all the way until you get to here. This seems good. This seems like a decent algorithm. Let's try it. Let's further comment out one more bad try. Um actually I I'm going to com I'm going to comment out all of this and then um just rewrite the function from scratch. Okay. So [snorts] here is our algorithm. So we need 12 total digits in an nlength number. Find the largest digit in n minus 11 digits. So uh we'll say uh needed is 12 and uh we want to find the largest digit in n minus 11. So I think if we do a for loop starts off at zero or I guess the last found index well I is less than the length minus how many are needed and then increment I on each iteration. Um and so we'll say uh let next max and I think we also could have just like an outer we'll have an outer loop that goes from 12 and then starts decreasing because that's what we noticed. in our pseudo code here it goes like 12 11 10 9 um so we'll have an outer loop uh we will say uh I guess needed that starts off as um 11 well needed is greater than or equal to zero needed minus minus. And then we'll say the uh current max index starts off as zero. And we'll start this off here. And we'll say and we also need the current max which will just be values at zero. And we will say um if the current max is if values at the I is greater than current max index or um I guess we just need to keep track of current max index is greater than values at current max index. We'll say the current max index is I. When this loop is done, we have our digit. So we will say um digits push values at current max index. And we will set current max index to itself + one. Um

Segment 16 (75:00 - 80:00)

this seems fine and very simple. Let's see if this works. Um, we could have messed something up. I If anything, it's going to be we're messing up our for loop variables. I'm a genius. I have a straight G. Look how fast that was, too. [gasps] Oh my goodness. Okay. Um, I'm pretty sure we got this right. Uh, let's just run it a few times so I can see the output down here. Okay. 987654321111 811119 uh 434234234278911112111. Is anybody watching? Is anybody witnessing my genius here? I can't believe this. [gasps] Uh really I just followed my own advice because um for years I did on code wars I uh I always tell people like solve it with pseudo code first and uh we did and we came up with like a very optimal solution. Holy crap. Okay. So we'll just confirm that um uh so we need um join those digits together, convert it into a number and add it to the sum. And then after all of this, log the sum 31219 77869. Yeah. I'm a genius. Okay, here's our answer. First try. Please work first try cuz I have no idea what we could do different. Yes, man. That only took me an hour. Jeez. Cool. But anybody out there watching, this is the kind of perseverance you need in the age of AI. Um, how many of you would have just stopped at this point and just asked AI instead of figuring it out? Probably a lot of you. Um, and the thing is this is an orderin solution. It's 12 in or 11 in, but it's still not even though we have a nested loop, this is a fixed value. So this is not an n squared solution. Um yeah it's still again it's still like 12 in but you know — [snorts] — uh no way debugging my own code is never as bad as debugging a code. Yeah, but I so when I streamed on um Monday night um a lot of people were like uh I couldn't figure out part two, so I just asked chatgbt. Yeah. I mean, so similarly like me actually writing out the pseudo code here is what made me solve it. There's probably a there's probably a again there's even better solutions than this, but this worked. Okay. Uh this is Java JavaScript or TypeScript. Uh we have a few type annotations but we're using TypeScript here which is why TypeScript is yelling at us about this because um it's possible that I values at I is undefined but we know it's not because of the uh range we're putting on our for loop here. So in Typescript you can use this to say hey I know that it's not going to be undefined. All right, that took way too long. Let's move on to [sighs and gasps] day four and hope and hope that it doesn't suck up all my time. Uh let me get a directory ready for day four. Got our input, sample input, our file. Yeah, [snorts] I mean that's that's a really that's a great point. I mean, this is the a sign of the times, though, because um I feel like there are a lot of students who might have been assigned to do Advent of Code and they're just probably using AI. But yeah, I completely agree. It's

Segment 17 (80:00 - 85:00)

like asking somebody else to go to the gym for you or somebody else to in my opinion, somebody else to have the fun for you. You don't get to have the fun. And I'm learning in the process. is like I mean you saw me struggle for an hour to figure this out but I got there and I learned from my mistakes. I think on this next one I'm going to write some pseudo code, right? Like that's I learned that I'm I'm probably going to be better at solving it if I really think about it before throwing a bunch of random code at it. Wasn't random code, but still. Okay. Uh, let me grab our um our sample input and then we'll have our actual puzzle input. We'll copy our fs read have a function for part one and let's figure out what we got to do. Okay, you ride the escalator down to the printing department. They're clearly getting ready for Christmas. They have lots of large rolls of paper everywhere, and there's even a massive printer in the corner to handle the really big print jobs. Decorating here will be easy. They can make their own decorations. What you really need is a way to get further into the North Pole base while the elevators are offline. Actually, maybe we can help with that. One of the elves replies when you ask for help. We're pretty sure there's a cafeteria on the other side of the back wall. If we could break through the wall, you'd be able to keep moving. It's too bad all of our forklifts are so busy moving these rolls of paper around. If you can optimize the work the forklifts are doing, maybe they would have time to spare to break through the wall. The rolls of paper, the at signs are arranged in a large grid. The elves even have a helpful diagram indicating where everything is located. Okay, so this is like an XY grid. Everywhere there's an at sign is a roll of paper. The forklifts can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. If you can figure out which rolls of paper the forklifts can access, they'll spend less time looking and more time breaking down the wall to the cafeteria. In this example, there are 13 rolls of paper that can be accessed by a forklift marked with an X. All right. So, the condition is um forklifts can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. So, when we're talking about adjacent positions, it's going to be 1 2 3 4 5 6 7 8. So, three below it, three on top of it, and two to the right of it. So if there are fewer than four in the adjacent positions, then we can't access it. So we mark it with an X. Okay, this should be easy enough. I say that, but the idea is we'll read in our input um essentially like an XY grid. So each one of these has a location of uh an X location and a Y location. So after we've read it in, we know all of their X locations and Y locations. Then for each uh item with an at sign, we'll need a function that will grab all of the neighbors, count them up. If the neighbors is less than four, we uh keep track of that location and that it's included in our index um included in the total number of rules. Uh now, I did say I was going to use pseudo code, but me talking through the problem just now is my pseudo code. — I'm going to now I'm just going to code it. Um, so we will just directly pass in our input here. And this is going to be um what do we call this? Call this the diagram which is just a string. And then we'll further parse it. So we get this big old string in. And I want to turn this into a grid of like XY. I guess I did say we were going to parse it, but if we say the grid is uh this split on new lines. Um then this will give us an array of strings. So we'll have one array, two arrays, three arrays. And we should be able to say like if x is zero and y is zero then if we say grid

Segment 18 (85:00 - 90:00)

at zero oh grid at y at x will give us a given value. um possibly undefined. I Yeah, that's that makes sense. But why? Because this list of strings is going to be 0 1 2 3 4. That's our y-value. And then the index into the string itself, which is the second brackets here, is the um the column. So that's how we will be able to access these things. Um, and we kind of we just need to iterate over the rows in the grid. So I'll say uh well I'll just have a nested for loop. So our first for loop will be for the columns. So column starts off as zero while I is less than or while the column is less than um grid dot grid at0. length. I'm going to put this in a variable called width column++. And then we'll also be looking at the rows. And so rows start off at zero. Well, row is less than grid. length because that gives us the total number of rows. Row++. So we now have a way of getting access to every uh row and column in the grid like so. And we will only look at it if it is an at sign. Um so we'll say if value and this can be a const um is equal to the at sign then that means we want to check uh for all of its neighbors. Yeah. Uh so uh slide charge is saying if I'm not mistaken this is a 2D array problem and the solution could use nested for loops. Yeah. So that's basically what we ended up with. Um [snorts] one is iterating over all the rows. The one for loop iterates next columns. And so for each given cell, actually let's call this a cell. If it's an at sign, we want to check we want to count the number of neighbors that um are an at sign. So we'll put it into a function. We'll say uh neighbors is count neighbors. We'll pass in the grid. row and we'll pass in the column. So given some row and column, count all of the neighbors around it and return that value. Um we'll have the total start off as zero. And here we'll say if neighbors is less than four increase the total and log the total at the end. Now let's double check if it's less than or equal to four or less than four. Um the forklift can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. Fewer than four makes me think less than, not less than or equal to. Okay, now the hard part of this solution is implementing count numbers, but it's literally just going to be accessing everything around that given row and column. So we have our function count neighbors takes in the grid which is a string array takes in the row which is a number takes in the column and um we'll have the total start off as zero and then return the total and then we need to check every single location. And so the uh top left, the top, the top right, the left, the right, the bottom left, the bottom

Segment 19 (90:00 - 95:00)

and the bottom right. So these are the one, two, three, four, five, six, seven, eight locations around a given cell that could be an at sign. So we will say uh if the grid the top left is going to be uh the row minus one. So the previous row um and I'm going to use optional chaining because when we're at the top group row minus one will be undefined. But I'll say if that row exists um and column minus one at that given row I guess exists and if that is equal to an at sign um then total was equals one. Uh how do I do this optional access? I know it's possibly undefined. That's what I'm trying to fix it. So, can I do Okay, that's it. So, if this exists, then try to access that given column. And if it's equal to the at sign, increase the total. Okay. [snorts] Um I think I already see a pattern. We're just going to have a bunch of if statements. So what if we create a list of neighbors that has all of these in there and then we just iterate over it. So this will make it much easier. Um and we can keep our comments. Okay. So that's the top left. The top is row minus one column. The top right column + one. The left is this row column minus one. The right is um this row column + one. The bottom left is going to be row + one column minus one. The bottom and now that I think about it, we could have done this with a for loop, but it's fine. So the bottom is row + one at this given column and then the bottom right is row + one column + one. Um okay. So these are all the neighbors that we need to check. So for each one um we'll increase the total and we'll do this with a fun little reduce. So we'll reduce over these. This gives us each neighbor. Um, and if neighbor is equal to the at sign, then we'll do our t total + one. Otherwise, return t. T starts off at zero. Okay, that should do it. And now we're going to expect the number 13. If we don't get the number 13, my thought is maybe we need to do less than or equal to four. But let's hope for the best. We want to see 13. Nice. Okay, so our solution works for the sample. Let's hope that it works for the regular input so I can get to bed soon because it's late here. Um, here we go. Yes, we're cooking. That was easy. I think that took us like a little over 10 minutes. Great. All right. Now, on for part two. The elves just need help uh accessing as much of the paper as they can. Once a roll of paper can be accessed by a forklift, it can be removed. Once a roll of paper is removed, the forklifts might be able to access more rolls of paper, which they might also be able to remove. How many total rolls of paper could the elves remove if they keep repeating this process? Starting with the same example as above, here is one way you could remove as many rolls of paper as possible. Using the highlighted at signs to indicate that a roll of paper is about to be removed and using X to indicate that a roll of paper was just removed. Stop once no more rolls of paper are accessible by a forklift. In this example, a total of 43 rolls of paper can be removed. Start with your original diagram. How many rolls of paper in total can be removed by elves in their forklifts? Okay, my thought is um we can use our existing solution, right? because we're still counting neighbors. But if uh

Segment 20 (95:00 - 100:00)

if it can be removed, we overwrite the grid and keep iterating from there. My only thought of why this might not work is does that mean we can always just start from the top left? Like if we start iterating from the top left and removing them um does that work or is the forklift able to enter from any given side? Right? Like this doesn't indicate to me where we can start removing things. Oh no. It's multiple iterations. So we do multiple passes. So we run it once, remove those 13 that we found the first time, and then do it again. That's what this means. Because we found the original 13, we remove them, and then run it again on the exact same grid. I think that's the answer here. — [snorts] — Now this we could run into this running forever but it should be a recursive solution. [snorts] Uh an SPDN is asking are JS strings immutable? They are. If you want to overwrite them you need to change overwrite them. Yeah you cannot mutate strings in JavaScript. Okay my friends let's hope let's try for part two. Um, okay. I think the way we'll get this working um this is this whole thing here is a pass over the grid. So let's put this into a nested function called grid pass. And then we'll [clears throat] run it once, which gives us a total of 12. And I think we'll actually return the total. So that way we can keep track of if anything was found because basically we want to keep running it until nothing is found. Um, so we'll say and outside of here we can say um here and then we'll also have a grand total. We'll increase the grand total by found and then log grand total. I basically want to keep iterating while found is greater than zero. So as long as we found something, keep on keeping on. Right. I guess I could do a do while. Okay. But now grid pass needs to overwrite all of those locations after it's done. So here's what I'm going to do. Um if the number of neighbors was less than four, we're going to keep track of that row and column. So let's also have a found. And this is just going to be an array of rows and columns. So this will be an object where row is a number, column is a number will be an array of objects like that. And so uh if we found one that can uh be accessed, we'll then push it into here and then we will overwrite those values in the grid afterwards. So afterwards we

Segment 21 (100:00 - 105:00)

can then say um for each of the found values this gives us a row and a column. we need to overwrite that given string the grid by removing these. So we change them from being an at sign to being a period. Um and actually I don't think we need to iterate over found. I think what we'll do is we're going to again look at every single um row and column because that way we can build up the row string and then just replace it in the grid. That is what we will do. Um, and so instead of this being an array of objects, we could just have an array of numbers that itself has an array of numbers. Is that how that works? How would this work? What's the syntax here? So I think we do at row push column and found at row when we start equals a new array. Okay, but this will uh better allow us to um keep track of those values and not have to search through an array. Okay, so here's my thought. We we're keeping track of every row and column that we can now overwrite. And then we loop again. But now we are defining a an updated row that will be a string starts off as the empty string. Um we grab the existing cell but we will say if found at that row at that column then updated row we automatically overwrite with a period otherwise we put the current cell value onto it. After that, the grid at that row is going to be the updated row. Done. First try. Infinite loop. What's up, Rocket Turner? Welcome in. Yeah, our code just runs forever. Fantastic. Um, we need to do some debugging. Um, let's do one pass and log out the grid. Oh, well, no, we are we we're overwriting the input value. Yeah, so this is overwriting it. So, that should work. But, um let's see what our grid looks like after one pass. Um, I want to log it out nicely so we can easily look at it. So, I'm going to create a print grid function, and it'll just iterate over all the rows and columns building up a string. Um, for every row, we're going to console. log grid at that row. I think that's literally all we need to do. I don't know why I created a function for that. Um, print grid. Okay. We want to compare it after the first

Segment 22 (105:00 - 110:00)

pass. So this is the initial state. Um they're replacing it with X. So I will replace mine with X instead of a dot. Um, I want to print the original first as well. We'll look at it before and after. Okay. So, this was before, this was after. Um, let's replace it with X again instead of a dot. This is wrong. We're replacing the wrong ones. Um yeah, we replaced these with X's and we should not have. So something's wrong with our replacing. So um we put that row and column in there. Oh, it's not going to be at column, but it could be. So, instead of pushing it, uh, we'll put it there cuz we were just pushing it in. So, we'd have to do a find instead. Um, we'll it'll be uh boolean array of arrays. like that. That way, this is basically going to be like a sparse array. So, it starts off empty and then we set a value at that given column, but that column might be like three or four. But if we were to do found row at uh zero, that could just be undefined if there's nothing there. So we're just putting true into this sparse array. That way we can do an instant lookup like this. That's what we needed. That should fix it. Yes, that's what I want to see. Um so if we compare that to the original or to the expected state, that's much better. Okay, fantastic. So now uh I'm not going to print the grid anymore and we should be able to do this while loop. So keep iterating and replacing until you don't find anything and we'll have our total 43. 43 is the answer. Okay, we did it. And now we run it for the main input. It's pretty instant. It didn't run forever. And it will work on the first try. Yes. Feels good. All right. Yeah. Thank you, Mark Boots. That was so much easier than day three. Um, yeah, we were done with day four in under 30 minutes. And day three took us over an hour. Oh, you just solved it. Two more boots. Nice. Love to hear it. And thank you uh Zunib for the raised hands. Uh yeah. Um okay. So yeah. So uh Fineman diagram says just turn them into a list. Oh, we're using Python here. So for every row read it and split it list of lists. Yeah. So in JavaScript I mean this might be the same thing in

Segment 23 (110:00 - 114:00)

Python but we can access indices of a string using bracket notation which is why I just left it as an array of strings because here this is saying grab the string in the array at that index and then grab the character in the string at that index. Yeah. Okay. That took much longer than intended or than I thought it would. U but I am going to um I'm going to get out of here. This was fun. Oh yeah. So we do have a Discord. Um I will say um it's not as active and it needs some maintenance. I need to fix the onboarding workflow, but we have a channel in the Discord for uh discussing Advent of Code. I think Mark Boots has been sharing their solutions in there. So, feel free to join if you'd like. Um, I don't know if I will stream Advent of Code again. If I do, it will probably be maybe like Monday morning. No, I'm I have a bunch of work stuff Monday morning. Monday afternoon maybe if I do stream Advent of Code. But uh it's sad to say, but no, nobody's watching my Advent of Code YouTube video this year. So um I posted it over on Syntax. Uh basically I did a walkthrough of my solutions. So I streamed it live on Monday night and then I did a separate recording where I just did a straight walkthrough not struggling like I did in the live stream. Um and I published this here. But, you know, I mean, it's a it's absolutely a decent number of views — that time of year. Don't get me wrong, that that's a decent number of views, but comparatively for the amount of effort because I have to stay up so late um it may not be worth it. I think um this year um there's just less people looking for solutions because they just ask chat GBT for the solution instead of watching a YouTube video for the solution. Um, but last year, so if you look on my coding garden channel, I didn't have a lot of successful videos last year, but one of the more successful ones, uh, was me solving Advent of Code. Um, oh, I'm I guess I'm thinking of two years ago. Yeah, two years ago, 20,000 views on a video was huge for me. And [clears throat] two years ago, so many people watched my video because day one was like hard and they wanted to see how I solved it. Um, I guess last year the live stream didn't get as many views. I don't know. Now I'm worried about views. It's my job to worry about views. Um, but I digress. Uh, thank you everybody for tuning in. Uh, we are going to do a raid, so stick around for the raid and I will see you soon. I'm going to stream at least once next week whether or not I stream Advent of Code. Um, so follow also follow the syntax channel because I might live stream over on the syntax channel. You can find all the syntax stuff on YouTube and everything there. Um, and is that does that work? Yeah. And then that's the links to all my stuff too. Uh yeah, and SPD says, "No tough algorithms yet. Haven't had any dystras, etc. So far. " Yeah, the most views are for people who did it in like four minutes, the competitive ones. Yeah. And I mean, at that in that sense, it's like they're the first to post because they did it so fast, but also people might watch it to be entertained because they're solving it so fast. But I will say one of the things about my videos is I explain the solution, right? It's not just let me solve it. It is like here's the solution and here's why, you know, which is which I think is a little bit different. Takes a little more effort. But okay, I'm going to get out of here. Stick around for the raid and follow all the places because I will go live again next week. Uh thanks everybody for watching wherever you are in the world. Have a wonderful morning, afternoon, evening, or night. And until next time, here's this —

Другие видео автора — Coding Garden

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник