 All right Good evening everyone Welcome to day nine of Haskell in the advent of code You may know it's some changes on the channel. That's because it became a twitch affiliate yesterday, so I Will officially get paid If you like if you subscribe So now you can like like and subscribe Especially like if you have a you have like Amazon Prime And you've like connected that to your twitch account you can like subscribe for free and If anyone here does that then Don't be cool because I literally have zero revenue From the channel which makes sense I just got affiliate yesterday, but I want to see I want to see what happens you know, how much should I get first subscribe and Also, I'm like a paid PhD student, so I don't really need the money but I Kind of want to make at least one cent Because if you're getting paid for something that makes you a pro streamer Which is hilarious. All right, let's look at day nine of the advent of code Make the deer day nine go to day nine Touch day nine touches touch test input and touch So how's everyone doing today? We had a we had a squadron of fighter jets fly over the city today They claim it was because you know, it's like a Christmas thing they do, but I know the truth Use to celebrate might affiliate status There's no denying denying it. All right, let's say It's open day nine Actually did some hacking after hours yesterday and I changed the Parsing of the initial problem from like string based parsing To byte stream based parsing, which essentially means that We're not converting it to a length a list of characters anymore. We are working with the raw bytes when we when we are When we're transcribing it or like when we are Taking it from the raw input to the actual input that we look at Because I thought it would perform part because I looked at some streams, right and the and the rust people they're doing like 160 net like micro seconds And then I looked it up and like fiddle around I got mine to like three milliseconds But then I tested it even more and turns out that Ask but ghc the program that the program itself takes like One and a half milliseconds to start to initialize the whole runtime system So and like rust doesn't have to do that because it just compiles everything away. So I mean, that's cool It's cool that we I Mean we have a lot we the runtime system does a lot, right? It takes a thunk see the value It's a thunks it doesn't all the things that we needed to do but Would be cool if you could do microsecond level time so Let's look at it. This is the moving cup of the day. It is the cup of the summer 2020 I think or 2019 No, yeah 2019 summer cup. I like this cup, but you can see it's slightly green Or like, you know, it's not green, but it's reflecting some green. So it's like being Green screened. All right, let's look at today's problem Yesterday, we had the creator of advent of code come in and say hi Be cool if he came again or if they came again, I'm not sure My name is Evan, but You never know All right and coding error. All right, we have the video game and we have Okay, an open data port on a little screen in the seat in front of you. So the ports are on stand standard You manage to connect it to your computer to clever use your cell papal clips. Nice On connection the port opens the service of numbers your possible input. The data appears to be Acquainted for the exchange masking addition system Xmas Nice Appreciate the puns Xmas starts by transmitting it a preamble 25 numbers after that each number you receive should be the sum of any two of the 25 immediately previous numbers The two numbers will have different values and there might be more than one such pair For example suppose you're pretty humble because this is numbers one through ten or five in there any more to be valid The next number must be the sum of two of those numbers 26 will be a valid next number will be one plus 25 And thanks for the follow a little more 49 would be a valid next number this is some 24 25, but 100 would not be valid No, two of the previous 25 numbers sum to 100 50 would also not be valid although 25 appears in the previous 25 numbers the two numbers in the pair must be different Okay I oh, yeah Happy birthday on Saturday Uh, how how does a stream birthday celebration go? I don't know like I would play happy birthday But someone copyrighted that song So I can't but uh, yeah happy birthday le more Wow Everyone's birthday is this week. Actually I have some I don't know sad news but I'll be so i'm going back home to iceland on On friday actually I take the train to stock home on friday and then I fly From stock home on saturday So on So on the friday itself Um So on the friday and saturday i'm not going to be streaming I'm currently i'm currently living in sweden. I've been living here for five years now So i'm technically i'm eligible to become a swede like I can get a passport if I want but the problem with having dual nationalities is that like Declaring taxes in the u.s. Become such a pain. I was born in iceland as you can tell from my icelandic came order This is supposed to be the icelandic flag. It's like an abstract version of it All right, but let's uh, let's start the problem like this problem should be a bit easier than the ones That like last two problems. I think so Supposed to 26 number is 45 and the first number no longer an option As is more than 20. Okay, so it's a rolling thing. Okay, cool Now for the next number to be valid There needs to be some bear from on 19 21 to 25 or 45 that add up to it Okay, so we can't just like pre-compute things and then look up in a map. We will actually have to We'll actually have to do some proper work Okay. Yeah, wait 26 would still be a valid next number Is 1 in 25 um Are still is that true though Isn't wouldn't wouldn't I think that's not true. I think because you are at 45 then one would drop out So 27 will be valid, but 21 will 26 would not be valid But yeah, I was born in iceland. It's a tiny Well tiny it's bigger than danmark, but the population is pretty low. So it's uh, it's a small island country Best country in the world Doesn't everyone say that though I like it. I like the people Uh, so here's where I'm right now This is Gothenburg, Sweden And I'll be going here on Friday and then I fly from here All the way to iceland. It's gonna be great Anyway, let's keep keep looking at the problem. So I think this 26 would not be valid because Is Because it's one will we dropped out like 45 will be the new zero Wait one for 25 is actually just 24 numbers. Okay, I will Okay Because yeah, and then the then the preamble is 25 numbers. So One through 25 that's 25 numbers, right? Am I wrong? Oh Geez like now I'm I'm like, do I know how to count? Oh in a random order Okay, right But okay So that is okay But uh Okay, okay. Yeah, okay. Here's this 20. Okay. Thanks to me. Yeah. Yeah And the banana lake too I see I thought I was like in order and then one would have dropped out, but it's 20 that dropped out So, okay, that makes sense. All right. So think if I would not be valid 64 66 would not be valid as they are the result of 19 plus 45 and 21 plus 45 Respectively So what I want to do is I want to pre compute a map of the numbers And then so it'll be like a reference count. So I will have I will have like a map and it will count The amount of numbers that sum to it In the map, okay, and then when I When I and then I keep track of like the I keep I have to keep the input around, right? And then I will Take 20 Can I just map Minus 20 over. No, I can't do that Okay, let's see. Let's see because I I kind of I feel like Oh, right Right, right, right We're looking for a number that sums up to it, right? So 45 um So we essentially take 45 we check all the numbers in the map for all the numbers in the list And we just say So we we keep a set of the numbers in the map And we keep uh, and we keep the Yeah, we just keep the set of the numbers in the map and then we map 46 Minus all the elements in the set And then we check whether any of those is an element in the set Okay. Yeah, that's the trick. That's the same That's the same trick we did for that 2020 pair thing That's okay All right, let's let's let's just work it So now we're gonna look at this here as a byte string. Let me let's just write a parser from scratch Import This is gonna be module main where we're gonna import a text dot parset Got a byte string Not are we gonna do that though? Aren't we just going to Okay, we don't need it right get input is going to be This is gonna be file path to a set of ints Get input is going to be We're gonna import data dot set set import qualified data dot set as set We're gonna do uh set dot from list Over read at int We need type applications here Language type applications Uh, we're ready to set from list Read at it lines Dot get Uh read file file set int fmap map read ints Hi Marifin abit I'm saying that correct I don't Yeah, it's always nice to have new viewers I hope you all are enjoying this. I I'm having a lot of fun doing the programming and uh, it's nice to have company You know, I feel like all of you are here with me Um And you know just just hacking it's like a hackathon Just programming. We've got some buddies It's nice stuff So We have pp set From before I I'm just going to I should create like a utils thing for stuff I use again and again But I am I want to also keep these self contained So I'm just not gonna do that I'm not gonna be Oops Actually, that's good. So yesterday I was trying to optimize it and I had int sets And that's just a set that contains ints I think I'll actually use that instead Uh Because it's just gonna be faster Because I think set uses or int set can use the fact that ints are hashable and stuff like that It's gonna be faster In set, uh But we're not we're not going for so much for speed here, right? Okay, uh main IO Main is get input test input into putsterln.print.pp set Hey Oh, there's like this, right? Uh ghc Day nine I just oh day nine and we are going to time it see what happens Yeah, four milliseconds just reading the set. This is why we This is why we have a hard time competing against the Competing against the Rust people takes time to Do these things right? See like I was what I was talking about earlier, you know Because we have we have like a big startup time, right? So if I just do here if I just If you two milliseconds just or one millisecond like one to two milliseconds just Just printing something out So we have to pay that cost every time so This ain't too bad, but I'm not gonna I'm not gonna worry about it too much I tried writing like adult parsec last yesterday. I did text.parsec parsec is fine It's not included but like yeah for like here. We don't actually need a big uh a big A fast parser, right? It's just so such a simple stuff And like the parsing of integers is actually quite it's a bit tricky You need to like you need to like take each digit and then you need to like fold over it to create the number It's a kind of a core function, but uh You just I just want a function that goes from byte string to int directly, you know But that I don't think that exists Uh, when are we getting them emojis? Good point in the super I tried adding a emoji yesterday But it kept complaining that It was incorrect For no reason. I just said it failed try again later. So We're gonna get them one of these days in the super also In the super's real name I shall not reveal here. It is not mine to reveal Okay, let's see We got the set Oh wait, I can't map over sets, right? Can I map over an inset? That's like the thing with sets. You can't map over them easily because that changes the I can map I can map functions that would not change the ordering I should be able to do that Okay, we got some map functions here map map monotonic, right see map Would have we'll have to update everything again again again, but map monotonic will not because It doesn't change the ordering of the function. So we say here, okay We need we need to keep the order. So let's not make this into a set We need to keep the order here So, okay Okay, so, um Let's just write it solution There's no less defense What are what is a question? What are we supposed to be doing? Okay, this is 21 27 the first step of attacking Oh, yeah, thanks Hintazupa. We did it in eight days I think it's like people like adamant of code people like there's not so many people streaming house come also like card out on each All right First number in the list, which is not a sum of the of two of the 25 numbers before it What is the first number that does not have this property? Okay, so Let's see here So, okay, so So for the solution we're going to take in and we're going to return an int, right? solution a input Because okay, so where preamble that is going to be take 25 from the input So the first 25 numbers in the input Uh Curse set is That is going to be set dot from list Okay, uh, so this is take 25 of the input and then So Is it like uh, here's a larger example is only because there are previous five numbers. Okay, so preamble length is a variable So So you hit Pre-lane, let's call it that so you're going to take pre-lane here We're going to actually we're just going to say Pre-arrest is going to be split at So what does split at do It uh, it splits at the element so split at one One two three that's going to split it into the element at so it's going to split out so the the first So it was split it so that the previous the first length list contains 25 contains the int elements, right? So that works and Let's see. So we split at Here now Solution prime, okay, so it's going to be so that is going to be taking the input. Okay Solution prime is going to take in the The input now, okay, we already have the input now It's going to take in the input that's going to be like the ones the list of elements drop from the set And It is going to take in the current set It's going to take in the rest And then it's going to take in it's going to return an int right solution So here we we set we send in the input. That's the list of things to drop We send in the the Valid This is called a preamble We send in the preamble and we send in the rest. That's the first ones. We're going to be checking And let's see here The solution, okay, so So we're just gonna we're going to assume that we have the uh two drop To drop and then the Two crops and then I'm gonna say preamble I'm gonna say to check And then to checks So we're gonna assume we have this now solution If we run out of things to drop that's You know, we're just gonna go we're just gonna say error. Okay, so we So anything else like if you don't have anything to check Then we're just gonna error out error So nothing to check and or drop That's fine. We're allowed to We're allowed to say that so solution here. Okay, so we're gonna say equals first of all So we have the preamble. That's the first five elements in the set So we are going to say set dot map monotonic Okay, where a check set equals set dot map monotonic of uh two Check minus On the preamble Map monotic monotonic Say undefined here What's wrong with this? Data.inset does not export map monotonic. That's not good Okay, I think we're we're we're going too far with this inset thing. Let's just Let's just import set I mean, it's not gonna be that fast. I mean it's going to be fast enough anyway. We don't actually We don't need to deal with this Okay, so we're going to check set and then we're going to say if So if if to check Uh if to check is a um Wait, so I mapped the so I want to find if there is a number Good to me. Did you do it in rust? Like what language did you do? Oh nice. Is that including startup time? Like is that like command line runtime or is it? Because like like I showed you like it takes me A second Millisecond and a half just to start ghc like just run print hello world or put string l in hello world. So You must have a faster computer If you can start it and run the program in Shorter time then it takes me to start Okay, let's see Uh, so okay, so as it could be one plus 25 the entire program Uh, you're like but like just the part that does the solution then right or like also the parsing and everything else So, okay, we want to check Are there two numbers in the preamble or x plus y? Such that Said right so then we have to check Is there such a number? x let me see so said minus So we're checking if said minus y That's what we've computed. We have a set of said minus y's and now we want to see if I think we want to see if the So the sets of so this yeah, so the intersection of this set Is empty? So we have the preamble Then we map the monotonic preamble. So if set dot empty Is empty is it what I think it's null, right? I think if we check with I think we say null to check if it's uh Okay, so we want to see okay. This is is subset of Okay, check whether the two just that's our disjoint that will be So if the preamble and Is this joint? on the check set right Then then there's so if if the these two sets are disjoint then there is no element in this set Which is also in this set And if they are disjoint then there is no such x such that said minus y is in the thing And then we return to check else We call solution prime Hey, just some one in water Nice to see you Okay, timmy, please clarify if you when you wrote time day nine That said Oh 800 milliseconds. Okay That makes a lot more sense. I finally got it Uh That will be a tough one to beat I think two drops Preamble prime Two checks Uh So it was up for like the part two also timmy Chat dead Timmy, which is just talking what do you mean just some in water Rift chat can I get an f in chat? Oh my god. Nice Uh We want to Delete so what we will want to what we want to do is that we want to delete We want to we want to delete We want to say a Yeah, we want to say two no two drop set dot delete Uh on the preamble And this we want to set dot insert the to check A Wait insert is the other way around. Oh, yeah, so we're gonna go To set that insert like this. I think they should work Uh, let's see. So for the test input It should be solution five Compile it and run it We get 62 out Here you go chat have at it Hinta Oh Yeah If you if you want to subscribe That would be I've never had a subscriber before if you have like prime gaming you can subscribe That'd be funny. What language are you writing it in this is Haskell? Functional programming language. Um So everything is an expression which is nice Wait, let me check It said 62 The only number who does not follow this was 127 Okay, that's not good. So it immediately said 62. So we took the first Of okay, that's not good Let's see a Let's see what happens here Okay, so let's see This is gonna be trace show id And this is gonna be trace show id We're gonna import debock dot trace here Uh, yeah, but then I'm constructing all the pairs. I don't want to construct the pairs to me that is n squared we can do this in n log n right Even n because we can do hashing assets Am I wrong? so pre preamble I mean, yeah I mean if you do it that way n squared to me that's gonna take 800 milliseconds, right? Ain't nobody got time for that But yeah, I let's see if uh, I mean, maybe this just doesn't work That would be Let me work out p plus pp set Let's let's create like a like a g function here, which is going to soon be takes takes in a string and a set int and returns search print t T is going to be a t id s equals trace id plus pp set s s So let's trace all of these here t p preamble every t see t Computers, I mean I know to me but we We're trying to do this You know because because the things that like this will get more and more complex as we go on and then we need We need to make sure that it's as fast as possible, right? We want to do it. We want to keep keep us honest So the preamble is 15 20 25 35 47 see Okay, what is the Trace show id toad to check Printing side effects as that of IO What is this black magic? It's called Debug dot trace I think it literally just does unsafe perform IO Print or put string ellen and then returns the element It's good for debugging, but uh We you sometimes get things in a weird order So let's see here we have So Is it the So so so I have the preamble is 15 20 25 35 47 Uh Do I have a job? Yeah, I'm a phd student. So they pay me Pay me money to think about stuff like this And teach and I have to publish papers about stuff like this Uh, so yeah, this kind of is my job but Yeah, but I'm also You know the thing is we just have to think about super theoretical things And we just don't do that much like actual sit down and program dot stuff So that's why I'm also doing these things, you know kind of keep my skills fresh So here we have p right and we were trying to say 40 Okay, yeah 40 minus 15 25 40 minus 20 20 15 yeah five and minus seven Thing is that this is not monotonic Uh On the clock pierce It's 642 Okay, I'm not on the clock right now It's my free time Give it up for pierce He knows what I do Because he does the same thing Do I think it's necessary to have a university degree to work as a programmer? Uh No But there is a difference between working as a programmer and working as a computer scientist Um And it's kind of I I want to say it's a bit so the thing is One part of programming is the architect the architecture right you're writing the entire system up Uh, you know you you you you you're designing the system and then you have to know your computer science because You want you don't want to do n squared algorithms, right? You want to do pure Good fast algorithms, and if you know a lot of computer science Especially like, you know, if you have a bachelor's or a master's that's what you learn like here are the tools We have designed so far And here's how to use them Um And so you become better at architecting the system Right now another part of the system is just actually just writing all the code, you know, so If you know what you're supposed to be doing You don't so, you know writing the code itself so the actual program you don't need to have a university degree but but um You know, it does give you a lot of tools to work with Um, but you can you can learn you can learn those yourself Uh You can just pick them up as you go so, you know I've been doing this stuff for eight years If you'd been writing code for eight years you'd probably be better than me But I don't know Yeah, that is true Uh That is very true. So how many languages do I know you want to like know actual human languages or actual programming languages Because I know a lot of languages, but like those are like, what is knowing a language like I can do stuff in C I can do stuff in Java I could I can do so I am very comfortable in python. I'm very comfortable in haskell You know comfortable in javascript Um, but then there's like so many other languages like math lab and stuff, you know that like yeah I can I can make things happen, but I I don't want to use them, right? So but you know, I'm a programming language researcher, right? So my job is to create new programming languages Fiddler around with it and see what happens, right? So and you know, do I know those languages? I mean, are they even languages if I made them up? That's kind of cool, right? But uh human languages. Well, I know I know English hopefully I hope you agree. I know uh Icelandic Uh, so I I want to say so I could claim that I know Danish Swedish and Norwegian But I want to say I just know Scandinavian Um And then I know some German So I want to say five languages that I could claim that I'm okay with Icelandic English Scandinavian So Norwegian and Swedish. Yeah, not so Danish. I know but I wouldn't say I I'm good with it. And then and then German Spechanebischen Deutsch Aber ich glaube, dass meine deutsche Ansäure Nicht verstanden was sagen für Ich habe Deutsch nur Nur vier jahren gelernt doch Das ist ganz gut. Ja Anyway, I I know enough German to I lived in Berlin for like three months and it had worked out Yeah, I know Icelandic is a it's his own thing um nobody knows I mean Icelandic is a very is a very specific language And it's like it's one of the Nordic languages, but uh but The thing is okay. We can't use map on a tonic. Why because our Our function is not there Because we're we're doing the we're negating so it becomes exactly Non-monotonic correct and it's in the wrong direction No finish Now I know how to pronounce this banana and he let you Just added himself as a fin Uh, no, I don't know finish But I really like the fins though. I was at I did I did a stint at stanford It was a stanford summer program thing And there was a bunch of fins there and they're the only ones who could say my name The cutest thing though is like a fin speaking swedish Such a such a nice way They sound so cute Because they're like they just have like a complete finnish accent and then they they're still speaking swedish. That was That was nice Okay, uh, this specifically is not a monotonic function So we have to pay the cost here That's okay So now we got 127 see This is what happens If you lie about monotonicity I'm wondering can we do this? And keep it monotonic Because I would want you know, I want a map monotonic where Convert the set to a set of To an ascending list of elements Because I kind of I want this to be o of n and now it is almost o of n Okay, we have to construct the set anyway, right? So from list so build a set from an ascending list in a linear time. So this is log n If the elements are ordered a linear time implementation with performance equal to from distinct asc list so what I want to do Is a map anatonic So the anatonic I think so anatonic with monotonic tonic. So it said something here It had like take drop drop Antitone antitonic so map Antitonic Of a set F over a set so this is going to be a function which is strictly decreasing Uh map antitonic is going to take a function from a to b And a set of a's and I'm going to return a set of beans Map antitonic upset So that we're gonna we're gonna say here We're gonna say to ask list set So we're gonna like make the ascending list That starts with that starts with zero one two three, right? So this is like and it says subject to map fusing, right? So we say map f Set to ask list And then this is going to be a strictly decreasing function, so we say set dot from desk list Can I just do this? It won't it doesn't like it Okay, and we need eqb eqa Thank you. I don't even think I need the eqa. We're not using any function. Okay map antitonic Antionic Monotone Antitonic this is gonna be anti Tonic And it doesn't want to do that Because it's not in set oops Okay, so works see and I were doing we're essentially doing map monotonic But we are flipping it around because so so we have we have an ascending list and we apply a strictly decreasing function It will literally flip the order it will exactly flip the order, right? anyway I think that's cool map antitonic Should be in there, right? That's what I would like one of our utils Gotta go fast. Yeah, so now we have So, you know, this is gonna be o of n this is gonna be o of log n And it's gonna be it's gonna be fast And to me Our good friend said 800 milliseconds Okay, but like yeah, we did this and then we're doing set disjoint, which is gonna be that's gonna be a pain, right? So Let's check it preamble check set preamble to drop preamble prime We don't need the To here Uh, let's Get our input We're using the old Let's just copy paste Oh, no, I already wrote it. Let's just copy paste Wow, these are gonna be big let's see Uh, but okay, but it's gonna be 25 So go test so input Into the print Anybody five milliseconds beat that to me Let's see if it's correct though before We start gloating See That's why you learn computer science You save 795 milliseconds Imagine what you can do with that time I can't follow multiple times Anyway, we do set disjoints checks Because they are log n and not n log n not n squared And that's why it's also like, you know, look at the input. It's a thousand elements So we've checked Thousand times eight eight thousand elements Wow. Wow the n squared solution has to check a million elements You know the difference between eight thousand dollars and a million dollars That's a lot of dollars Anyway, we did part one. Let's say Yeah, and it's like a non-total function. So it works now Maybe we have to do it n squared anyway for For the second time I think also, you know, if you're gonna be doing a live stream of programming That's when you should have a computer science degree, you know You won't be caught unawares by any You'll be caught unawares by any algorithmic complexity, right? Because you know Eighteen milliseconds in python Python is also fast, especially like did you put it into sets in python that you do use the python set stuff? ban banani le tu Because like that's what I usually do in python I use all the built-in collection stuff because of that is written in like high performance C And because it's written in high performance C it goes fast and you just glue it together with python And that is like that is the best way to program. That's how ccp does it The company behind eve online. They just have C plus plus I think And then a bunch of python To glue it together and it's fast enough for a video game okay, so You must find the continuous set for at least two numbers in your list which sum to the invalid number from set one Okay Python has glue. Yeah, that's how you do it. I mean that's python is a scripting language, right? So you should use it to write scripts like glue stuff together Also, if you're joining in we just finished part one we did it in five milliseconds Of which like three are just starting the thing because uh Uh gc, you know, it's about your runtime to it initialize, but it's fast After we get started And you know if you're writing a server You're not gonna be killing the server starting the server killing the server starting the server you You warm it up even like the containers that you can run on amazon lambda like they are warm. So When you get in it just it just fires off if you're joining a Please feel free to follow the stream because the more followers I have um Which like gives me a bunch of stuff that I can do I can do like I can do stuff if I have followers, which is which is nice But I don't know I don't know how to do this I started streaming properly December 1st nine days ago and uh, so I'm still a noob. I'm not like junta super. He's a pro streamer for a long time. He knows how to do emojis And that's not that's not trivial. Okay. So here we've let's say input is getting the input and So test input Getting the input and then we are just going to say Let's all one equals the solution of five on the test input now input is going to be to get input of input and then Let's say let's all solve one is this Sol two is solution of 25 over the input And then we're going to print print sol one sol print sol two So what i'm doing here is Uh Yeah, so what I did is that I I bought a bunch of stuff Not for this stream Right because I do like that's part of my job as a phd student is I have to write articles I have to make research happen As pierce Go tell you all about that guy knows how to do research Uh, you have to publish these kinds of papers and then You have to keep a talk About those papers, right? So here's my setup at work Pierce I'm trying I'm trying to wrap you here. Okay That guy knows how to he does the papers all the time. Okay So, yeah, this is why I have this setup Because I I do I do talks Um And I think I think actually pierce He showed off his like super cool setup and like for On twitter, he had like a nice microphone and a weird stand and whatever showed his super cool setup and I was like, damn I gotta get in on that game So I had like a bunch of expensive lights also. I have money issues in the sense that I Spend too much money. So I got a bunch of nice lights. I got a nice spring light I watched Alejandro Serrano's talk at ICP. He had a nice green screen thing going on. So I got a green screen And I have a nice Canon camera Which I'm using as a webcam Yeah, find it find it pierce link it in chat. Are you even allowed to link and then I'll go to it. I'll show it on stream live Yeah, so that's why I have all this stuff And also, you know, I haven't been able to travel so much 2020 So being able to communicate Uh with my family and friends on zoom in like high quality with good audio It's just been very helpful for my sanity, you know And you know when grandma sees you on screen and you look like you're on tv You know, you have very good quality and she's like wow nice That brings me a lot of joy Now you can see me in hd. Yeah and uh Yeah, that's why I have this nice camera um, but then before that I had some I was playing around with uh getting I was you know, I was trying to like build my way up to the setup I just decided in the end. I'll just get a proper camera And then I just got into photography. Well, um, but then I I bought this small camera here I'm not gonna show it to you because we'll screw up the entire setup Already screwed it up a little bit but Yeah, so I haven't like an extra camera lying around to the keyboard cam and I like the keyboard cam Because what is a programmer without his keyboard? You got to show off your keyboard also I keep wrapping peers peers and I we've met at least like maybe twice the least ones but we are Twitter buddies he's also into giving good talks and Having nice keywords, but he has fancier key switches than I do. He has like top or stuff Yeah, I think we met at ice. No. Yeah in Berlin, right? I was like you were hanging out in the couch like Where the bar was but like not at the bar and Yeah, you were hanging that there and like it was you it was uh, William Bowman Also a legend was Paulette Also She was a Twitter legend like she she had a thousand followers that day in Berlin That's cool. And uh, and then now since then we've just been Twitter buddies And it's funny, you know because I I only talked to Pierce on Twitter also on this peel talk thing though Yeah, also Paulette like she's the one who gets us into this stuff, right? And Bowman like they those those two know Twitter. They are so good on Twitter Uh, but uh, yeah, so I met Pierce once but he's my Twitter buddy Is that a parasocial relationship? I don't know I don't know what it means Yeah, I also did my own company for a while And it's like going on the side, but You know if you do a phd It's it can be hard work But they also pay you for like three to five years to just think about programming languages And you're just uh, yeah, oh famous recordings. Are you guys are you are you all ready for this? Oh shit, I didn't even like it Oh, no, this is my Icelandic account. I have an alt I have two accounts. I'm one in Icelandic. Why because If I tweet to everyone in Icelandic all the time people don't like it. Wow. I didn't even like this. Wow. Okay. Anyway, here's his crazy recording studio And I think I copied this initially like I got this mic But then it turns out it's a like I don't like condenser mic. They pick up way too much And I stole the camera setup and now I am the master as Darth Vader famously said X codify Yeah You know one of these days I'm gonna get guests on the stream. I'm gonna have pierce come over and we're gonna do stuff together It's gonna be good Let's see Hello guys, I'm learning a data science and analysis on python I totally want come on pierce. You got a nice camera. You got a nice microphone. You can totally be on stream Okay, I'm tolling you in this field and If you want to upgrade my skills, you might say with this hot new top my first project is to build data paper on a spot of a baby clan Okay, he's just linking his stream Go check this stream up. I mean I I really rep I like people programming on twitter because I I want people to know how it is See you around kriaks. That was nice to have you around All right, let's let's go work on problem number two Also, check out this talk. It's good. It's good stuff Okay, uh, yeah, but yeah programming on twitter. That's it's a pro like it There's always there's a couple of streams and they always have like 10 viewers Like the ones I have the most are like programmers who are like gamers and they're like doing a pre-stream Um, yeah, I mean it's a no-go, but I'm a noob. So I enjoy the talking Okay Now, but let's uh, let's keep going at this actually We actually got 62 earlier once we when we did the thing wrong, right? And that's just uh That was just the look I think okay, uh Final step in breaking the Xmas encryption relies on the invalid number you just found you must find a contiguous set of at least two numbers in your list Which some to the invalid number from step one This is gonna be It's gonna be a bit rough Yeah, kriaks, you know, I it's it's probably a no-go, but I I also respect the hustle I think that's the thing, you know if you want to if you want to stream Because like twitch does nothing to help you grow. I think you just have to hustle. I respect the hustle Let's see Now we have to find a contiguous set This is the 2020. This is day nine of the Haskell of the advertico 2020 We're doing problem two. We did problem one in A millisecond or so. I think compute time just a problem Uh Which was better than to me, which is like the only Only thing yeah day seven was hard day seven was very hard day eight Was hard Uh, but don't despair it gets a bit easier again day nine. I think this is easier than the ones we've done before I'm just I'm just talking way more. That's also the problem, right? I got I've I've got more people on the stream now I like talking to them so it takes me way longer than it did initially When nobody was talking okay, uh So find a contiguous set of at least two numbers in your list with some to the invalid number from step one I think we can reuse the code Uh So we're gonna have like a running sum And then we will we will We will like add to it if we have If it's too much and then drop from it if it's too little No drop from it if it's too little and add to it if it's Drop from it if it's too much add from it if it's too little Let's see. Okay. Uh, so what the one we're looking for Is going to be the number so solution two It's gonna take in the so we don't need the preamble, right? We just like to find the contiguous set of the least two numbers So let's start it off with The solution so the the what we're looking for that's gonna be the first one looking for Then we're just gonna take in the List events And No, we're gonna do it like this. So this is gonna be looking for uh I'm mixing up things looking for we're gonna take in the current sum And we're gonna take in a list of hints and we are going to return so Okay, so it's gonna be the list of these are gonna be the Numbers we have currently This is gonna be the list numbers we're looking at And then we're gonna return We're gonna return the numbers. We're looking at we're gonna return our numbers. We end up with so we're looking For we're gonna have Curse some we're gonna have curls So this is gonna be like we're gonna we're gonna be looking at the the top one because we might want to drop it Uh, so Yes, this is gonna be cur L's And then we're gonna have incoming Okay, uh, and we're gonna be looking at the head of incoming. So it's gonna be i Incoming now We're gonna say okay if current sum Is the one that we're looking for Then we return the current else else What do you want to do? Else so if we were not we don't have the current sum Then we want to check if cur some Larger Then looking for then We want to see we want to do do we want to do solution It's okay codify I was new Eight days ago And look where I am now. I don't know even where I am. Am I somewhere? Solarizing to my head. So if We're looking for so if the current sum is Is more than we're looking for We are going to remove So the new new current sum is going to be current sum minus c We're going to remove the first element of the elements we have And the cur ink the current And the the incoming will remain the same So and then we have a we'll have c s here and then we have the Let's have this these just ink And this is going to be else So if we have this and okay, so otherwise a looking for some cur sum minus c And then we so we remove the top element and We we we just we remove it from the sum and then we keep going And then we don't change the incoming otherwise If uh, if it's not equal and it's not bigger, then it's going to be smaller And we're going to say solution two looking for Cur some plus I ink plus I And then else oh Yeah, and then I need to I need to add it to the back of else, right? What is at doing in that context? The at is so here we so this is a pattern match so c to c s But I want to be able to refer to the whole thing So so instead of having to say a so I give a I give a name to this pattern match So else is going to be the same as So else is going to be ccs So I just give the name to the pattern match that I'm looking for It's actually more useful if you're dealing with records and stuff But yeah, you're just saying else at So you're giving a name to this pattern here Okay Here I want to I want to I want to add it to the end right So I would I know this works It is not good So what do we do? We use a clever data structure Known as did it out sequence Which uses something crazy called finger trees To give you lists where you can add to the back in the front as much as you want without Without even wondering about it import data dot sequence import qualified data dot sequence as Okay, and then this here the current L. So that's the only data structure we have to add to the end that It's going to be a seek of hint And this is we're going to return a seek of hint. So that's going to be correct Now I have to change this to be the this operator seek Dot I think it's this Let me let me see because there's like a you you can pattern match on these sequences data dot sequence Do okay, yeah Oh, there's a Funny little guy here Uh Like this so it's just essentially saying it's just the same as the colon except it's in a sequence And what we can do here is we can say seek I think right Yeah, it's like this Raffiano if we follow that logic You would have spent 800 milliseconds solving the first problem We got to do it a proper algorithmic way, you know Why doesn't this want to import? Okay, but that already worked right But it's this one that doesn't work Oh, it does work. Okay Let's see If this works, so then we add the elements wed into the end of elements, right? So this is going to be a super efficient sequence I mean data dot sequences in the standard library, right? We have to use the standard library, right? Gonna be good um Let's say print Solution two and what are we looking for? Okay, so actually This will be solution to prime And we're gonna say solution to it's going to be you know the end we're looking for and The incoming things and it's gonna return as a seek int And solution to It's going to be So we just what we're essentially doing is that you know, we want to give a kind of a name to the initializer here where So it's going to be solution to prime Of uh, so the looking for and So we're going to have two elements here a b and then rest a So the current sum will be a plus b We need something here and then we're going to feed it the rest And this is wrong Solution two is applied to too few arguments a So it takes in the okay. We have to pass it the looking for as well We don't actually have to because we're defining it like It's going to be in scope here. So we could just use this looking for but Okay, and how do we create a sequence of two elements? We're just gonna from list it. This is gonna go so fast I need to feed it stuff. Yeah salt to area for this That was fast If I do say so myself We looked at the entire data set and we did all this summing back and forth And it took us This little time Now we have to check We had to get the smallest and the largest number So, okay Cont set Are we just gonna believe that this just worked? Cont set is equal to Print cons set So four five seven four four zero three seven two three five seven nine seven four five seven four four zero three I mean, this is four eight one four six two three. Yeah, and seven two five three seven nine seven I mean, this is a continuous set Uh, let me see and then we do It's what I like about Haskell. You know, we did we did all this cool stuff It, you know, this is not like a line, right? So it We did all this cool stuff In seven lines of code, right? We even we even did It's good stuff. I like it Um So I do computer science. Well, you save time save hundreds of milliseconds Uh, let's see here The set is definitely continuous Uh, let's see data that sequence. There's like some two list function, right? Can we is there like is we can we just say Print concept. Okay, so Minimum of concept We're we're just gonna say Res equals minimum of concept plus the maximum Conset print res See if it's correct. I think it should be What is the sum here? Because the sum had to be the number, right? We even Yeah, I mean the sum is correct and it's continuous. So this is correct Damn we did day nine And I I like these solutions. They're very algebraic And they are extremely fast And I they're not n squared. I think they are all an n log n in the in the in the Highest, you know This is gonna be this is yeah I like it. I like these ones So here I'm sorry Timmy What are you doing? Are you like finding all Possible continuous sets and checking the sum of all of them anyway I'm pretty happy with this result It all worked out for us in the end Uh We did it quite fast This is like oh of n log n And mostly because we I think this set dot this joint operator is It is expensive Uh Where m are the two sets, but this m here will be 25, right? So it's not gonna be such a big Big issue, right? Oh, this is m times log m Which is bounded by n and we have to iterate over all of them. So it's gonna be n log n So for all of them we do log n log log log a I like this map antitonic thing because then we We can just we can map over stuff in a We can map over it in like o of n time Even if it's not a monotonic function It's a it's an exact opposite of a monotonic function And then this here Like they say uh look No, wait Okay subject to list fusion. So like I think this this here. I'm not even sure it will like Create these lists. They will just fuse jc does magic like that and um Set out this joint that's gonna cast us m log n This is going to be n log n 25 but the it's a constant. So it's gonna be this is gonna be 25 so initial one 25 a And then we have to iterate over all the elements. So this is gonna be o of n And for all of them we are going to do Some set dot inserts to leads that's n log n. These don't matter So this okay, these are gonna be log n So we have to do that n times. That's gonna be n log n Um, no, this is gonna be log 25 so it's a constant This is where it's not changing. It's not independent. So this is gonna be o of n And the same thing with this disjoint thing Is that these are going to be These are going to be m log n And then this is going to be this is going to be It's the constant also 25 Like not a constant like it's going to be the o of c So I think this all of this here is going to be the Um o of n Because these both of these sets are not dependent on n, right? They're dependent on the constant So this is an o of n solution Which is quite a lot better than n squared And this is also an o of n solution We're using the magic finger trees. They are crazy good It's actually some new research on finger trees They've made them slightly faster But i'm not gonna show you that paper because Okay, I don't like that That's okay. You don't have to like everyone, right? Um But uh Yeah, so it's basically It's a bit like a list except you can put stuff at the left and the right typically slower Then let's move on. Yeah, but yeah, but they are they are quite good on like average Anyway, hey Yeah, it's gonna be a I mean, I think I'm not gonna say short one I want to say this is like the average length one Um, like yesterday was four hours. That was so long And uh, you know, I I'd appreciate a follow if you If you haven't yet but then I gotta pump those numbers up, right? Um, and if you have like a free subscribe, that would be cool Like one of those cool Uh You like the amazon prime subscriptions like if you're subscribed to amazon prime, you can like subscribe for free Anyway, I just want subscribers, you know, I have zero subscribers right now Uh, but I don't think I don't think people subscribe that much. I also think like I'm not running ads or anything. So I don't know But you know, it's I'm not here for the money. I'm here for the Company while I program and you guys are Quite good at it, right? Wow I got Wow Okay, I can press something and say send thanks Damn It's my first subscriber. Thanks. I like shine You're gonna have something like forever. Uh, if you keep subscribing You are gonna have like You're gonna be like a founder of the channel, right? So Yeah, I will remember you When I am the biggest programming streamer with dozens of viewers Program streaming isn't that It's not that big of a genre actually What you're playing to do after aOC that is a good question. I've been asking myself that So one idea because I like I quite like these problems. I might just do old aOCs Uh like from 2019 from 2018 Um Yeah, that is true like So, yeah, so that's that's what I might do I just might do more advent of code because I haven't I didn't do the previous I did do some of it previous usually but I never finished an entire year. So I might do that um And then you know, I'll also I think I'll also like to stream me hacking on research stuff I think that's gonna be interesting right because then we don't have an oracle. We don't know if it's correct And yeah, big channels, there's way too much chat right and that's why you have this subscriber only chat Because like if you have a thousand viewers And like 20 of them are subscribers and that make chat makes chat okay again But you know the big one's like poke main. It's just you know The chat is just going All the time and there's no way Can keep up with it Anyway, thanks for tuning in I hope you like these Solutions I'm quite proud of them actually I think I think it's okay to be proud of these solutions. They are Functional And they are they are fast What more do you want get come at am a Day nine day nine is also Used to follow day nine. That was like a good streamer by the back in the day Oh, like another streamer, but he liked these episodes on youtube That were a lot of fun Uh Okay. Yeah, that's cool. Are you using the regular expressions for parsing? I mean, I guess you are right. There's no There's no problem so far. Let's use the regular expressions unless You're doing some crazy stuff with But uh, yeah, so I'm I I limit myself to I've limited myself so far to the base library On uh on stream I I did use some Like base and like stuff included in base, you know, like data dot set Data sequence. This is all like included with the base ghc I tried to optimize yesterday's I tried to get as much. Uh, oh, yeah, that's true I just used a bunch of string splitting And it was it worked But I that's like after that I was like, hmm. I should start writing a parser And that's what I did like yesterday, uh, not on stream but after the stream It was like four hours yesterday. It's too much But I still went back because I love this stuff And I used the parsec parser Because I wanted to see if I could make it faster And then I used something called the atto parsec parser, which is even faster than parsec And I was like, why am I taking two milliseconds to just load the thing Where the rust people are doing, you know, 150 microseconds and then a And then it turned out that, you know, ghc just takes two milliseconds or like a millisecond and a half to start And if that's, you know, so I already have 1500 microseconds there And I can't go below that so I was like, no, I'll just I'll just keep doing it in a algebraic way and then We can we can we can do microseconds later But you know this solution here So nice, right And like there's no kind of what is he doing here? Like what is wait? What is this crazy operation? There's no category theory or anything. It's just like Check of the set is the joint You know, add it to the elements, keep it running, running Cali Uh, so I guess this would count as dynamic programming I think this is technically dynamic programming in the sense that, you know, instead of calculating the sums again and again We just have like this running sum so it's kind of memoized But uh, yeah, so this is dynamic programming And you want to do fast Fast programs for like hard things. It's usually dynamic programming and then like programming competition problems are usually dynamic programming And You just have to You just have to really think about like what am I going to be evaluating? What do I want? Like is there a way to keep the running tally? That's usually what you're doing Giving you give the state along as an argument. Yeah. Yeah. Yeah. That's also what I'm doing here like a solution to This is this is tail recursive right It ends up calling a function at the end And then what it can do is it can reuse the closure. It's called Uh, and this is like a very classic pattern. Yeah, you you you define the initial thing And then you kind of launch A tail recursive version and this here gets Compiled okay, it doesn't get compiled into the loop, but it behaves like a loop Like a tight fast loop And that's That's fast. That's always going to be fast. That's how you do fast functional programming Essentially, you kind of write these tail recursive functions and the optimizer can optimize that so much because it doesn't have to create all these contexts And it becomes pretty fast, right? So And I think that's true, you know, I do these tail recursive, but like that's just how I write code, right? In functional like I'm so used to Tail recursive optimization that that's always like how I think about it. Like I'm like I have input I am just going to iterate over the input, right? And how do I do that? Well, I do a loop and how do I do a loop? Well, I do tail recursive stuff So it works. It works great. It's fast and So what what computer science knowledge did I use here? Well I think this was definitely computer science This monotonic map over a set because I know I kind of know how the set structure works. It's like actually like a tree structure it's a heap um So like if I use the hash set I could not have done this because then the sets would have the Hashes in the set would have changed I guess I could have just put back into the set but that didn't work. So I know what monotonic functions are And then I That's how I figured out. Okay, I can do I can do the same with map on antony that was computer science, I guess and a Doing it in a set based manner I don't know. So I did I have a bachelor's in math as well as a bachelor's in computer science. So I I tend to think about sets a lot or you know, I'm very comfortable with sets So just turn it into a set operation and also, you know, people know set operations, right? They're gonna be fast super fast especially like Set this joint because it's basically going to like take all of the elements in one set And like for each of them it's going to check if it's in the other set That's how I would do it. But this is probably an even faster way Doing it, right? And then you just have to find one element. That's not any other set and And you're gonna be that's gonna be very fast So I guess the set thing came from knowing computer science Also, like that's why I do it in python, right? I I push everything into these collections and these, you know, the dictionaries the sets Not list so much a little bit because all of those are written in C and then there's they're gonna be fast. So you If you do it in sets in python, you know, you don't have to do Set operations in python. You do it to set operations and C And that's gonna be fast. So that's kind of where I got this thing from All right I'm gonna Call it quits for today. I think we're pretty happy with Day nine at least it didn't take four hours. I would have been too much And uh, I'll be back tomorrow at six o'clock But then I'm gonna take friday and saturday off because I'll be traveling Uh So if you want to get a notification about that, please Follow you'll get a notification when I go live next time And if you're loving this content, do like do like my one subscriber Alekshine, I like Alekshine. He didn't follow me, but he did subscribe Or they I'm sorry. Hey Even shouldn't assume Uh, yeah, so thanks again and see you all tomorrow for day 10 Of the advent of code All right Yeah, that's true See rafayano has a founder badge next to his name You want that you should uh, you should join. All right. See you all uh tomorrow. Bye