Skip to main content
View on YouTube to comment and like!

Syntax Video

Working on Debug December | AoC | Clash of Code | Syntax Bytes w/ CJ

Chapter 1 00:00

Transcript

What's up everybody? Welcome in. Uh welcome to Debug December. Uh in this live stream, I'm going to be solving the first five Debug December challenges. So this is a yearly challenge that's released by Sentry. And it's similar to something like Advent of Code, but instead of solving algorithmic problems, you are given some broken or buggy code and you need to fix that code. So that's the plan for today.

We're going to do the first five. We'll see how it goes. Uh, but welcome in everyone. Good to see you. We're live in three spots. We're live on Twitch. So, I see all the coding garden regulars. Welcome in. What's up, Stony? Uh, and Acid Spark and Oscar and Dynamic Voyage. Welcome in. I'll catch up on chat in just a second. And good to see you, Internet Doggo. And Oscar over on the YouTube side. And Lenny, welcome in.

You're watching my uh reverse engineering the pizza video. Nice. Yeah. So, if you haven't seen it, check it out on the Syntax channel. Um, the order a pizza with code video. This was so fun to make. Basically, uh, Wes challenged all all three of us to order pizza with code. So, definitely check this out. Um, and then I published my point of view because this video is more of like a summary. It's all three of us and it shows all three of our perspectives. Um, but I published a video on my channel that uh showed how how I did it. So, you can check that out, too. I'll post that over here.

[laughter] But yeah, it was really fun to do. Yeah, Sexy Pizza is a local Denver pizza chain. I don't think it exists outside of the Denver area, but it's definitely my favorite pizza place. Yeah. And welcome Anani as well. Yeah. Um, I it took me a second to uh sorry for uh being so late. I'm streaming on three platforms. This is the first time I've streamed on the syntax channel in a while, so I had to figure everything out, but I'm here now. I am here.

Welcome in. Um, okay. You can hear me though, right? You can hear me. It did. Yeah. So, if you watch my video, it starts off with um actually I'm gonna turn my mic down just a little bit. I think I'm peeking slightly. Check, check, check. How's my audio? Uh, but yeah, uh, the I um I ordered the pizza and then there was one more endpoint that linked the payment to the order, but I didn't know what that was. Uh, so my card got charged $45, but yeah, it it got automatically voided like 24 hours later. I didn't even have to reach out to them. It they just voided it from my card. Yeah. And I did uh I did eventually get pizza. I did.

Yeah, you'll see it in the video. Um, and internet doggo says, "I like Debbug December more than Advent of Code. I couldn't get today's part two at all. My solution worked on the sample, but not on the real thing. Advent of code is hard. Um, it's really satisfying when you solve it, but it is hard. The So, days 1 2 3 and four took me a total of about four hours to complete. So, on average, an hour per challenge.

Sometimes the challenges only took me like 10 minutes, but other challenges took me like over an hour. They're hard. It's hard out there. So, [laughter] uh, yeah. And thank you for all the follows on the Twitch side. Yeah, we're we're going to try to start streaming more on on uh Twitch. So, be sure to follow on Twitch. We would appreciate it. All right, without further ado, let's jump right in. Uh let's look at the intro. We'll read what this is all about. So, this is called Debug December by uh Sentry. I'll link it if you want to give it a try and if you haven't yet. And I believe there are prizes. I don't know all the details. We're going to read about it in the intro. Um but and I also believe Centry has a Discord where you can talk about this stuff. We will learn. Okay.

So, Debug December is a series of 24 coding challenges where you solve bugs. One challenge unlocks each day and we hold a raffle for swag on days 36 12 18 and 24. Okay, so tomorrow they're going to hold a raffle for swag. So, if you want to be included in that um Yeti slippers.

Oh, do we get to see a picture? Maybe not. But tomorrow they're gonna So I think everyone that completes up to day six will be entered to win and 30 of you will win some exclusive Yeti slippers. So be sure to enter. Uh and then yeah, I think it's the day 24.

They're going to raffle off a PS5 for everybody that solved all the challenges. And they got ghost boards and blankets. Actually, while we're while we're thinking about it, let's check out the sentry shop because there is still a sale going on for all this really cool merch that I'm wearing. So, we got new uh syntax beanies and uh you might not be able to tell, but this says syntax embroidered embroidered in black.

Um but all of that is 20% off right now. So, if you check out this, we got the internet hat. We've got some really cool merch. [laughter] These pins are only seven bucks. These are some legit pins. And yeah, this is the uh hoodie that I'm wearing. I love this shirt. I wear this shirt all the time. We also have fingerboards. Oh, they're sold out. Uh we need we need to get some more fingerboards. Um, but this little uh fidget toy key cap thing, these key caps, they're real key caps. I don't know why I said that, but they're legit key caps. You can pop them off and put on your keyboard, but like the vibe key is the size of a tab key, and like you could put that on your tab key.

Um, and good to see you, Kristoff on the Twitch side who says, "Hello from Barcelona." Hello. Welcome in. Um, yeah. And, uh, MO potato says, "Do you ship to Argentina?" I don't know. You're going to have to check this the the shop website. Um maybe look at the shipping policy to see what companies or what countries they go to. Uh yeah, but fingerboard is awesome.

These shirts are get you some merch. Also, like uh these these key caps and these pins make for a really good uh stocking stuffer. But aside from that, there's also the Sentry collection. And this might be where we get to see the prizes you can win. Also, the Sentry shirts are so cool. I I wear this all the time. It looks like a heavy metal shirt, but it just says full stack. It's sick. And then there's also uh the bug slayer shirt, which I wear less of now because I am vegan and I don't I don't kill bugs. [laughter] I kill software bugs, but not real bugs.

Um but this shirt is also really cool because it says like bug slayer. Um and then they got some retro shirts. We got we got the coolest merch, if I do say so myself. Um, also if you want to support open source, we've got some cranberry sauce. When is the CJ collection? I just got to take the time and do it. I think most of the the merch is spearheaded by Caitlyn and Wes. Um, so I think if I want the CJ line, I just got to make it happen. [laughter] We'll have I mean, the thing is the beanies are are are my style. If you've watched Coding Garden over the years, um, I wear beanies. These are a little bit thicker than I'm used to. Um, and we have other colors, too. So, we've got black, gray, and red available. I wear the black one all the time, and then I gave my wife the gray one. Um, so yeah, the these beanies you could consider the CJ line, but I do want to do a black on black shirt. So, a similar acid wash shirt with black embroidered syntax

logo. Um, that is I I want to do that. That could be the CJ line. I don't know. But yeah, check out the shop. stuff is on sale and uh you could win some cool stuff if you enter into debug December. So check it out. A black one with Yeah. So some sort of like cross between coding garden and syntax could be cool. There was a really cool uh actually let me let me find it really quick. There was a sticker that Wes made for React Miami that was like really cool floral pattern but that kind of works for like coding garden cross with syntax. I'm going to find it. Give me one second. I'll be right back.

Okay, I couldn't find it. But I am going to show you the pins and the uh the key caps uh because they're cool. They're really [laughter] cool. Actually, I want to turn my screen on. Okay, I'll be I'll be right back. I'll be right back. We're just getting started, everybody. We're just getting started.

Cool. These are the key caps. They're nice and clicky. Check them out. And then here are the pins. It's chaos. It's chaos. I just didn't want you to see me roll my chair back. I have no idea what's on the back of this chair. Uh but yeah, there we go. I started my cool little Sega game that I made back there. Um and then here are the pins.

They're sick. I put I've been bringing with them them with me to uh conferences and putting them on my um my lanyard and then uh these also work pretty well if you want to put them on hats or beanies. They're sick. Check them out. Cool. And what's up, Harsh? Good to see you over on the YouTube side. Um and thank you Kristoff who says, "Nice keycaps." Okay, without further ado, let's continue reading the intro to Debug December. So, there's challenges released every day. They're going to do raffles on these days. So, the good. The Yeti has joined the office. The Yeti has joined the offic's party planning committee, and it's finally time for his big debut, the office holiday party. He had 24 festivities planned, and he can't wait to get the party started. Nice. The bad. Just as the party is underway, the tech is on the fritz. The yeti must run around the office and get everything working before the party is ruled as a disaster. Help the yeti debug the office and get the party back on track. He might just send you a thank you gift.

Sweet. So, each one of these days, each one of these challenges is us getting that party back on track. Is everyone ready to start solving? Actually, if you're going to solve, then you should uh [laughter] go do it yourself. Mute me, try it, then come back. Uh let's get let's get into it. We'll do day number one. Okay.

The Yeti is planning the office holiday party and is trying to restore power to the party venue. The circuit breaker system crashed and needs a new configuration. This function should create a fresh power config while keeping the backup config untouched in case we need to roll back. But something's wrong. The backup config keeps changing, too. So, we have this function restore power. It takes in the new power level. Oh, we've got uh hover.

There's a full-on editor in here. That's great. So, we get the new power level. We get the backup config. We create a new config. We update the power level. And then, we return the config. And the unhandled error that we're getting here in Sentry is cannot assign to readonly property power level of object.

Okay. Now you might see this code and if you're a React developer you probably know what's going on but I can see it. So this is a shallow copy right? This only copies the top level properties but we're editing a deeply nested property. So my first uh thought is we could do a structured clone or like a deeply nested clone.

But I don't know if that's going to fix the readonly property. I mean I think it readonly properties happen I think when there's like object.freeze or object.define property that makes it not setable. But I think we can still copy it. So I'm going to use structured clone but I don't know how it works. So, I'm going to search for structur clone on MDN. I spelled that wrong. Did they find it? They found it. This is newer. It's now bas baseline widely available. So, since 2022, there is basically a deep clone built into the web browser. Um, and what do you do? You just call structured clone. That might mean that I thought there was going to be more options, but I think this is all we'll need to do. So, if we instead do a structured clone, that should do a deep copy of all of the properties and then we should be able to update it. So, let's go. We [laughter] did it.

Nice. Um, I will say, uh, if you don't want to use structured clone, can you think of how we would do this? Because the code would be really freaking ugly, right? We'd have to do this. So um spread it to copy it the top level properties. And then we need to copy the main panel. So we're going to spread um backup config main panel into that.

And then we need to copy the circuits. Um so then we would do circuits. If you're a React developer, if you ever worked with like Redux, you you've done this before. like we we we go we [laughter] go actually if you've done this before shame on you because you should have installed a library. Um and then I think this is all we would need to do to solve it as well. Yeah. So that does it without the structured clone.

But it's ugly. That's some ugly code. All right. Yeah. And so great point. Um if you think of other solutions, let me know in the chat. But um over on YouTube, uh internet doggo is the other solution is to do a JSON.parse JSON.stringify. Yeah, this is I don't know if you want to call it a hack, but basically if you stringify the object and then parse it again, that's going to be a brand new object. I think structured clone is probably the better way to go. I don't know which one's faster, but this is how we did it definitely before we had structured clone if we wanted to copy the entire thing. Yeah. Yeah. Oh, no worries, Julian. I saw your comment today. Thank you. Thank you for tuning in. It's been a long time since I've seen you. Yeah.

[laughter] Uh, welcome in and good to see you. Uh, Mugisa, hello. Hello. Hello. Yeah, we did it. Stringify back to JSON. Yeah, I uh Iron is mentioning the same thing and no one's watching on that other platform over there. Okay. Um, moving right along. Oh, look. They literally told us you could use structured clone, nested spreads, or JSON parse.stringify.

Look, our powers combined. We We figured it out. Let's move on to day two. The Yeti's big debut on the party planning committee is off to a rocky start. Yeti programmed a fancy automated greeter to unlock the doors for the guests, but the system is completely unresponsive. The first guests are shivering outside and the doors won't budge. Something is definitely wrong with the code. Can you fix it before the hot coco code gets cold? All right, greet and open door. So, we've got room, guest name, which has a default value of guest. We're looking at the room. If the room is one, then we'll say hello guest, your room is blue. If the room is two, we'll say hello guest, your room is red.

Otherwise, we get back unknown. Um, and the error is unexpected return. The function has returned an unexpected value.

Why would we get an unexpected return unhandled? Is this how switch case cases work? I think you can have return statements inside of a switch. I'm just going to look up the syntax for a switch case because [laughter] that's where I'm going to start because I can't see anything obvious.

So yeah, we can have breaks, but I think you can still have a return inside of a switch statement. Um, I'm pretty sure. So, that's not it. It's got to be something less obvious.

Oh, I see it. Do you see it? [laughter] Um, this this is a weird error to get unexpected returned, but one the room is a string. It's got quotes and we have uh the cases here for for numbers. So we need to match over it or I've got an even better solution.

Use double equal. Can you can you do a conditional in a case or is it just have to be Why am I forgetting how to program? [laughter] I guess cases just have to be solid values. Okay. I was going to say let's do some coercion, but we can do it one of two ways. We can change our cases to uh be strings or we could turn this into a number and then switch over it as a number. I think we'll do that. That's cleaner and less less changes that we have to do. Okay, that didn't work.

Um okay. Hello null. Your room is red.

Okay. So, is this the right syntax for u auto assigning things? [laughter] So, when we get null, oh, because null is an explicit value. Wow, JavaScript sucks. Okay, so the only time this will work is if no second argument is passed in. But if they pass in null, that's going to overwrite it. So, that doesn't work as a default argument. So what we're going to do is just do this inside the function if not guess name. So that'll account for um if it's null or undefined, then assign it. Moving right along. Cool. I like that this was a a two-tiered challenge.

Great job, challenge designers, cuz yeah, we solved the first bug and then there was another bug. Sick. Uh, anybody in the chat, how would you have solved it? And did you spot it first? Let me know. Let me know. Yes. Um, return stops the switch. Yeah. So, I don't know. I I all of a sudden I just forget everything I know about programming. And maybe it's because I've tried different programming languages because like in Rust you can do pattern matching. In Go, you can do pattern matching. And that's like similar to the case statement. But yeah, case statements can't have conditionals.

They need to be an individual value. Yeah. Parse it as an before. That's that's what we ended up doing is just uh turn it into a number. Switch over that and welcome in uh Mihir Nova from India. Very good. I should have used TypeScript. Yeah, TypeScript probably would have caught that. Um and um I think if you change the switch case to take a boolean value and then the cases to be if statements that will give out a true false return condition.

Yeah, that would work, but that that would only work for true and false. So

yeah, um we could do like case equals one and case not equals 1 [laughter] or something like that. It would it would be trickier to try and do that. Um I just did guess name. What do you call this operator? Uh nullish co it's nullish coalesing. Yeah, you did the nullish coalesing operator on both of them. That would have worked, right? Um, if we could do it in line, we could say get the guest name. If it's null or undefined, then it should just be um guest like this. That would work too. I like one place in the code to change it.

That's my preference there. Doesn't go have pattern matching? Oh, no, no, no, no. I was thinking of um uh Oh, camel. Yeah, we were we were reading people's solutions on Clash of Code. I was thinking of OKAML, not Go. I don't know why I got those two confused, but I don't think Does Does Go have pattern matching?

No, there's a pattern matching library. Okay. Yes, I was confused. Thank you for correcting me. Uh, all right. Am I going to attempt the advent of code fully? I don't think so. It's way too hard. It takes too much of my time. Um cuz the the the challenges go live for me at 10:00 p.m. If I was just solving them in the middle of the day maybe, but I don't I got other things I need to work on.

It's it just takes me too long. Oh, good call internet doggo. Yeah, we could actually do the um Wow, you could do the nullish coalescing assignment. Is that valid syntax?

Can you Can you do that though? Oh, I see. With You can't do it in line, but you could do it instead of the if statement. Yeah, you're totally right. We could do this. Um, like that. That That's the play, right?

I don't know if I like it, but that but that works. So that basically says if guest name is not null or undefined then overwrite it with guest. Yeah. Yeah. Yes. Kristoff says, "I feel so dumb and unable to code." That's okay. I do all the time. If you watch today's uh three devs challenge over on Syntax, uh I just felt dumb this entire video.

This is just I mean we're we're we're happy to look dumb for for your entertainment, but this is 20 minutes of me not knowing what the heck is going on for your entertainment. So definitely check that out. Okay, let's look at their bug breakdown. Uh the switch statement uses strict equality, meaning the string inputs for room would not match the numeric cases. Also, default parameters in JavaScript only are only activated for undefined. So passing null for guest name did not trigger the default value.

Great. Yeah, I figured that out, but uh it took us a second to get there. Let's let's keep moving. All right, day three. The Yeti is planning the office holiday party and has curated the perfect playlist with all the festive hits. The music streaming system needs to assign each song a unique incremental ID to cue them properly. While testing the system with thousands of songs, something's gone wrong. The last few songs won't get their IDs, and the Q seems stuck.

Without fixing this, there's no party music. Oh no. All right. unexpected return I have now learned is just internal to this system because that's that's not like a classic um JavaScript error that I've seen before. That just means our function is returning the wrong thing. Um maybe if you use an assert statement that's what you'd get.

But uh we input with a th00and we expect this. Yeah. Um I know the problem. Do you see the problem? I know the problem. Do you see it? It's because plus+ increments after like returning. So you you you just want to do plus one. Um because if you do plus+ you get the previous value and then it increments it. But we want to actually increment it.

Okay. Hidden. Oh new ID to be bigger than the previous ID. Great. Um, [laughter] numbers can only be so large in JavaScript. So, if we tried adding one onto the largest possible number or yeah, largest possible number, uh, it won't get any bigger. So, we need to use a big int. Are we allowed to do that?

Uh, let's look on MDN on how to use um big ins because I don't use these often. Um, either that or we turn it into a string. Um,

easy enough. Just put it in the big int constructor and that should be able to go bigger than that. Okay. Cannot big mix big int with other types. Use explicit conversions. Okay. If that's the case, can we convert it to a string and then add one to it? [laughter] And then if we have to do that, do we have to um just do our own maths? Are we going to have to implement big big int?

Because I don't like that. I don't like that at all. Um

all right. What is the error we get? Yeah. Acid Spark on Twitch says, "And Door Dash delivered the wrong order because it had [laughter] Wait, are you talking about because of the wrong ID or something like that?" Um, yeah. Mochin, uh, Rakkesa, I see you over on YouTube. I've been watching for a long time with the CSS battles. It's really fun to watch. Nice. Uh, from is it Mozambique? Welcome in. That's awesome.

Okay. Uh, internet doggo says, "This is another double layered one." Yeah. Okay. Um actually can we do console logs? What is the type of this thing? Is it um number or are they passing in some other interesting type number?

Yeah. Um

cool. See you later, Kristoff. Have a good one. Was I not just doing big int of current ID plus one? Okay. I did get a hint from internet.go over on YouTube who says we need to do plus one in. Do we have to do that? Oh, you're totally okay. Okay. Order of operations. If we try to add one to a number before converting it to a big int, it's not going to be the right value. So, we need to do this and then add one onto it, but we need to add one as a big integer. And the way to do that is with the letter N with Yeah, n. Okay, that's that's the play. Nice. Okay, [laughter] we got there. We got there. Um, but yeah, that was the issue. We were literally just It wouldn't have worked because we were trying to add one to the biggest possible number before converting it. Very good. Very good.

Oh, they ended up fixing this one. That's good to know. It's I'm glad I'm doing it a few days later. [laughter] So, uh, oh, that was on Twitch who says, "Dynamic Voyage says, "This is the one that had a bug in it, which they ended up fixing." Yeah. Yeah.

Yeah. Oh, you actually got the wrong order from Door Dash. I'm sorry to hear that, Acid Spark. [laughter] And they didn't include your banana Nutella waffle. You know the worst thing about getting the wrong delivery or something missing from your order? Now you got to wait another hour if you want them to replace it. Like it's it's fine.

They'll probably like I've had multiple issu is issues and they refund me or they give me store credit, but I'm hungry now. I don't [laughter] I don't want to wait another hour for the thing to come come again. And yeah, it's the worst. It is the worst. 27 minutes was the estimate. That's not horrible. But still, you were you were ready to eat that. What is it? Banana, Nutella, waffle. Yeah. All right, let's read their breakdown. JavaScript numbers are 64-bit floats that can only safely represent the max safe integer. Beyond this limit, incrementing fails. For larger numbers, return big int. Cool.

All right, day five. Here we go. [snorts] The Yeti is ready to set the mood with beautiful string lights around the office. Yeti has a list of all the light IDs that need to be turned on, and Yeti has written a function to handle it. But when Yeti flips the switch, nothing happens. The light should be glowing by now, but the party space remains dark. Something's not working as expected with the lighting system. Can you help Yeti illuminate the party?

Okay, we get the light IDs. We're iterating over each one with the given ID. Turn it on. Oh, I know the issue. We need a promise.all. Yeah. So, this for each happens instantly. Results is the empty array.

What we would want to do is map over promise or do a promise.all and then return a promise inside of each one. Um, this might be a little bit tricky because we are logging an error message. And so if an error happens, we're going to have to fix that. But if we change this to a map, now this returns a promise for each function call. And then we're waiting for each one to be complete. And then we now have an array with the final values. And instead of pushing, we can just return um the data like that. We could do it in line, too, but this should be fine. Uh so this should work. I think the issue is if an error is ever caught, we're going to have um undefined in the result. So I'm we're curious if they care about that. No, they don't. We did it. Fantastic.

Um the for each method does not wait for async callbacks. The function returns before any lights turn on. Use for of or promise.all all with a map instead. Yeah. So, promise.all is going to fire off all of these promises all at once. If we did a for await of it would do them one at a time. Um, so let's let's see that codes too.

Um, basically we want to do this. Actually, we want to go No, I'm going to keep this here. So um

we have the results starts off as empty. We'll do 408 for let value. I got to look up the syntax for [laughter] 408 of um so we'll do uh light ids. Oh no, we just need to do a four of because the the the promise happens inside. So yeah, this isn't a for aait of loop. This is just a for loop with an await inside of it. Yeah. And then instead of returning, we push into results.

Uh and then we'll call this ID. So for every ID in the light ids, if you found it, push it into the array. Uh and then at the end of it all, return the results. Um yeah, and this is fine, but again, it's going to wait for each one to complete before it finishes. This is going to fire them all off at the same time. So they they each have they each have their uses. Yeah.

Um yeah, the other order had steak and eggs and smoked salmon. So wait, you ordered a banana Nutella waffle and you got you got steak and eggs and smoked salmon. That's the worst. What's up, Ched? Welcome in. Yeah, [snorts] that's that's the issue, right? Acid Spark, it's like I get the delivery drivers like they at a certain like they trust you as a end user at the at a certain point, [snorts] but um at a certain point you just got really bad drivers that are making mistakes and it's not your fault. Yeah.

Cool. Yeah. And thank you internet doggo for the uh plus one in. [laughter] Feel free to implement your own big int addition. I've done it before. I think there was a code wars challenge before big int was introduced into JavaScript. So I literally implemented uh big int addition to solve I've done it before. Yeah, just convert it to a for loop.

Yeah, that's the way or one way. Oh, thank you uh Juan Sebastian. Thanks thanks for dropping in. Yeah. Okay, the final one. Day five. Um here we go. Oh, regular expressions. Oh no. Okay. The Yeti set up a fun chatbot to handle party RSVPs, but it's gone haywire. The bot is supposed to filter out spam messages, but instead it's randomly accepting spam and rejecting legitimate party invites. Everyone's confused. Half the office thinks they're uninvited. The Yeti triple check the spam detection function, but something's still off. Can you [snorts] debug the filter before the whole party falls apart? Return true if the text contains spam. Case insensitive.

All right. Um, we get the text. We do reax.est. I think that returns a boolean. Yeah, it returns a boolean on the string that we're testing it on. And we're looking for spam case insensitive across the whole string.

Okay, so it looks fine. I think we just want to see what the inputs and the outputs are. So this message contains spam. should have returned true but it returned false and the error we're getting is error [snorts] [laughter] but I'm pretty sure pretty sure um that we can do this. I'm just going to do it in a little console here. So if we do this, if we test it on hello world, false, hello world spam, true.

Okay, so something else is going on. Um,

let's log the text.

There are no console logs. Console is not defined. Thank Thank you. Thank you for that. [snorts] All right. Um, great. Can we Can we do a type of Are there weird characters inside of it?

Um, I'm going to do an array from on it to see if there's any weird characters inside. Bam. It could be something obvious. I'm not going to look at the chat because it could be something completely obvious. I'm going to keep working on it.

Um,

yeah. So,

we're doing it right. We have spamreax. test on the text. And in our example here, we test it on the Next.

Um, let's store this in a variable.

Let's log it. True, false, true, false.

return true if the text contains spam.

Huh? I don't know. My my only issue is that like this is working [laughter] my console here. So like there there are other functions we could be using, right? like [snorts] um what is what does exec do instead of test? It finds it. So we could like check the length.

Um yeah, I think I'm just going to I'm just going to do that cuz this is really messing me up. So if I do exec and then um turn it into a boolean.

Do we need to flip it? Is that the only issue? Oh,

okay. That solved it for some of them, [laughter] but then it failed for this. Okay. Um, I'm going to go ahead and say the logs it was giving me are not good because basically the answer remove case insensitive. That's it. That's they don't they don't want a case insensitive match. But the logs it was giving me did not hint me towards that. Uh, let's see what we were getting.

Wait, really? Wait, what? No.

Oh, it remains stateful. That's a weird bug. So, [laughter] I guess there are there are two fixes. Oh, no, cuz it says it's case insensitive. Okay, that was okay. Okay, never mind. It's a it it was it did the right thing because um at a certain point when we got to the next one it should have been case insensitive but that it wasn't but I did not know that about regular expressions. So apparently they are stateful. So if we recreate the regular expression on every execution then um it's not stateful. Great.

Or like it doesn't hold on to the state from the previous one. Okay. Oh, that that was tricky. That was tricky. But that's also something I learned. I literally did not know that the um case insensitive flag was stateful. Let's look up the Let's look at the rejax. And um let's look up state.

There's nothing about state in here.

Yeah, I don't know how I would know that or where to look that up, but fascinating. We got there. [laughter] Um, and thank you for the follows over on Twitch. Appreciate you. And what's up, Zardy? Good to see you. Yeah. Funny Boy Rock says, "I solved this one on accident." I was tempted to just put the regular expression inside, but then my optimization brain was like, "Well, no, you want to create you want to create it once and then reuse it multiple times." But I swear I've done this before. I swear I've created a regular expression to be reused that worked with case insensitiv uh with case insensitivity or I just had a bug in my code and didn't realize it.

Yeah. Also, I don't think um um I don't think it's a JavaScript issue. I think maybe that is well maybe if it's the implementation of statefulness then that is a JavaSc JavaScript issue. Oh, it says it right here. [laughter] is it was there the whole time. Um, [snorts] objects are stateful when they have global or sticky flags. They store a last index from the previous match.

Using this internally, tests can be used to iterate over multiple matches in a string of text with capture groups, the more you know. [laughter] Great. Well, here here's the thing. We learned something today. So, thank you very much, Sentry and Debbug December, and the designers of these challenges uh for forcing us to learn about regular expressions.

Mind blown. Yeah. [snorts] Um, yeah, I found it. Cool. Yeah. Julian says, "It's a weird bug. They've run into it once." Internet Doggo, you didn't have any faith, Internet Doggo, that I was going to figure it out. [laughter] I think at some point I was going to move the regular expression inside the function. I just ended up figuring it out um accidentally. Like it was really weird that we didn't get to see the log until I did a weird thing with testing.

Oh, you know what it was? I inverted the solution. So, [laughter] it was because I did not match that it failed, which let me see that there was an issue with uppercase versus lowerase. I I stumbled into the solution. There's I did not get that right on purpose.

Yeah. So, no, I don't think removing the global flag would have worked because it still needs to be Oh, wait. Is that the case? Yeah, that would have worked, wouldn't it?

Cool. But is the case insensitivity? Um, well, I mean, that worked inside the function. Um, is the case insensitivity stateful? No, that was it, too. We didn't need the global flag. The more you know.

Yeah, that was it. [laughter] Awesome. Okay. Well, that was fun. So, we've got up to day five. Um, if you're just joining us and you haven't tried these yet, um, go try them. Try them out. And they're going to be doing a raffle tomorrow. Did the MDN doc say G or I? Maybe it's because it was both of them.

Global or sticky. So, it's just G or Y. And I've literally never used Y. What does the Y mean? Returns whether or not the Y flag is used with this regular expression.

But what what does the Y flag do? [laughter] Um flags. Perform a sticky search that matches starting at the current position in the target string.

Never use that. Okay. But, uh, this was fun. Thank you to Sentry and the designers of these challenges that that they threw they threw us for a loop and we learned some stuff today. So, that was good. I'll remind you they're going to be holding a raffle um for some cool swag. So, definitely go play.

And I think I want to do something else. I haven't been live that long, like 40ish minutes. So, should we try to do Advent of Code? [laughter] Let's um maybe maybe we take a look at Advent of Code day five and I decide if I want to do it or not. Actually, we let's let's do a poll. Um actually, let's let's let's play Clash of Code. We're going to play Clash of Code. I'm I'm I'm actually done with uh with Advent of Code.

[snorts] If you're Mark Boots is saying it's easy. I'm tempted. We're gonna do a poll. Um, can't I do a poll? What What is it called on Twitch? Do we not have polls?

I thought we did. Oh. Oh. Well, we're going to use um straw pole.

Do we do um Advent of Code Day Five? Do we do Clash of Code or uh I guess I could try a CSS battle or we could do a Code Wars 7 Q speedrun.

All right, we'll run this poll for uh a minute or two. Let me know what you want to see.

And while that poll is happening, I will remind you there is currently a sale on syntax swag. You can get these really cool pins. These are only like seven bucks. Really great stocking stuffer if you want to uh get them for that developer in your life. Also, this fidget toy clicky keys. Um these are dangerous. If you attach the if I have one of these near me, I'm just going to be clicking like crazy and the keys pop off. Uh so this this vibe key you can uh put as your tab key on your keyboard if you have removable keys.

So, go grab this stuff. century.shop. There's a sale. I think the sale ends today. So, if you want to get 20% off all this stuff, including I'm I'm just a walking ad for for the [laughter] shop, but including this hat, including uh this hoodie. I already mentioned it earlier, but this shirt is awesome. So, go go go check out the shop. Also, the Sentry. Actually, do they get 20% off Sentry merch? Maybe it's only the syntax stuff, but regardless, these shirts are cool, too.

Like this one, it looks like a heavy metal shirt, but it just says full stack. Um, and then this one, the bug slayer one. They've got the uh the retro collection, but uh I don't wear white t-shirts. I only wear black t-shirts, so I would not wear this. If it was inverted, maybe.

No, if it was gray. If it was a gray here with black sleeves, I would wear it. [snorts] But, uh, go check out the shop. Get yourself some deals, some some Christmas presents. I may regret the poll. What did people vote for? [laughter] 11 votes for ad. It's fine. Well, I I'm I'm going to li I'm going to time limit myself. I'm not going to spend an hour, but that's okay. Um, [snorts] here's what we'll do. We'll do part one.

Everyone is telling me part one of Advent of Code day five is easy. So, we'll do part one. We'll assess part two. If I can't even think of an algorithm for it, we won't try it. And we will definitely do at least two Clash of Code with chat. So hang in there if you're not interested in Advent of Code because in I don't know 15 minutes you'll have a chance to battle me in Clash of Code. Okay, here we go.

Um, day five. Should I log in as my CJ user? because right now I'm logged in on the because I do the challenges twice. I do them live and then I do them for recordings. Yeah, I'm going to log [clears throat] out really quick and um log in so I get credit for this on my main account.

Oh, nice. Uh nice to hear aspar got their uh got their their waffle finally. If you were uh wait log in if you were in suspense as to how that conflict would resolve. [laughter] They got their waffle. What's up, SQL Gordster? It's been too long. Welcome in.

[snorts] Welcome in. Welcome in. Um I see I see you over there. Is there a syntax advent code leaderboard? No, there's not. Um I don't think I can create more than one leaderboard. Can I? Cuz I have my um I have my personal leaderboard. You can see the leaderboard co there if you want to join it.

Um but my leaderboard is stacked. There are some really good people in here. So I I don't even have a chance in my own leaderboard because this is the same leaderboard I've been using since like 2020. So there's a lot of really good um advent of coders in there. Um, yeah, we don't have a syntax one, but that's okay because I think this is probably the last challenge I'm going to do this year.

And in typical CJ fashion, um, I am not going to read the problem. I'm just going to solve it. [laughter] Add vent of code. We'll make a directory for 2025 day five.

Um, I don't want to do that.

Yeah, everyone's telling me part two is hard, but what if what if part two is easy for me? What if I just I just know it instantly? Well, [laughter] that's that's not going to be the case. We know that's not going to be the case. Um, so I'm [snorts] just getting my directory set up. I like to create two files for the sample input, which is this.

This seems like a double parsing challenge, which will be cool. So basically parse the whole list till you get to an empty line. That's going to be one aspect of it. And then parse the list after that. So that's just a fun parsing challenge. Um, if we look at our puzzle input, yeah, same thing. So you basically have a bunch of ranges and then an empty line and then the rest of the numbers. So there will be an initial parsing challenge.

Cool. And we'll do our FS stuff.

Create a function called part one. takes in the input, which is an array of string, and then we'll do the work. Um, if you're just joining us, we're doing Advent of Code. I'm going to update my title across everything before I get into this so people know what I'm working on. So, give me one moment, please.

All right, that's updated there. Got to update the

stream title. Can I edit I can't edit the stream title on X. Is anybody even watching on X? Say hello if you're watching. If you're watching on X. Um, cool. Mark says, "I need pen and paper to draw the logic out." Sometimes you have to. Sometimes it helps to diagram or in my case like pseudo code to figure this stuff out cuz um yeah, some of these challenges are very tricky.

Cool. Okay. Split by a new line. Okay, that's You're giving me an easy way to parse it. I'm going to parse it manually. [laughter] You could Yeah, you're totally right. You could create two input files. Um chat is turned off on X. Oh, dang. Well, that's unfortunate. I can't change it. Um, this is my first time actually getting it working cross streaming because I'm streaming on X, Twitch, and YouTube using a EngineX Docker container. But, um, yeah, I can't change that. Okay, that's why there's no chat [laughter] on X. That makes a lot of sense. Thank you for letting me know, Internet Doggo.

Okay, without further ado, let's get into day five. Um, we have ingredient IDs, which are ranges. Um, and then available ingredient IDs. The fresh ID ranges are inclusive. The range 3 to 5 means that ingredients 3, four, and five are all fresh. The ranges can also overlap. The elves are trying to determine which of the available ingredient IDs are fresh. In this example, this is done as follows.

Ingredient ID 1 is spoiled because it does not fall into any range. Ah okay. So basically we have all of our ranges and then we figure out how many of these are fresh which are the ingredients that they've passed in. So given a bunch of ranges given a bunch of numbers which numbers fall into those ranges. Easy easy as pi.

Um and we want to find the number of them that are fresh. Okay. Here's what I'm going to do. Um, we're going to have our uh ranges actually our um we'll call this is fresh and we're just going to build up a map. So this will be a map that goes from a number to true. If that number is in this map, it is fresh.

Okay. Then we're going to iterate over the input. So we'll just do input for each one. This gives us the given line. And at first we're going to be parsing all of our ranges. And then after that we're going to be parsing our uh is fresh. So we're going to have total fresh starts off as zero. We're going to be counting that up. And then we're also going to have um is detecting ranges and that starts off as true. So first we're detecting the ranges. So if we are then we're going to do those um if line is equal to uh nothing. If it's an empty it's if it's an empty line then is detecting ranges is now false and we would do the next thing. So we would we would break out of this. Okay. Um, if we are detecting the ranges for that given

line, I'm going to split it on a dash, that's going to give us our start and our end. And I am going to map that into a number uh into a list of numbers. So that way start and end are both numbers. And then I'm going to iterate from the start.

So let I equal start. Well, I is less than or equal to end. I ++

um I is possibly undefined. Oh, it's not. Uh [laughter] because we know that there's going to be two numbers there. Okay, for each one in this range, I'm going to put it into my map. So, I'm going to say is fresh add um I if No, no, just add it. Just add it.

It's not added. Set. Set. Set. So set I to be true. So [snorts] we're iterating over all of the ranges. We're basically keeping track of every possible number that is fresh. And then after that we're going to be looking at the individual values. So the line here is going to be the value. We'll just store that in a variable as and we'll make it a number.

And if the is fresh map has that value, then we will increment the total fresh. And when we're done with all and then when we're done with all of that, we'll log out the total fresh and we'll run it on the sample input and it's going to work first try.

Yes. Okay. So now we run it on the regular input. It's a big number. It's a big list. [laughter] Map maximum size exceeded. Okay, that's that's an issue. Okay. Um, apparently we can't store all of the numbers in a map. That's okay.

For for this, instead of storing them all in a map, um, we're going to have ranges and then we'll check every range for for each number. That's that's that's what we're going to have to do because we can't store them all in a map. I mean, we could just put them in an object. Would we run out of memory if we did that? And so instead of we might run out of memory um and is fresh at that value.

Would this work?

Let's watch it run out of memory. [laughter]

Dynamic Voyer says, "This is why memory costs so much now." Yeah, cuz I'm I'm putting all my ranges in the memory. Okay. [laughter] Uh Okay. So, instead of I guess Yeah, I guess this is technically um a set. That's what internet doggo said. Using an object. Yeah. Okay. But instead of that, we're just going to have our ranges and we're going to iterate over the ranges. Um, so we will have our ranges which is going to be an array that has the start and the end which are both numbers.

Starts off as empty. Um, how does TypeScript work? What am I doing? Oh, this this type goes on the other side. [laughter] [snorts] Okay. And now instead of doing all of this, we're just going to put it in here. So we'll say ranges.push an object with the start and the end.

Um these are both we're going to ignore TypeScript at this point because we know we know that those are actual numbers. Um and then for every value we iterate over all of our ranges to check to see if it's within that. So uh we'll just do uh ranges for each.

We get our start, we get our end, and uh if we still need our value. So the value is going to be the line parsed as a number or the the input parsed as a number. No, line. Yeah, like that. And if value is greater than or equal to start and value is less than or equal to end then total fresh gets incremented.

Let's run it on the sample input to make sure that this works. Oh, we got four instead of three. Dang it. Um, [snorts] let's see. I thought it was inclusive. So, it's 3, four, five.

Oh, if it's in any range, not each one. We just need to we just need to find if it's in any one. So, I'm instead of a for each, I'm going to do a a loop because then we can break out early. We could also do sum. Yeah, let's do sum and we'll just return this instead of a loop. So return sum um and we'll say if some of those values are true so it'll break after the first one total fresh increments by one three. Great moment of truth. Will this work on the regular input? Because we're still doing a metric boatload of iteration, right? Because we have all these ranges and we're checking every single range for every number. We are technically breaking out early, but uh let's give it a try. Oh, it's it's very fast. It's very fast. [laughter] And it

didn't use a lot of memory. Okay. Okay. Great. We did it. Part one. All right. Here's the deal. We're going to read part two. The moment this um seems hard, we're not even going to try it and we're going to move on to Clash of Code. Uh but I will catch up on the chat.

This is fine. What's up, Bio Ross? It's been a long time. Uh, and Mark Boot says, um, greater than or equal to ID or less than or equal to. Yeah, find technically would have worked, too. You're you're totally right. Can you do this? I saw T Soding post this, but it seems like a meme because I think this does some sort of bitwise operation. I don't think you can do this in JavaScript. Can you

just vibe code? Okay. Yeah, I cuz I saw T Soding's post and I was like, "No, that's that ultimately turns into like a bitwise operation. That isn't actually a uh you can't do that in JavaScript." [snorts] Um, yeah. Internet Dog says, "Bro, my solution's two lines and doesn't crash. I think you can do better than putting them in an object." Um, look, you know, I did my best [laughter] and I solved it. It I solved it in one pass for parsing.

There's bet there's definitely ways to do better ways to do it. Um yeah. All right. I got broad. Yeah. I can't believe it. It's okay. I I don't blame you, internet doggo. I I am yelling at myself for my own lacking solutions because I I could do it better. But we're moving fast.

Part two. The elves start bringing their spoiled inventory to the trash shoot at the back of the kitchen so that they can stop bugging you when they get new inventory. The elves would like to know all of the IDs that the fresh ingredient range is considered to be fresh. An ingredient ID is still considered fresh if it is in any range. Now the second section of the database, the available ingredient IDs is irrelevant. Here are the fresh ingredient ID ranges. The ingredient IDs that these ranges consider to be fresh are 3 4 5 10 so and so on. So in this example, the fresh ingredient ID ranges consider a total of 14 ingredient ID ranges to be fresh.

Yeah, I think um this seems doable to me. This um No, see internet doggo is saying you're not going to move fast on part two. It's so hard. No, I think it's great. I think it's easy. I think we take all of the ranges. We combine them for any overlapping ranges. Then once we've combined all of the overlapping ranges into an individual set of ranges, we just count how many values are included.

easy, right? Or am I crazy? [laughter] How hard would it be to uh to collapse all of the ranges? Um, okay. Internet Doggo is saying they tried that and they're still haven't solved it. So, am I naive? [laughter] My assumption.

yeah, an internet doggo is saying, "I think it's going to be harder than you think." But everyone else is saying, "Yeah, that's it." Okay, how about we set a timer? We'll set a timer for five minutes. If I don't make any leeway in 5 minutes, I'm going to stop. But that is that is my solution is we're we're basically going to go across all the ranges and combine them and then count up how many are within each.

Yeah. And Fox says, "Let him cook." Thank you, Fox. [laughter] Let's do it. All right. We'll do it on the sample input. Um, we're going to have part two.

Yeah, I like the idea better of just splitting on the double new line. So, we can ignore the last part. Um, so if we do lines.split split on double new line and grab the first one. That's just going to give us all of the ranges.

So, this will give us the ranges, which is going to be a string array. Um, oh, and then we split that on the new line.

Okay. So that'll give us the ranges. Um we are trying to calculate the I guess the total um fresh total fresh ids. And so that's going to start off at zero. We're going to look at all the ranges.

We're going to parse that range in the same way

and then I'll just make sure everything's working. So, we're going to log it out and then we'll have to figure out how do we combine overlapping ranges because now that I think about it, we might actually run out of space as we're keeping track of what we've seen and what we've seen and what we haven't seen.

Um, so I'm going to I think I'm I am going to have a map. I'm going to have a um well, no, no, not a map. Um, a list of ranges. So, let uh we'll call this inclusive. Well, let's call this reduced ranges.

And this will start off as an empty array. And very similar as before, we're going to have a start and an end. Okay. Now, uh when we come across a new range, we're going to look at all of the reduced ranges we have. If this range falls within one of those ranges, we're going to expand the existing range. If not, we're going to push it in. So, uh we're going to iterate over uh each one.

And I think I'm just going to do a for loop so I can break out once we've found the range. But [snorts] I guess the issue is if two ranges um let's first let's find all the overlapping ranges. So we'll say uh overlapping ranges will be all of the reduced ranges. We want to find any given range.

Let's call it R where R.Aart Start is greater than or equal to start

and r.end is less than or equal to end

number or undefined. I I gave you a um Oh no. I thought doing this in Typescript would allow us to assume that it's not undefined, but we can't. Okay. Why is this Oh, lowerase number.

[laughter] Okay. Uh All right. So, if the start range of an existing range is greater than or equal to the start and um Oh, no. and and the start and the end is greater than or equal to that end then that means this is this is uh inclusive like this is uh range is inside that's one thing we need to consider is it inside the other thing is does it overlap with the start and then does it overlap with the end so this one is it is inside um or we want to find uh we'll say overlaps beginning And this will be if the if our start if our end Whoa.

You can't hear that, but this is going off and it's really loud. [laughter] All right, that was 5 minutes, but I'm I'm making progress. But who who's uh who's engaged? Who thinks that I should keep going? Um cuz I think I should keep going. I think I I think I got this. You heard it. Okay. What's up, Wesley? Good to see you. Uh, Chris Maid said, "This is code that can be done by AI." Sure, but I'm doing it.

I'm the coder now. Keep going. I says, "Keep going. I'm going to keep going." All right, I'm going to give myself another 10 minutes. I think I can solve this in in 10 minutes or less cuz we're we're on our way. So, if it overlaps with the beginning, that means that the end is um greater than or equal to that given range's start.

If the end is greater than the start and the end is less than or equal to the end. Um so thinking about ranges here. So we have a this range is overlapping with this one if its end is greater than or equal to the start and less than or equal to the end. Okay. So that overlaps at the beginning and then we need to also find overlaps at the end

and this will be if the start is greater than or equal to the start. No, no. Um, if the start is greater than is less than or equal to the end and the start is greater than or equal to the start.

I think this is it. And these are all different conditionals because uh inside is start and end are within the start and the end. So that means it's inside the range. Um if it overlaps at the beginning, that means the end of our current range is after the start of the previous but before the end of the pre of of the one that we're looking at. And it's at the end if the start of our range is less than the end but also greater than the start. Okay, we've done it. We found all the overlapping ranges.

If there is an overlapping range um then we'll need to combine them. Uh oh, I'm outside the loop.

If there's not an overlapping range also, what's going on here? How could this be undefined? Oh, cuz it possible that we don't find any.

What is overlapping ranges? It's going to be a Why is it a never?

Oh, we don't want to We want to We don't want to find We want to find all So, we want to filter. [laughter] I swear I I forgot how to program. Okay. But yeah, this will give us all the overlapping ranges. Gez. And then we don't need to do this. Okay. Uh so if overlapping ranges has a length, that means we have some of some of them and we need to combine them.

Otherwise, we'll just push it in because we found a brand new range that did not exist before. So, we'll say reduce ranges. We're going to push in that uh range that we just found with that start and that end.

Um and again we can ignore TypeScript that says this is not possible. Okay, great. Um

yeah, now we just do the combination logic. Um

I think yeah, that's what we'll do. And

We're going to have the combined range which is going to have a start that will be overlapping ranges at zero and the end which will have overlapping ranges. is that one and then we want to loop over the rest of them.

Though I'm I think I want to do this logic to know is it inside or at the beginning or overlaps as we go. Um so I'm actually just going to do this in a loop itself. Um, and we'll say let uh found overlapping range starts off as false. If we found one, it'll be true. And then if we didn't find one, then we'll push it in.

Otherwise, all of this logic will for com combining will be in like a single for loop. So we will say for let I equal zero. Well, I is less than reduced ranges.link. length I ++ for each one. We're going to do our check

and R is going to be reduced ranges at I um which is not null. And here we'll do this. So if

this it overlaps at the beginning, else if this overlaps at the end,

else if this

Um, did I get rid of my comment? Overlaps at the start. Wait.

Okay. Um, if it overlaps at the beginning, we're going to create a new range where the start

is okay. So if if our start wait no no no no this is within I got these wrong. [laughter]

This is start. This is end. This is within. We'll confirm that. But okay, if um our start is greater than equal to the the existing one and less than, then we we don't do we're not creating a new range. We are literally just going to say found overlapping range is true. And we can also just break out of this loop.

Done. Okay. If it overlaps at the start, we're going to create a new range. The start will be the range that we found's start and the end will be no it the start will be our start and the end will be that range's end because we overlapped at the beginning.

So we we start from our range. We end at that that ranges range. Cool. And we will say uh reduce ranges at I equals that new range that we just uh we just created like so.

And then if it overlaps at the end then the start

will be this ranges start and the end will be the end that we just found. Okay. Uh for all for every one of these cases we're going to set found overlapping range to true. And I think this should be fine. The one issue that I'm thinking about is um what if so we're iterating over these ranges and we find one and then we replace it. Are we ever going to hit a scenario where we have two ranges in this list that are overlapping?

I don't think so. based on this logic

because then we can just break out. I don't think so. I think because we're doing it as we go, we're never going to hit a point where the ranges haven't been found yet. Okay, we're close. How many How many minutes we have left? We're done. It took us 10 minutes, but um I I think this is it.

I'm only going to debug for five minutes on the final input, but we've done it. I think we've done it. So, at this point, I'm just going to log out our reduced ranges to see how we did on the sample input.

And don't run part one, only run part two. Okay. So if we look at the original problem,

these were our ranges. 35 104 16 20 12 18. We have 35, we have 10 uh 18 and 16 to 20. Okay, we did something wrong here.

Or did we?

Here was my thought. Do we need if once we overwrite I inside the list um do we need to decrement I?

The thing is this isn't the end of the world because this is still valid, right? We just have extra ranges which is the only issue because 3 to 5 totally valid. Uh 10 to 20 totally valid. Well 10 to 18 totally valid and then um 16 to 20 totally valid. But like technically this should just be 10 to 20, right?

Yeah. Yeah. So, we need to make sure that we've flatten it to only the valid ones. Um,

and if we think about our logic, when we come across 10 to 14, it's the first time we've seen it, so we push it into the array. When we [snorts] come across 16 to 20, it is not within that range. So, we push it into the array. When we come across 12 to 18, um, yeah, this is the scenario where we have multiple ranges that we need to collapse all into one.

That's the issue. So, uh, because we, so we push this one in, we push this one in, we push this one in, and then when we come across this one, we only collapse it with this one. We don't collapse it with that one.

Okay. Am I crazy to think that we now change start and end and then iterate from the beginning again? This could this could be heck this could be make it really bad. But here's what we do. Um if we found it, we replace start and end.

Yeah. In the case where it's inside, we're done. We don't have to care again. I think in the case where we're overwriting it, um I think I'm just going to set start equal to or I'm going to set end equal to R.end and reset I so it starts back at the beginning to search again.

And then in this case, we'll set start equal to R do.st start and set I back to zero so it starts iterating again to check them all over again I think

is that crazy to say

let's try it that didn't fix it [laughter] Okay, I don't think I'm going to debug it anymore. We're so We're really close. Actually, I'll I will I will ask chat for help. If you can if you can think of what I'm doing wrong and what might actually get us there, I'll accept it. Otherwise, we're moving on because I I don't have all day.

Can I just do math.min and math.mmax? Um I don't know. Easy

way is sort them by start first. Oh, good call. Okay, I like the idea of sorting. So, sort before we search. Sort by start before we search because if we hit the scenario where um so we pushed all three of these in.

No, cuz that wasn't overlapping. Oh, read them all in and sort that by start first. That would solve it. I think I think it it actually might be that easy. Yeah, I think it's that easy. I think sort all of the ranges first by start and then we should have all the collapsed ranges.

Okay, we are going to do ranges.sort sort A and B. And we want to do A.Sart minus B.start. Um,

but I want to turn it into, you know what? I'm gonna I'm gonna reformat my code. [snorts] So, we're going to say uh sorted ranges are going to be ranges dot map. So, grab each one.

It gives us the line. I guess we'll call it the range. And we're just going to return an object with start and end. Uh but here we can get rid of all the um the notnull assertion by giving us back a list of the sorted ranges. And here we'll sort that. From there we'll iterate over the sorted ranges and that will give us the start and the end like so. And then we don't have to but we can get rid of all the not null assertions because we have validated all of them ahead of time.

[snorts] Okay, this will work on the sorted input I mean on the sample input. But will this work [snorts] on the regular input? I don't know. I don't know. Let's let's run it. I have really no way of telling.

[laughter] I don't think so. Right. So this number um

well we'll try it. We'll try it. So basically what we need to do now is determine um how many of them exist. So for each one we'll subtract n from start. That gives us the total number that exists within the range and then we have the total. So I'm going to do that. If it doesn't work I'm done. I'm done. So [snorts] um we want to say our reduced ranges.

We're going to produce that into the sum and this gives us the range. Well, we'll get the start and the end. The sum starts off as zero. We'll log the total and we want to do um we want to return sum plus end minus start. That gives us the total number in that range. And so if we run this on the sample input, we get 12 and we were looking for 14.

[laughter] Um, is that not right?

Let's log our reduced ranges again.

So 3 4 5 is three numbers. 5 - 3 is two. We need to add one because it's inclusive.

14. Okay. If this works on the regular input, I'm going to be very amazed. But uh we'll see. Yeah, we'll see. Okay. Not not the right answer. It's too low. [laughter] There's some there's some more collapse ranges we need to uh we need to do. Actually, what if we did it twice? Okay. Okay. Okay. [laughter] What if What if we did it and then we did it again?

um on all of the overlapping ranges. That's the la that's the last thing I'm going to try and then I'm and then I'm done here. Um so do it once and then do it again because you're going to have ranges with different starting and ending values. Um

we'll get the sorted ranges which we will do I think

and then we calculate the reduced ranges in a function. So we'll say the reduced ranges is going to be Um we'll we'll create a we'll create a uh get reduced ranges with the sorted ranges.

Um and then get reduced ranges will um I don't know what it's going to take as input but it's going to do this and return the reduced ranges. What does it need as input? I think it just needs the sorted ranges as input.

Cool. So, we get them. Great. Then we're going to do it again. So, we're going to say we have our reduced ranges, and then we're going to overwrite it with um the reduced ranges sorted, which they should already be sorted, but we're going to sort them again anyways. I think they should already be sorted.

Yeah. So, we do it once, we do it again, then we sum them all together. That is the exact same number. [laughter] Uh what if we sort them by end and then do it again? Um so or in reverse order,

same number. Um but what if we do end N andB.end. Same number. What if we do it in this way? Same number. Okay, I'm done. [laughter] I'm not going to try anything else. Um, those are all the same number, right?

Yeah. If we if we look at our output, there's there's bound to be some overlapping in here. We just don't know where. I mean, is it this like is this number? No, that's less than this number. Yeah.

What if we just do it a bunch of times? Same number. I think it's the same number. Yeah, it's the same number. Okay. I'm I'm a little hard-headed here. I need to stop. I need to stop. Um, [sighs and gasps] okay. You need to repeatedly go through until no overlaps.

Yeah.

Run it until the number doesn't change. Yeah, that's kind of what I was what I was thinking. But but it [laughter] it stays the same. Um after we find our reduced ranges,

man, I want to solve this, but I also want to move on because I've been live for almost two hours. So, let's do some clashes. Um I'll think about it, and if I come up with a solution in my subconscious mind by the time we're done playing Clash of Code, um then, uh we'll do just that. But this will be fun because you can all join in.

And we are going to do a private clash. And let's just do fastest mode. You can use any programming language you want.

I wish there were a better way to check all of these. You know, one time I think I wrote a script that that went through and checked all of these or unchecked all of these. Okay, you can use any programming language. We're going to do fastest mode. Here's the link if you want to join in.

Oh, somebody just said this and I think that might be it. You didn't check when start is less than end is bigger. like when the range is outside. I think I did. So if the start is greater than the start, you're totally right. No, no. Yeah. So well then it's then it's over it's overlapping in here, but this should count for it. So uh if it wasn't overlapping in any other way, meaning it was before or after, this still pushes it in.

I think that's what you meant. But basically, yeah, we have this check to see, have we seen this range already? Um, opposite to the first check. So, if the start is greater than or equal to the start and the end is less than or equal to end, that means it's inside.

That should work anywhere inside though.

Let me know what the condition should be like write it out [snorts] because um if we do end greater than equal to start then that's overlapping here.

Yeah, that's what I thought. So if our range is not inside or overlapping, then we just use the range itself. Yeah, I agree, Stony. [laughter] All right, who's who's joining us? We got a few. Yeah, we got a few. I'll I'll let this go for another minute if anybody wants to try and debug my code.

Like, what am what am I doing wrong here?

I altered the ranges. I did not leave the old one. So basically I think this I mean this could be our issue. And also maybe we don't break maybe we oh okay we do break here because it's not within any existing if it's not within that range. But um if it's here then this is my thought earlier like start back at the beginning and iterating again.

Um honestly I think I think we need to do that. I think we need to start back at the beginning. start start looking again and we overwrite our start. So here we'll say um end equals r.end start back at the beginning and then here we'll say um

start equals r.st start. Go back to the beginning. Same number. [laughter] Man, I can't figure this out. Oh well. I have to leave it. I just have to leave it.

Get rid of the equal sign. Which equal sign?

Yeah, Derp sauce, good to see you. Welcome into the stream and uh thanks for all the follows today, everyone. Appreciate you. Okay, let's just do class of code. I I I get so caught up, but let's just do class of Clash of Code. Here we go. Fastest mode. You can use any programming language you want. First one to finish wins. Here

we go. There's a timer up top if you haven't played before. Code is on the right. You can choose your programming language here. And they do some initial parsing for you. So in accounting, a journal entry is valid if the sum of the debts, positive values, equals the sum of the credits, negative values. You must identify if the journal entry is valid.

If the journal entry is invalid, identify whether there are too many debts or too many credits. Weird. Okay. So um line one an integer of the number of values in the journal entry. Okay.

Okay. And the sum of the debts. So let's say uh total starts off as zero for each value.

Oh the sum of the debts. Okay. Okay. Okay. We need the uh debt total and we need the credit total.

So if this value is negative then we add it to the um debt total otherwise we add it to the credit total and what do we want to return invalid debt exceeds credits. So here we'll say um if the debt total plus the credit total does not equal zero or uh is is less than zero.

Then we're going to log

Invalid debit exceeds credits. So if the debt total plus the credit total is not positive, it's invalid. What do we log when it's valid? Just the word valid. Okay.

Please work first try. No. Okay. Uh, reverse. [laughter] Reverse. No. Okay. We're We're off then. We are way off. So, let's log the um instead of plus equal, do I need to do minus equal on the debts? Let's see. But I am after all of this, I'm going to log the the debt total.

and the credit total

and it should be if it's less than uh less than zero. So our [snorts] debt is 52 and our credit is 53. We need to do plus equals

Um, oh, it's it's I'm doing the wrong thing. So, the the debts are the positive values. That's that's why. And then um if debt is greater than [laughter] [snorts] Okay. Um the debt total is 53. The credit total is 52. So 53 + 52. Oh, I I should I I shouldn't log this anymore.

That's why it's failing. But this is it. This has to be it. Yeah. Oh man. Let's debug this. I think console. Will output it without failing. So the debt total is four. The credit total is -64.

We don't need to do greater than zero. We just need to do if the if the debts are greater than the credits and it's going to be the absolute value of the to of the credits, right? Yeah, that's it. I don't know why that was so hard. Wait, it's not.

No, don't even don't even take the absolute value. Just No.

If credit minus debt is greater than zero

plus Oh man, why is this so hard? Everybody else has done it already. I [laughter] can't even think. I don't know why I'm trying to program today. I know. I know. Today is like an off day. It's crazy. Okay, [snorts] our debts are four. And let's make sure make let's go back to the beginning. Do we have this right? So, uh the sum of the debts, which are the positive values. So, any positive value we're adding on to the debts? Great. Um the credits are the negative values. So, if it's not greater than zero, then it's negative. Amazing. So we've calculated the debt and the credit correctly.

and it is valid if the sum of the debts equals the sum of the credits. Oh, it has to equal

and then the credit would be math. absolute value.

Oh, there's multiple different outputs we need to do. We need to do [sighs] Okay, [laughter] [snorts] we can do this. If the credits are greater than or equal to the debt total, then credits exceed debits.

Just read the problem description. I think that's literally all this is because I I tried to get ahead of myself. So, this goes here. This goes here. And this needs to be credits exceeds debits.

There we go.

[laughter] I'm making a fool of myself. I'm a [laughter] fool. What's up, uh, Mumi Mumu Jamala? Good to see you on the syntax stream. I remember you were in the the coding garden stream the other day. Yeah, welcome in. Good to see you.

Um, all right. If the total number of credits is greater than the debt, amazing. Um, if the total number of credits is less than or equal to the debt total, that's not the case. So, in this case, we have extra credits and less debts.

I think I could just do this first. So like if the uh credit total plus the debt total is less than zero, then it's valid, isn't it?

Because that means the total number of credits is in the in the is positive.

Oh, what do you guys think? If I were battling Scott and Wes right now, would I be would I be struggling? Come on. All right. [snorts] Valid if the journal entry is valid. But what makes the journal entry valid? Um, it is valid if the sum of the debts equals the sum of the credits.

I don't need the equal sign. greater than [snorts] but in this case um

oh do we log nothing

okay except for that oh [laughter] missing my else statement Jesus. Come on. [laughter] Come on. Come on. Come on. Oh my goodness. Okay, everybody.

We're going to do one more, but I don't know if I should be doing this today. Thank you, Dynamic Voyage. Thank you for the claps. Thank you, [laughter] Mumi. We got there. We got there, everybody. All right, let's look at everyone's solutions. If you submitted, click share my code and we'll see how you did it. So, Funny Boy did it with JavaScript.

Uh, grab all of them. Um, oh, I that makes sense, too. Sum them as you go, which was my initial thought because if the sum is zero, it's valid. Um, if it's less than zero, we have more credits than debits. Otherwise, more debits than credits. Yeah, I don't know why I over complicated that. All right, let's see how um Andrea did it with Haskell. Amazing.

This is like uh I think we're using pattern matching here. Okay, we are going to sum

[sighs] mapping over each line.

What is words? Is this a special HA hasll thing that like splits on spaces or something like that? Let me know who who did this. Um Oh, I guessed it. Yeah. Uh Mumi says read is like parsing. Words is like split. So fascinating.

Words splits it on spaces. So that way we get each individual value, sum them all together into the total. And then we do some pattern matching. Would you call this pattern matching or is this just a case statement in HASLL?

Yeah. And thanks for the follows over on Twitch. Appreciate you. Wow. Case statement is pattern matching. Fascinating. Okay. So this one line of code reads it in, splits it, and sums it all together. Amazing. From there, we just look at the total. very similar to the previous solution. So if the total is uh zero, oh look at these little compar comparators [laughter] that's another another built-in instead of like less than or greater than literally lt is less than gt is greater than equal eq is equal. So uh if it's equal to zero valid if oh this is another thing compare total compare which gives us back these comparators. Hasll is a is just crazy.

All right. Uh Ram 10 did it with C. Read everything in. Sum it all together. Yeah. Everybody figured it out. I don't know why it took me so long. Just sum it and then you can compare the sum. Uh [snorts] if it's zero it's valid. If the sum is greater than zero, debts exceed credits. Great job. Uh Mark Boots did it with JavaScript. Same thing. Sum it all together with reduce. Luis did it with JavaScript.

Same thing. Nobody else did the debits and credits like me. There we go. Nice developer Zeke. They've got their debits. They've got their credits. They increase them all and then uh and then compare them. All right. Did we get any other programming languages? Uh Agenda did it with Python. Yeah, we've got our positives and our negatives, which are our debts and our credits and then we compare them all.

Fascinating. All right, let's do it again. Redemption. I'm going to do better this time. And we're going to do fastest mode again. F. First one to solve it wins. Haskell is pretty cool. It's I'm coming around to it. The more I look at it, the when I tried learning it a while back, it was just um it was it was hard. It was hard hard learnings.

Join the dark side. Yeah. [snorts] All right. Uh I'm going to take a like a one minute break, but I'll be right back and then we'll we'll start the clash. So BRB [music] Hey

[music]

[music]

baby. [music]

I was back quicker than a minute because I'm going to remind you that we're having a sale on all our merch on Syntax. So, these pins you can get, they're only seven bucks, but they're 20% off, I believe. Seven bucks. So, they're really cool pins. Get them as stocking stuffers. There's like an internet pin. Um, we also have these clicky keys.

uh with a vibe key cap. You can actually take this off and put it on your keyboard, put it on your tab key if you want. Um these are like 10 bucks, but check out the syntax store. Go to centry.shop. Today's the last day. 20% off everything. Um so yeah, 20% off the keycaps, 20% off the pins, this hat with a cool syntax logo. We have three colors. We've got black, we've got red, we've got gray.

They're really nice. You've got this uh sweatshirt, black on black. Check it out. century.shop. All right, let's code.

Let's go.

Fastest mode. Replace the digits of the number n with the corresponding character. Um,

we are given all of the characters.

What? What is this asking? So, we're given all of the characters. We're given a number. Oh, and then we only want those ones. Okay, I think no. Replace the digits of the number n with the corresponding character.

String obtained after replacing the digits in N. Are we replacing or are we just choosing the ones that have that given indicy? I think that's all we need to do, right? So just read these in. Read that in. Um and then uh split that on the empty string. So we get all of the digits, map that into an array of numbers. So we have the indices in indexes indexes. I don't I don't know the proper uh uh pluralization.

And then we just want to return the inputs filtered given the character and the index where um that index. Oh no, we'll we'll do the opposite. We'll iterate. We'll map over the indices, get each indicy, and then grab that character from the inputs and then join it all together on the empty string.

Yeah, easy. Redemption. redemption. [laughter] Uh yeah. Yeah, I figured it out. I got their asset spark. Uh split filter join. Yeah. Yeah, [laughter] exactly.

Yes. Yes. Feels good. Makes me want to do another one. And I beat the person using Haskell. So, [laughter] by uh 8 seconds. All right. All right, let's [snorts] see how Mark Boots did it. Yeah, they got there. It's just a matter of how hard how how fast you can type at this point. So, uh, split this number into each individual number, find that letter in the original string, join it back all together. Great work.

Agenda, uh, did it with multiple variables. So, uh, read all of the characters in. So, we have our character array. We then have, uh, the numbers, the indices that we're looking for. Um, looks like they're converting that into a string so they can look at each individual indicy. They're grabbing that character, building up the string, and then printing the string out. Nice. Zeke did it in a very similar way. So, map over each indicy, grab the character from the original input, join it back all together. Uh, let's see. Hasll.

Wow. It's like I'm reading a poem. It's like [laughter] um I wouldn't even think I this this looks like documentation. This doesn't even look like actual executable code. Uh so this reads in all the characters. Um and then that's going to read the indices. So um get get the line. So get the line of space separated characters.

Uh split it on the space that we learned. And that's what words word does. Concatenate it all together. So join them back together. Um put that into a variable called characters. Then we read in the number. Um and I guess they defined read a read digit. But this map here is taking a um the string number. So like 1 2 3 4 and then each character it's turning them into an integer. Is that what is that what's happening here?

Because it's technically a string. So like you have like the string one but we need to turn it into the number one. And so I think that's what this is doing because you do zero minus using this from enum thing. This must be a must be a built-in. This basically gives us all of the indices as numbers.

Then we're going to write out for each indicy. Um, if that indicy is in the characters array, wait, no, no, no, no, no. This, this is hard to read right here. Basically, we want to look at characters and grab each indicy. So,

wow. You're going to have to tell me what this does. Uh, Mumi. That's crazy. That's crazy. Um, it's m mapping each of the digits char to an. Yeah. So, that is what this is doing. This gives us each individual string, turns it into an integer. Um, and what's up, Alex? Uh, they did hasll in uni. It's It's wild. It's wild. Yeah.

Check out Alex if you haven't. They have their own YouTube channel. They also do a deja vu. So click click that name. Go subscribe. Uh what the heck is that? This is Hasll, my friends. [laughter] Okay. So Mumi is saying this is a list comprehension. So all of our Python folks out here might be familiar. So chars exclamation mark I is just characters at I.

So this just grabs Oh, that's weird. But that makes sense. So we are we have all of the indices given that indicy it's like reading functions from right to left too. This is so freaking weird. So given that indicy grab that indicy from the characters array each one list comprehension to to grab each one write it out. Amazing. Amazing. Um let's see xbx. Let's see how they did it.

Yeah they built up a string. So, for every number, uh, grab it from the list of characters. Nice. All right, we don't have any other languages. Cool. All right, we're going to do one more and I think I'll call it for today, but let's just do fastest mode again. I [laughter] my heart my heart can't handle not being able to solve like a shortest mode. I I need I need a uh confidence confidence boost because that that first one really threw me for a loop.

[sighs and gasps] Yeah. Oh, yeah. Learn you hasll for great good. That's the name of the book, isn't it? Learn you hasll for great good. I think it's like a a book. Yeah, it's an online book for free.

Yeah. Yeah. Check it out.

Uh, [snorts and clears throat] thanks Mark Huggin. That again I think my I'm just off these days. Like it's taken me so I I guess because I'm not practicing algorithm problems as often because I I used to do like several code wars a month, right? I was staying up to date with that style of thinking, but I haven't done that in months.

And even when we do Clash of Code these days, like I it's so hard to think. Um yeah, my [snorts] I I'm glad that you could laugh at my expense because I was not having a good time um that day. What's the best thing in the market right now for front-end devs? You mean like what who's hiring for what kinds of skills? Is that what you're asking?

Because I don't know. Oh, yeah. I know, Mark. I mean, we all were messing things up, [laughter] but I was struggling that day for sure. Like, what framework? I mean, React is definitely going to be the most employable. Um, if you're in certain parts of the world, like a lot of Asia, some of Europe, Vue.js is very popular. It's not as popular in the United States, but it is used a bit. Um, [snorts] AngularJS or Angular, not Angular.JS, Angular is very popular uh in enterprise and at larger companies.

and everything else it varies, but people use spelt. If you want to find a job, React is probably the answer. Yeah. Yeah. Great, great call out, Internet Doggo. I need to stop shilling the the syntax merch. But this is the last day. The last day for the sale, so you can get 20% off. You can get this beanie. We have three different colors. We've got red, we've got gray, we've got white.

Uh, red, gray. No, we don't have white. Gray. We have gray, red, and black available. Um, actually, I didn't even show this. This internet hat is pretty flipping sweet. Look at that. It's big, fat, and embroidered. Um, you got these these clicky keys with a vibe vibe key. Also, these pins.

Everything's on sale. Buy this merch. It'll make you a better developer. centry.shop. Okay, let's do it. I'm going to win.

Given a sequence of integers, count how many peaks it contains. An element is a peak. If the peak has elements on its left and right, the peak is strictly larger than its adjacent elements. Great. So, we're actually going to go to less than n minus one. And we'll say peaks starts off as zero.

Um, and what is what is it reading in here?

That's going to give us each individual number. So um if

inputs uh we don't we don't need to do this. So if inputs at I and actually we'll start at one. So if inputs at um I

is greater than inputs at I minus one

and inputs at I is less than inputs at i + one, then that's a peak. So add one.

And we're only going to go up to n minus one. So we're not looking at undefined. Um but then we'll check the very last one because if uh inputs at -1 is greater than inputs at -2 then we have one more peak. It's the last one in the array which is a peak

peaks. First try. Dang it. Um, do we need to parse these as a number? Let's do it. So, we split it and then we'll map it to a number. I don't think we need to do this though. And then we start iterating at one.

And we're going to look at we don't need to look at in minus one. It's weird that they give us that, but we could just do this.length minus one.

What's the input for test number one? Three is a peak. Four is a peak. Six is a peak. No, it's not because four is not a peak.

It has to be on both sides. We're not accounting for that though. Yeah. So, three is a peak and six is a peak. Um let's log uh inputs at I to find which peaks well we're finding none which is the thing.

So we should find it on the first the first pass because inputs at I is three and that is greater than one. We need or no. Um, and no, we need and because is it, uh, greater than. That's it. Needs to be greater than. That's the answer. First try, not first try. [laughter] No. No. I had some wrong. I only got 70%. Oh, I submitted too fast.

Oh, man. Rip. Rip. Rip in pepperonis. Right. [sighs] You get an oops 500 on centry.shop. Try again. Try again. Man, [sighs] as someone who still supports an internal AngularJS, you triggered my PTSD. Sorry about that.

I only got 71%, man. Okay, we're going to do one more. I gotta I gotta get in the top three. I gotta get in the top three. All right. Um let's see how Andrea did it. Okay. I don't want to decipher Haskell again. [laughter] I really don't. Um but it looks great.

Great job. This is some some good looking code. [laughter] This is This is insane. This like again it doesn't look like code. It looks like Yeah. I don't know. Cool. Great work. Uh Mark did it. If it's greater than it and it's greater than that one. Oh, so we didn't have to look at the last one

cuz the last one isn't considered a peak. That was my issue cuz I did I did a peak on the end. Dang. Yeah, Zeke did the same thing. Funny boy, same solution. agenda. Let's see. I don't know why they got it wrong. They got a 70% as well. From one up to the length. Oh, I think you needed to do length minus one, right? Maybe.

Yeah. Nice job, Wesley. I submitted too fast. Um, but Leslie got in. Great work. I should not have done this. I don't know why I thought I needed to do that because our loop only goes to one before the end.

And then if the last one is bigger than the second to last one, that's a peak, right? Does that not count as a peak? That's why I got it wrong. All right, redemption. One more time.

So, I guess apparently the sides couldn't be peaks. Oh, well. All right, last one. I'm going to get in the top three. Actually, we we have no idea if I will because there's [laughter] no there's no idea what the challenges are going to be. [snorts] Um, but I really I really want to get in the top three. We're not reverse mode. I'm I'm uh I have PTSD from the uh the video that we just published if you haven't if you haven't seen it. Um the video we published today is uh me, Scott, and Wes all doing reverse mode.

And we we had a heck of a time. Wes did pretty good except for a couple of them, but we Yeah, we had a heck of a time trying to figure all of that out. All right, I'll give you 10 more seconds. I'll do one last plug for the merch shop, Sentry.shop. We got hats. We got uh what are these called? Sweatshirts, toques, beanies, [laughter] what? Jumpers. Jumpers is what they call them in in uh Canada and the UK. We got pins.

We got clicky keys. We got other cool stuff. All of it. 20% off. Today's the last day. So, get you some get you some cool syntax merch. Hoodies. Yeah, it's a hoodie. Um let's do this. Focus. Focus. Lock in.

Here we go.

Determine how much of each type of sandwich bread is left over. I'm just shrugging because there's so many words. Is left over if the maximum possible number of sandwiches are made. You're given in lines where each line has slices of a loaf of bread and the type of sandwich the loaf is used for separated by a space.

Okay. The types of sandwiches each loaf can be used for are a BLT which uses two slices of bread, a club, which uses three slices of bread, and Sky, which uses five slices of bread. Loaves of bread can be combined for sandwiches of the same type. So, we are given the number of loaves of bread. We're then given an integer, which is the slices of bread, a space, and the type of sandwich.

Um, oh, okay. So, we just need a lookup table. We'll subtract that from the number used.

Easy enough. So, we have our uh lookup It's going to have BLT, which is two. It's going to have club, which is three.

Oh, I think I'm going to have to reverse this. And it's going to have sky, which is five. So, we are given the amount of the number of slices of bread we have and the type of sandwich we're trying to make.

So, are we going to log multiple times? Yeah. Why are there three outputs? Oh, how many we could make of each? Gotcha. Gotcha. Okay.

So, um, we're going to iterate over BLT, or actually, we're just going to iterate over lookup object.key keys on the lookup. That's going to give us BLT, club, and sky. And then for each one, we get our type T.

Um, oh, wait, no, but why why do we have the type here? Yeah, somebody already did it with PHP. [laughter] Please, engine feeder, prove you're not a bot. Say hello in the chat because that's crazy. [snorts] Um Oh, that's how much bread we have. Okay.

Okay. So, um what do we what do we have? We need to calculate that. Um and this is all going to start off as zero.

So have at the sandwich type is going to be uh s times our lookup for that sandwich type. No, that's not right, but we'll figure it out. [snorts] Okay. So, the line is the slices of bread

and the type of sandwich the bread is used for. So, we have Oh, this is so hard to even figure out what they're asking. Okay. Each line has slices of a loaf of bread and the type of sandwich the loaf is used for separated by a space.

So we have five lo loaves of bread and and we're looking for BLT. We then need to output.

It's so hard to figure out what they're even asking. [sighs]

Yeah, we don't even need to um

A BLT uses two slices of bread. A club uses three slices of bread. And a sky uses five slices of bread. We have that stored here in our lookup. Great. We're now reading each line. It is telling us we have five slices of bread and we're trying to make a BLT.

Oh, no. And we made a BLT. Okay. So, um, we're going to have our total slices of bread

and we are going to increment the total by I need to look at all the other test cases cuz when would we never when would we ever have more than Yeah. So, is this telling us we have 15, 19 total loaves of bread,

an integer s, slices of bread. Okay, we have slice five slices of bread. Great. A space, the type of sandwich the bread is used for. So, I just used five slices of bread to make a BLT.

It takes two to make a BLT. So, we just divide it. Oh, no. Like, we subtracted. Okay. And we're just we're going to do that each time, man. Okay. So, how how much does it take? How much does it take? It's going to take our lookup at the sandwich type.

Great.

I'm just so confused why we have to output three times because I could output for this to say Um, [sighs]

man, I need to look at the other test cases. The test case with a single one is is so not it. Okay, we have 10 slices of bread and we're trying to make a BLT. It takes two slices to make a BLT. So, um, oh, I see this is why. Oh, no. No. Cuz see, see, you're going to log. See, it logged multiple times. Okay. I feel like I'm understanding it and then I'm not.

No matter what, we are always only going to output three lines. We're always going to output three lines no matter what. So, we need to do one pass to calculate uh leftovers for each time we try to make a sandwich. That's what we need to do. We're we're calculating leftovers.

Um [snorts] okay. And uh at the end of it all, we are going to log three different things. So we have um let's call this leftovers. And it starts off as zero for each one of them. And then for this given sandwich type, we do leftovers at that sandwich type, we're going to increase it by a certain amount. How much do we increase it by? We take the uh lookup for that sandwich type that tells us how many we can how many it takes to make it and then we divide that we get the remainder of the division um for how many slices we have. So we take the total number of slices we have.

We divide how many does it take and we want to know the remainder of the division. How do we do that with modulus? [laughter] Okay. Oh my goodness. So now after it's all said and done, we can do leftovers.bt, leftovers.club, and leftovers. guy.

This is the uh worst worded problem in history and we're not we don't even have it right yet. Okay. So for test number three, we tried to do 14 clubs and a club takes three.

So that would give us a leftover of two. And then we try to make seven BLTs. They take two per So we get a leftover of one BLT.

Then uh we try to do eight clubs divided by three. So we have a leftover of two. So this gives us a leftover of two. So we have a leftover of four. And then 12 clubs would give us zero. So that we should have leftover of four for clubs.

But given those leftovers, when you combine them together, we can make one more. Cuz if there's four leftover loaves of bread for clubs in total and clubs take three, then we can make one more club, which makes three leftover pieces of no one leftover piece of bread for clubs.

Okay. Um, and it should be one. So, we actually need to add up the number of loaves of bread we have in total

because the on these different lines we could end up with leftovers that we can then make more for. This is dumb. This is the This is just dumb. It's so dumb. [laughter] Um, I think I can rate this problem, right? Can I rate this? Because this is dumb.

This is so dumb. Zero stars. Um, okay. [sighs] We need our totals first. And this all starts off as zero. [snorts] And we will increase our totals at that sandwich type.

And actually, let's let's save this math. This we're going to use this math down here. So, we're going to say our totals uh plus equals lookup at that sandwich type. No, s totals at sandwich type equal plus equals s.

Great. So, we looked them all up. Great. We've added them all up. Now, we can do the the proper the proper thing. Um, and we can say our BLTs are going to be our totals at that sandwich type BLT

modded by um, lookup or BLT.

That's it, right? Yeah. Okay. One minute to spare. Um, please give me a chance to rate this problem because this is bad. This is a bad problem. It's worded bad. The inputs are in a weird format because it's like the number of inputs here are not the same here. Um, it's extremely hard to parse.

Get rid of this problem or change it. Just change this problem. [laughter]

Oh, Wesley. Yeah. So, Wesley got it really fast. So, they just said, um, Oh, they're on YouTube. I understood the challenge the first time. Don't know why, but hearing you explain, I see where the problem is with the text. [laughter] You figured it out on the first go. I couldn't. Um, yeah. Did you figure it out, internet doggo? Cuz that was hard, man.

Make the sandwich you have bread left over. How many of each type of sandwich could you make with the leftovers? Yeah, you can rate after you submit. Where's it at? Um, oh, I think I have to wait for the challenge to be over. I'll do that. I'll leave this tab open, but we'll look at everyone's solutions first. [snorts] All right. Uh, Engine Feeder did it with PHP. Uh, I don't know if you st you stuck around engine feeder, but are you real or are you a bot? Because you did it really fast.

And um, you did it with PHP, so I think you're a bot until proven otherwise. [laughter] So, let's see how they did it. Uh, they have the totals. Yeah, they add all of the totals up and then they print the totals out modded. Easy. Uh, XBX did with Cotlin. Very cool. So, Cotlin, if you're not familiar, is uh how uh can't we do Android apps with this? It's like the new Java.

Does it run on the JVM? Yeah, it runs on the JVM. No, it's interoperate fully with Java and the [snorts] Oh, there's a there's a version that doesn't run on the JVM apparently. Cool. Yeah, you can write Android apps with Cotlin. I think now there's a way to do like crossplatform Cotlin apps, but we're looking at Cotlin code. So, um a similar idea. We we need a map to figure out the totals of each one. So, I like I like this mutable map. Um for each one, we increment the type in the totals and getting the value for the type. Um, oh, basically what is the current value?

Add it onto itself, store it back into the the map. Cool. And then we have our the amount of slices of bread it takes for each. We do the modulus. Good to go. This not not I'm guessing it's probably similar to like the not null assertion. Like we know that BLT club and sky exist in this map. They're not going to be undefined. So that tells us, hey, don't worry. We know what what we're doing. That's my assumption. Um, all right. Funny boy did it with JavaScript.

Look at that. Single letter variable names. But yeah, similar thing. So, sum it all together. Map it out. Uh, mark boot with JavaScript as well. Oh, they did a reduce. Very nice. So, turn that list of numbers and sandwich types um into an object of totals. and then mod it by their values.

I like that mark boot. It's very good. Uh Andrea did it with Lua this time instead instead of Haskell. All right, this is a lot easier to read than Haskell. Um so we have our map for one up to the number. We're going to read every line. We increment the total amount that we found on the map.

Log it out. Yeah, Lua is it's just it's just JavaScript [laughter] in a way. It's it's very similar. [snorts] Nice. And then uh Wesley did it with TypeScript. Nice. Got some type annotations in here, but it's the same as the JavaScript solution. Just increment our totals and then mod by the values. Okay, I got 10th place. Not my finest hour. And I need to go. Oh, let's see. Lastly, let's see how O did it with Bash. Wow.

Ahawk, are you a bot or are you in the chat? Ahok, cuz it's cool. [laughter] O itself is like a full-on programming language, isn't it? Like, but it's available inside of Bash. Yeah, it's a scripting language designed for text processing. It's funny to me that you can use o when it's a bash challenge, but I guess with bash you can run any system program that's available on wherever they're running this code.

Fascinating. This is this is cool, man, because this is like reading like it's passing the input into this program that's running with O. Um, and for each line if like skip the first line, every line after that, we're building up this map of totals.

Yeah, that's what this means. I think this means skip the first line because we don't want to read the number of lines. We just want to read each individual line. Um, and dollar sign one is going to be the total count. Dollar sign two is going to be BLT club or sky. Wow. And then when we're all done, we have our map.

Yeah. Mumi is saying, "Nr is always the number of the current row starting from one." Fascinating. Okay. Yeah, I am rusty. Maybe I just got to do more streams where I'm solving algorithmic problems. We we'll probably bring back the the code war streams [laughter] on coding garden so I can stop being so rusty. Uh but that's all I got for today. Thank you everybody for tuning in. I It's so cool seeing everybody's names in chat.

Be sure to follow the syntax channel if you haven't yet because we'll be streaming on this channel every now and then. Um, again, thank you everybody over on YouTube that watched. Um, if you're looking for something to kill the time with, head over to syntax.fm. Got a ton of really fun videos for you to watch. Um, especially the one that we launched today. I I'll give you if you're looking for ways to kill time.

The one we released today is pretty funny. Uh, the one on ordering pizza with code was really great. If you're looking for things to buy for Christmas, we did a gift guide. Uh Scott has some cool CSS tips in this video. Um Scott and Wes talk about some like advanced CS. Just Just go watch the whole channel. We we just we do really cool stuff. Yeah, Scott also did uh creating an input range slider that's like custom styled with no JavaScript. We also re recreated the Lando Norris effect.

Syntax is cool, man. I'm so lucky to work there. [laughter] just when when you're done with this, go um go watch the syntax because it's great. What's up, Chevy? We're actually on the way out. Yeah, I'm I'm going to find somebody to raid. If you're watching on Twitch, hang around. We're going to go raid. Um if you're not watching on Twitch, go over to the Twitch site and see whoever we raid. But this was great. Thank you everybody for tuning in. Um I'll remind you, check out Debug December. That's what we were doing today. And there's going to be a raffle tomorrow and several other days.

So, get your solutions in and you'll be entered in to win those win those raffles. Okay, that's all I got for you. This has been great and uh I'll catch you in the next one.

Quote it with credit and a link. Quoting Syntax