 Hello, and welcome to today's stream Advert of day, Advert of code day six Sorry for being late. I was playing around with chat GPT making it tell me stories and the about Various things like the creation of Haskell But in the style of Tolkien All right, it's good fun Is it gonna replace people? Probably not, but I know that it will empower individuals to do a lot more with a lot less which is Not a bad thing Anyway, we're gonna do day six. We've been doing doing it every day. So a bit later today But maybe that's better. I don't know maybe more people are Available at this time. So we'll see see how people like it Yesterday we did some stacking And there was a lot of stacking actually, but most of it was just parsing and we had to Take this same This diagram here and kind of make it into something we could compute on Uh, so we did a lot of parsing and then the actual problem which is Insert into an int map folder boom But That's how it is sometimes um The old videos are up on the youtube. So if you want to see previous installments Uh, you see how people Write code in Haskell prefer to do that. Uh, I think the 2020 edition it was actually included in a paper Uh, which was like a study on how people write Haskell programs So We're doing good work here folks. Anyway, let's get started with day six Um Oh no, so we're on our way to an expedition to get star fruits. We're picking up fruits along the way But we're also getting them for solving tasks I don't know the lore Is a bit thin I feel but That's okay Right. So we're On our way to the fruit grove star fruit grove We get a handheld device And it's a communication system ah Maybe this is going to be like one of those um That kind of running things right It was a lot of fun last time Like I think in 2020 we had like this computer and we kept Maybe it was 2019 we had like a computer and we kept having to self modify the code and stuff. Anyway, so let's make sure we do it Extensive an insensible way today, right? um so I've had a significant experience dealing with Signal based systems Yeah, we did these Huh. Yeah, this is the int code. It was so much. I feel like I did it uh in 2019 I really definitely did the int code stuff and funny This is 2021. Yeah, I mean we did things in 2021 2020 we didn't do everything We managed to get quite far Oh, but 2019 apparently not all right Maybe I wasn't logged in at that point Because I feel like I Yeah, okay, I did it in 2017. I've been doing it, right? Anyway We're doing 2022 now Surely you'll have no problems. Let's do inspired by community broken He's a lock onto their signal. The signal is a series of seemingly random characters That device we use one at a time Okay, we We need to detect the start of marker In the protocol being sbs the start of a marker is indicated by a sequence of four characters that are all different The device will send your subroutine a data stream buffer And your server needs to identify the first position whether for most recently received characters are all different Specifically needs to report the number of characters from the beginning of the buffer to the end of the buffer And the first search for character marker Okay Okay, let's see um So I think this will like we're doing like a sliding window here, right? um And so either it's in the first four, right? Or and Okay, let's see Other first three characters will read There have been enough characters needed to find a marker. First time mark should first have the four characters received Making the most recent four characters and j q j because j is repeated. This isn't the marker The first time a marker appears is after the seventh character Arise once it does The last four characters you are j p q m which are all different in this case Your subroutine should report the value seven Because the first start of park packet marker is complete after seven characters may process Okay, so we have the an example That's out of here new file example Okay, and these are actually many examples, but We're ready to all the examples, huh? Okay Okay, so this is going to be easy to parse because it's essentially just a character stream, right read input File path to IO a list of stream Read input today is just gonna be Lines over read file read file Ah, but I need to do f map Because we're doing dot free. Okay Let's print out the example read input an example print gtao base three six And six dot chat We didn't save the example Okay, and we're not gonna have this line actually All right, we have the Arrays of strings Okay close others Now let's have the example right here Okay, and I want day six. Yeah. All right, so we have to check um First of all, we have to check how many there are. Okay, so wait, okay, so Because yeah, so value seven because the first startup argument I completed after seven characters have been processed. Okay Let's see, um Ask one takes a string and returns an int Ask one So we're gonna we're gonna process the initial for first, right? So C1 C2 C3 D4 and then the rest equals Okay, let's actually produce the first. Yeah c3 c4 Okay, so then we just say if let's import import In a dot set Okay, let's actually make this a bit different Equals task one prime A zero Task one prime a N in okay, then we actually match on them equals Okay, now we're just gonna be we're gonna do this crazy Stupid right um So this was position it actually starts at position four, I think Okay, so let's see here, uh, where S equals Let's import qualified Qualified as set let's say as is set dot from list C1 C2 C3 C4 uh So, uh, we say if length S equals So the size of the set is four. Let's see set dot size equals four Then we return and so um Basically if we have four unique characters Otherwise We do task one prime and plus one a C2 C3 C4 We we we you know, okay, and now let's write a task one prime Error End of input without marker I think this will work. Let me see print task one Uh print a map right crush 7 5 6 10 11 7 5 6 10 11, okay so That's a chunky boy Uh, maybe we're doing it super slow. I mean so we are doing it super slow well Okay, let's see because we should actually just kind of Remove from the set add to the set and So what do we know here? Okay, so Yeah, okay. Let's just see how how slow this is. I because I don't think it would be that slow If it is super slow, we change it actually Probably will be slow for task two. I don't know what task two is but But it usually gets slow. Okay, let's see if 16 25 is good All right, we got the first one now. Let's see if our Being lazy Actually didn't work out The start it's girl you're taking packets. We still isn't working. It's like other little messages. Oh my god Except because it's a 14 distinct characters rather than four ah now 14 We're not gonna do that right now. Let's see now. Let's rewrite this in a non lazy way Okay, uh Okay, so here we have the list And then this is gonna be this is gonna start off at Zero I'm just gonna start off at 14 Okay, um We are gonna say Case things set dot I had sets outside. Yeah So this set here, okay, let's say So what we should be doing is like not constructing the set every time right we should be Like adding the last one to the set And taking the first one out of the set and maybe the last okay. Let's let's let's not be so lazy. Let's say a Let's try and it's actually Yeah, okay, so see if we have to add c I see and see yes, okay now let's say So we start off with um Does we need to kind of keep track of the order of the letters? Okay, let me Uh, let's just see where That's equals take 14 What about a s comma r equals take 14 So we're gonna say a split at 14 Let me just see ggi a split at Three one two three four five Yeah, that takes three. Okay Okay, so this is gonna be the start and then we take we take 14 of them Let's see Task one right 14 equals Where let's call this Okay, sorry for being a bit confusing. I'm trying to kind of keep so we're gonna keep the track of the list It's gonna be like of length 14 and then we're always popping off the first one and And adding the latest one. Okay, so let's actually start at 13 here And then we are gonna start with We're gonna start with a set dot from List s and we're gonna start at we're gonna take 13 of them And we're gonna Give it the rest of the list And this is imp. Okay Uh, okay Ask one prime and A This is actually not gonna be s s Set up from list s Okay, so here we have the a In set In set charts we have the curve set And we have the next character and the rest of the characters Equals, okay, so pc pcs Previous characters previous characters set. Okay now We have a set of 13. Okay, so if c set dot member Curse set Now let's do it like this case if the c is the member so so adding the 14th character in um If it is a member Equals, then we return then we see when we we we return uh n plus 1 otherwise Except it was just 14 distinct characters So this is actually gonna be a Not set member right case a Not so if it's not in the set Then we return n plus 1. Okay, so otherwise it Ah, otherwise it's in the set otherwise it's a otherwise it's in the set so we um I think we're doing something wrong here, but let's see otherwise. It's in the set. So we do task one prime and plus one We'll do pcs Uh, and we append c to it bcs plus plus c And then curve set. Okay. How is it a set? Uh, that How do I uh remove from a set? How's everyone doing? Is this add and code hard to solve? and I would say that it's harder to solve than the ones we've seen so far See set up in a sense that like the input is actually Like it you don't have to parse it all right But uh, we're actually having to do some work. Okay. I think this won't work But um, but I want to see what it does 14 14 15 14 15 Yeah Okay, so we're doing something wrong. So first I was thinking like if we put it in the set Do we actually have to we can just keep putting in the set, right? I think we can just keep putting in the set Um I think yeah, let me just check Okay, we don't need any of this task and Curse set No, hey, it's all Uh, and then we have the C and Cs Okay, uh, if set dot size set dot size cur set equals 14 then uh Then we return n otherwise That's one prime n cur set set dot insert Wait It's insert. Yeah. Yeah C set out insert set Cs right because we want to see 14 distinct characters But we probably this probably won't work because we haven't we don't delete anything out of it uh Of course we need to do n plus one here 19 23 20 28 25 19 23 23 29 26 Okay, it works for the example But it doesn't work for anything else Yeah, we still we need to um, we need to remove the Ones uh that we previously added Okay, so if the set of the size of the set is 14 then we return n otherwise We insert this one, uh, and then a pc Set dot So now we we kind of we we remove that So we're keep we keep adding to the set It's just like the cs is Being empty equals Okay, I know said it's not a good message actually, uh, but uh Okay, so the set of the current set never okay, I think I might need to Okay, so it's a prime Yeah, so we like we insert and then check How's everyone doing today? have you tried Have you guys tried chat gpt? that uh Because it's uh, it's a lot of fun to play around with actually maybe I just I don't need the Maybe I don't need the Let's just do it the bad way. Okay Uh pc pc Yes, let's actually use a data.sequence here That's cool. So then we can Add to the beginning and end and it's good. Um Okay, so let me see no sets Import data Okay, um now So this is like a data structure that's it's not just the list you can always like operate on the front But for this one you can actually operate on both ends efficiently Okay, so uh We don't do c except from that we do um seek dot from list a s And then we just we don't need to seek for our For the rest ah Yeah, okay So here we have a and we have pc And it's this funny operator saying Okay, this is the front right, okay, so We do uh This is the front of the sequence Okay, so case uh escu and Okay, sequence. Okay, so case member I feel like we're doing uh too much here So here we will be That's not a workout the key things, huh? We just take the first s So pcs here Then we say R Say that 14 17 14 15 18 That's not at all 14 distinct characters rather than For so if we were doing this we're raised right we could just have like indexes and like okay, so Too early, right? So if the if the if the new one is not just go back to them So the thing is now we check but we don't check if all of them are distinct All right, so let's actually have the cursor here set up So we have the cursor here There's said What are you complaining about? This is supposed to be uh, no, yep Okay, so we have the cursor set. So we actually check a that size pcs plus I think maybe just the set dot in search Okay, so and I want to delete the So yeah, so I think we were like deleting before we were adding So now I want to I want to insert first I want to delete and then I want to add because I think before like we would delete kind of twice, right? PC Ah, but also yeah So the thing is that we were deleting But like maybe we'd seen it twice, right set dot from list or set So actually we're not gonna Do any clever things here. What is wrong here? Good much type a All right, 19 23 20 20 20 26. Okay, so the problem was that We were gonna be kind of adding and deleting from the set The thing is like if we'd seen a character like four times in the set Like the deletion of that character would delete it Like a lot of times, right? So this one works Like it was quite fast Username time 2 to 15. Okay. I mean, yeah, it took We're using all the commands here It took less than yeah 15 milliseconds. So that's just kind of Cheatsy boot time. Let's see All right Ugh That did take us longer than usual, right? but we did it And yeah, so I think like we were thinking about it wrong, right? We were gonna we would wanted to have this current set and then we wanted to You were I wanted to kind of add to it and delete from it, right? But uh Yeah, we should have been using something called a multi set, right? Which is also known as a bag. So then you Yeah, it's a set but So you kind of insert and delete into it, but it doesn't have an order And at that point just also it's also we could have just been looking at the PCS prime, right? Okay, well We did it And yeah, like I said, it's 15 milliseconds, right? So now we could have probably do this in a more optimal way, but it's It just seems fine, right? put example A6 All right, uh That's it for today We did have some viewers a sometime. I think uh a Some of my biggest fans are in the us very busy right now, so There was less chatting than normal. I think but I think I need to Because I put it on twitter, right? But I think like Last year I put it sometimes on Like the Icelandic programmers group and then I would get people Anyway, but also we should be on time, right? But uh, yeah, that was it for today and And you'll find me, uh playing chess after this or actually Yeah, probably have to review some homework from students. Anyway Thank you for tuning in And uh see you tomorrow for day seven, but you know, we've been doing this for almost a week now So I think, you know, we're doing quite well All right, but thank you for the day