 Alright Welcome to today's episode of Advert of code We're gonna be looking at day what 20 now Detain one even Let's see how we can do it Take a bit of a break For Yeah, you know, I've just been in Iceland. I've been able to go on nice Walks with a lots of buddies That's been really nice, you know, I've been stuck in Sweden for so long that you know, I was There was a bit hard, you know a bit hard too There's a bit hard. You know, I didn't meet so many people, but now I'm here and I've been able to meet so many people Let me know I changed the audio a bit in the sense that I moved the microphone over here Let me know if if you can hear it or not I Hope I mean it looks like you can hear it and I can hear in my ears, but you never know, huh? Okay, let's see How does today's Problem look like that's the question, right? Hope it's not too bad. I mean they've been getting harder and harder, but Are they gonna be super hard now date anyone? It's super hard Let's Let's see, let's see the leaderboard. Let's see I don't know. Let's see Let's see, let's look at stats. Let's see how many people have finished it Not many people finished it. I Mean by the fall of the curve, right people just kind of stop. Let's see Let's see if it's too hard for us. Okay, you reached a train. Okay. We got on a train Seems legit You reached the trains last stop and the closest you can get to your vacation island like any wet Aren't even any boats here, but nothing can stop you now you build a raft You just need a few days worth of food for your journey Okay, you don't speak the local language so you can't read any ingredients lists However, sometimes Allergens listening language you do understand you should be able to use this information to determine which ingredients contains which all engines And work out which foods are safe to take with you on your tip prep Okay, so you have a list of foods One food per line Each line includes that foods ingredients is followed by some all of the allergens The food contains Okay, I'll just turn it all this mark when they're listed as in contains not self is The ingredient that contains each listed allergen will be somewhere in the corresponding to the ingredient list However, even if Allergen isn't listed The ingredient that contains that allergen could still be present Maybe they forgot to label it maybe was limited language. You don't know. Okay. I see Hello, sir Chi Was that view ready for some Haskell it's gonna be good. All right, so okay, so, you know fun with the food. This is fish dairy soy fish Okay Let's see So here in this so the common denominator here between these two lists is dairy, right? So this has got to be dairy Okay, and Now this contains so as in this contains fish You have sqjhc in both of them, right? So that's That's gonna be fish and then this has to be soy, right? And if this is soy And this is their so we already kind of figured out right while the food contain it we feel it and it's definitely contain it Okay First step is to determine which ingredients can possibly contain any of the allergens in the food in your list Any about example, not if the ingredients kufsk Okay Noom-zib-zib-zib or church contain an allergen Counting the number of times any of these ingredients appear in any ingredients list, but it is a five They all appear once each except a zib-zib which appears twice Okay, so how Determine which ingredients you cannot possibly contain any of the allergens in your list How many times do any of these ingredients appear? Okay This is gonna be a set theory stuff, right? Okay, let's see. We are going to Open up Gc, let's open up a new day Right here new folder day 21 Yeah, we stopped at day 16 because on day 17. I got out of quarantine. So I've been busy You know how it is. Okay. So day 21 died HS module. Oh my god All right Okay safe A new file Searchy is the the audio. Okay, you think the audio is working good I moved the microphone. So I'm hoping it's so good But I haven't tried it Let's see What's this our test input? And the result. Oh great It's really hard for me to monitor it because like I could I could monitor it Wow Thanks, searchy. I have a nice microphone. It's nice and fluffy. You see Yeah, I went to uh I went to the uh We have like a zoo here except like it's only like farm animals And like foxes and stuff And I got a got to pet a baby cow Kind of like petting this microphone, you know I like I like I like animals We should name the mic, right? I would I would offer to have like a pole to name the mic But then it would just get named, you know mic mic mic face You know people on the internet they're not to be trusted with poles. Okay Okay, so now we have a list of words The order doesn't matter So let's just take this list right and then we split it Okay, we're gonna so Get Get input There's gonna be file path to io io string no io We're gonna have a list a set of strings, right? so Searchy, what's your favorite programming language? Are you a haskuller? Or are you in the dark side with the imperatives of qualified Did I not set a set this is gonna return us It's gonna return a list pairs of sets set string set string Now get input equals so we're gonna do uh to set uh to a to Inker list dart That is a cool language that's by like google right they they define a dart Or no, so google had go who had dart I remember it was like it was like the cool cool cool kids were using dart. Yeah, it was cool Yeah, that's cool I looked into it a while ago. I mean, it's not it's not not my favorite, but It's a good stuff, you know So you're like a you're like a mobile developer. Yeah Yeah, yeah makes sense Because like dart dart is like for android, right? That's like that's what they're supposed to be doing Where to Inker List this is gonna take a string and return a pair of set comma set String comma set string Yeah, so you can use can use dart on the back end as well I like do people do that Okay, so it's flutter it's flutter. That's a framework, right? That's not a that's not a Language, right? Let's let's split it split this right Span so we are going to take the words here Okay, so it's gonna be So where we yes, so we is gonna be word. So this is gonna be we're gonna get in a string here Okay, it's gonna be undefined. Okay, so v s is gonna be word Words of string. So what is wrong here? Um, yeah, we're gonna map Yeah, yeah nice But like yeah, but can you use can you use dart on the back end? There's like either like or like are people doing that full span a So we're gonna have here Yeah, we're gonna have span. So this is gonna be a Inkers and alerts That's gonna This is gonna be like this This is gonna be span equals Contains And then so the the ingredients are gonna be fine. So this is gonna be From set A set dot from it's gonna be from list fingers And then a two alerts of alerts Now what does two alerts do it's gonna be yes Let's actually just Merge these together because we never use the words otherwise You use you use dart for the front end and javascript for The back end it's kind of opposite, right? tend to like use react native and like just use javascript on boats Or like is that is that not cool anymore? So this is gonna be we're gonna be splitting Okay, here we're actually gonna be doing so we have the words alerts And you notice that it's either gonna end with a comma or it's gonna end with a Let's open the puzzle input. Let's let's see how they look So it's not gonna end with a comma or like a parenthesis opens, right? So comma comma. Okay. Yeah, sweet So we can literally just do This is gonna be contains like this we can literally just do map A drop well so we can just say So two alerts It's just it's just Um matter but drop last Where drop last is going to take in String and it's going to turn the the innits of the string, right? Oh, okay I've been using you know, I like react native um Well, I haven't used it too much actually Uh, but I like it because It allows me to kind of write an app Because I usually I usually so I usually program for iOS, right because I haven't an iOS device. So I program for iOS and What I can do Is that with the tail so there's a function that like That's like in it and it's yeah, it's gonna be good This is just in it. Uh, so it's gonna be in it So I use it So yeah, and then I can like write most of it in javascript, right? And I don't have to deal with uh iOS development Because it's just it's harder for me to test That's the problem, right? I just want to I just like opening up Uh, I just like opening up, you know chrome and just I'm just using it with so okay. What is wrong here? Now this two alerx that's gonna that's supposed to take in I think it doesn't know the types here two alerx can take in a Yeah, a list of strings and gonna return me a set of strings but Two alerx two two algers. All right alerx Alerx. Yeah, this is wrong also nice Now let's see here main IO main is Yeah, so have you tried a we can you can use kotlin there to like kotlin iOS no kotlin is like for java Yeah, that is that is actually also for android But yeah swift has a lot of cool stuff input Test input and this is going to go into print And We're gonna go to day 21 and we're gonna say G at C Oh three a day 21 and And we have to do is like this because we're not actually on linux. We're on We're on we're on power shell here like in windows measure command Day 21 dot exe And then we have to say out Default Okay, let's see if we did let's see if we managed to parse the test input Do do do Let's see. Uh, yeah, wait. No contain Oh No, wait, this isn't good so Uh Contained dairy fish. Oh, okay. How does the upload contains? Okay, we didn't do the span correctly Why does it say contain? Oh, yeah, I mapped let's see here now. Let's see Trace show ID We're gonna import debug dot trace here Let's run it again. Oh, yeah. Yeah Yeah, I like I like the codlin. I use it a bit I use it a bit actually So now it says contains Okay, and I say equal equal Contains and it's not it's not spanning. You know, do I have to say? Oh, yeah, this is supposed to be break That's like the exact opposite of span Oh, yeah, it's gonna work. I think it's gonna work now. Yeah. Yeah, so now we have The ingredients and we have the allergens Nice Okay, so How are we going to solve? So this is gonna be like a A constrained solver, right? So now I think we have to like flip flip the sets. So how did we know that? How do we know? Okay, so let's let's like build up the the keys Uh Now we're gonna say here possibilities It's gonna take a lot list the set of strings Set of string and it's gonna return, you know, map string Set of string Now possibilities. Okay, so if I'm empty, I'm just gonna return Map.empty now this is gonna be a bit slow, but I don't think it's gonna be that slow so You know, I'm not gonna write it in a tailor-created or gal. I'll just write it in a tailor-created way. It's gonna be fine Yeah, map as map. So this is possibilities. It's just gonna be It's gonna be possibilities prime of map.empty classic now Where possibilities prime it's gonna be prime so far is going to be and then So far empty is equal so far possibilities prime so far and then And then here we have, you know Ingers and allergs Uh Something more This is gonna be so here we're gonna for each of the allergs Okay, so we're gonna say fold l Uh some function and then the initial value is gonna be so far And we're gonna apply it to all the allergs Okay, and then we're gonna say here add to map Aware add to map It's gonna, you know, take an n so it's gonna take an m and an allerg And and it's going so yeah add to map. It's gonna take a it's gonna take a you know set of of string And and it's gonna take a Now it's gonna take a map map of string to To to set Set string Uh, yeah, they're blue. They're blue exactly. Uh, that's why they're so loud I have another one that has like the silent red ones the silent red switches And they are, you know, they're decent. They're quite decent actually but But the thing is uh, yeah, I use those when I have guests over and stuff that you know, I don't want to bother them too much With the like noise of the keyboard But I this is quite nice, right I like I like the tactile feel I really like the tactile feel So here you're gonna have the the map And then we're gonna have a string and we're gonna return a map of string set of string now add to map So this is gonna be allerg And then what we're gonna do is we're gonna say Modify like we want to Thank you Christian. Yes, Erce Ask ask something for me. I don't Grammar is not quite on point, but what do you want to ask? A Modify data dot math modify Yeah, I'm streaming a bit later. Where am I from? I am from Iceland. That's why Keyboard looks like an Icelandic flag, right? Good stuff. No Gil showing up. It's one of my Pop viewers. I think I think he's been in quite a lot of them, right? Modify, uh, wait, there's something called modify right where I can like I can like update. Yeah, update Okay, yeah update Exactly. So I take I take the yeah, so update up to the value of the k if xx an element is deleted The expression update with key update so update takes the yeah So we're gonna say modify. I'm gonna be modify I wanted this to be I want this to be string Map string string Yeah, Iceland is a bit cold, but you know not as cold as you'd think we have We have quite good Like a like a climate here like because of the Gulf Stream Nice Uh, so it's not it's not too bad actually Now let's see add to map. I think is it because I should be fold r-ing here Expected type maps out of string add to map and What is the type of fold? I always I always forget the map of fold l Okay, we're actually gonna say adjust here. I have to fold Yeah Howler exists too many else here Okay, now I'm folding car Yeah, but it's because I want to use adjust the immediately That would be nice, right? What is wrong here? Okay, this is gonna be the fold r is gonna be The folder is gonna be a bit slow, but you know, it's okay. It's gonna be okay Well, no, okay, let's let's fold l Let's not do it the bad way. We'll just flip This function We're gonna fold l add to map fun map, but just fun where so the fun is going to take in car set And what we're gonna do is that we are going to say That this is the result of this is gonna be Let's see set dot intersection Uh, it's gonna be set dot intersection With the ingredients Okay, so now let's see here print and then we say possibilities Let's see what happens No, this is vs code actually Because I'm I'm programming on windows like my usual driver is a Linux machine Hey, I know it but I'm trying to keep I try to keep things simple on this stream. So I don't go too much into lenses and stuff. It's uh It becomes a little much actually uh, oh Okay, yeah fold l add to map so far alergs so uh So far prime is going to be that right? So that's how I update it It's going to be possibilities prime of so far prime rest I didn't I didn't do anything Did oh adjust also, right? The thing with adjust is that it only adjusts the things in the All right, so the update The update updates the value x aka Okay, so I think I said I think I say alter But this should be Alter Now this fun of Just as Supposed to be the set everything you're using now. Okay fun of Nothing is going to be Ingers But this one this is going to be Just and then this is going to be Just I think I think I think this should be good Let's see. Let's see what happens Okay, so dairy can be this one, right? That was one we found out fish could be either of these two And soy could be either of these two And what does it say? In the above example not of the ingredients kfcds or Our trh can contain any other it can contain an allergen, right? So we figured out which one is dairy by doing the intersection We figured out which one can be fish But so you see So either of these two could be soy, right? And either and this one so fish could be Either of these two This could be either of these two Okay, so now Okay, now we found the possibilities Now we have to take the We have to like do the Do the solution, right? His fish is definitely going to be you know sq and then soy is going to be Fwjkl, right? Let me see Do we even need to do that? Let me just check here Okay, so what if we just do map.lm's A map and set.from list Let's see. Let's see what happens here map.lm's and then we do Can can't this is what's wrong with this Uh, so we've got the elements and then Let's just see what the map.lm's is That should be a list of sets of strings, right? And we take a union of all those sets And let's see what happens Okay, so solution It's gonna be it's gonna take you know the The the input leg which is a list of set of string comma set of string Don't turn it in right it's a solution What is the solution gonna do here a solution is gonna be a very or p So imp p equals possibilities of imp Okay, so and then we have Alerts is gonna be The set.unions Of the map.lm's of p Right, okay, and then we are going to say This is gonna be a set now And then we're gonna say okay, so take all the ingredients and filter out the ones in alergs, right? So We're gonna say a non al words is going to be set.intersection With Alergs And it's gonna be the first Of the input right And then we say a sum so this is gonna be Some of the the map set Set.size on non alergs I think let's see see what happens Oh, yeah, and then we have to run solution here Let's see what happens We get eight and now oh, this is be this should be difference Exactly not The ones were not alergs, right? Save it run again. Oh now we get four Let's see this one would be trace show id Okay, now trace show id On non alergs Okay FWJK Oh, right the exact opposite. We want to remove What's wrong here? I like this and it's gonna be alerts Where are you from Cersei? Nice this should work now. Let's Let's let's add the input here input See how if it's like super is it super slow? I don't think it'll be slow So we get the right answer for the test and I think we should be getting the right answer here, but Let's see I'll see if it runs super slowly also. Oh, no. Yeah, I forgot to Remove the traces here Let's see Two seven nine eight. Let's check if it's right. All right We got the first one In half an hour only it's been fast today. Let's continue to part two Always nice to get the first one, you know, at least your Which ingredient contains which allergen? Yeah, yeah, yeah, yeah, okay What is your canonical dangerous ingredient list? Damn Yeah, okay. Now we have the okay. So, I mean, I mean, yeah, yeah, and we figured this out The range ingredients alphabetically by their allergen and separate them by commas to couldn't To produce your canonical dangerous ingredient list. Okay Okay, this shouldn't be too hard So we already have the possibilities alphabetically by allergen right not by their name. Okay. Yeah Okay, let's see. Let's see how we can do this now. We have the possibilities here Let's just uh, you know, let's just let's just look at that Let's see how fast this runs actually like on the second drive 34 milliseconds. I mean, that's That's fast So now we're gonna be looking at a essentially a constraint solver But we are going to kind of bank on the fact that the input will kind of never It will never be degenerate like we will always have Like one ingredient will be one of the results Like here and then we're just going to remove that from the rest okay, so Solve can take a map of string of set of strings Map string to set of string Okay, so First of all now solve Uh, now we're going to be looking in the map and we're going to be applying a function To the elements of the map Data.map and we're going to see here Okay, so solve Pause equals. Okay, so now we're going to say where So we're going to say a single Only one pause is going to be map.filter So we're gonna this is going to be so we're going to say that you know One is equal to set set.size So we're going to say, you know the set of dot size should be one right? of pause Okay, and then we are going to say Uh, so pause this is going to be Uh adjust, right? adjust This is going to be We're going to say, you know map dot Okay, so we filter here filter Okay partition Only one, uh, okay, so this is going to be one Uh and more So we're going to partition the set actually map dot partition With uh, you know the this we're going to partition it this way so, you know The first map contains all elements that satisfy the predicate and second contains elements that fail the predicate, right? So So we have one and more. Okay, and then we're going to say, uh remove ones from the more, right? So we're going to say map dot map Uh, we're going to say, you know Uh, so one So all of the ones we figured out So once it's going to be this is going to be the set that unions of A Of a map of set of of map dot elements Of ones one. Okay. So these are a lot of single ones and now we remove those from the more so we say This is going to be, you know the We're going to remove set dot difference Of the one we're looking at and we're going to remove the ones the single all the single elements from it, right? Okay, and that's going to be we're going to map out that over the more So and this is going to be, you know map dot merge Uh, or can we say union map dot? What is it? What is merge here? Unsafe, uh, okay, uh, but that's okay. Oh, we can just say unions Okay, so we're going to say, you know map that union of the one You know the the one map and It's going to be it's not going to be tail recursive, but that's going to be fine and solve the mood Ones map union in the map dot union. And what is it complaining about? It doesn't like this because Map did you map? Yeah, I mean I fixed it already, right? It just didn't pick up my fix for some reason Okay, now let's say solve Possibilities Is it is it being super slow? I think it might be super slow right now I think it's because we didn't we didn't do head recursion here So, okay, let's do the let's do that It's going to be a solve prime So far Buzz It's going to be a solve Map dot union So we have all of these in here Where we don't need to do where it's here actually Uh, so map dot union one and and So far it's going to be a new And the No on the removed ones Just going to be is going to be solve prime And it's going to be solve prime Of a map dot empty and what what is it complaining about now? Solve is applied to to many few arguments Yeah, okay And this is visible like this Let's write something else, you know It's just uh, it's just linking it's taking so long to link. I think that's the issue here So we're not even started, right? That's a problem Let's see see what happens. Let's see what's happening Why are you taking so long? Okay, so it's all it already Okay, it already linked but like why is it? Why is it not printing out starting, you know It's just not printing anything first Let's see Let's I do do this again Sound like something fell in my room Always scary, you know What why isn't it Just doing anything Let's let's issue here Okay, so so gtcid is just screwed for some reason No, it's linking This runs I mean and it's also it shouldn't be taking that long to just print solve the possibilities for Just a test input, right There's something going on here, you know Something weird going on here. Does anyone spot anything weird? I can't see Okay, let me just restart the whole editor I think I think maybe that's that was something piling and linking What is going on? Like why is it so? I mean, it's not even just not even printing starting. You know, it's just doing the Okay, let's see here like let's let's just make it print anything Okay, so it gets into a loop Oh makes sense so if it is so we have to say solve prime of so far and pause in case You know the size of pause so map.null Pause equals so far like this I just iterated that it didn't it didn't show any Didn't show anything, right? Okay, but I figured out which is which Yeah, we just didn't have the loop We just didn't have a base case, right? It's not good Okay Now let's solve for the input Oh, wasn't that slow, right? Okay, so we get the We get the list and like even if we print all of it It's in 243 ballistic. I mean we could do better, but you know, this is Fine, I think Now we are going to say This is supposed to be as List of strings, okay, so we say Sol equals solve map, okay, so now we've got the solution Now solve to output That's gonna take a map of string and set a string What it's actually going to do and it's going to return a string, right? So what's solved to How to put now all of these are going to have length one, right? So we're going to have a We're going to have we're going to say map It's going to be map dot Assox So as socks equals map dot assox of mp It's going to be map of associations of map, right? So there's going to be a list of key comma values And now we're going to map over that map Map a so we're going to take you know a list of key comma vowels This is going to be a set, uh, but we are just going to just like You know take the Hugo like take them like take the root of the set uh data dot set Uh, I think you can just say root and it will just give you like if there's just one element Then you can you can just get that element, uh root Do you do a split root? No, I don't want split root. Well, I want index element at uh, how do I just get like Like just the first one Uh, it's fine. I'll just do find min, you know And it's going to be k comma find min copy Now we're going to say a sort By a compare On first and we're going to map second Of this right so we're going to you know get them in alphabetical order and then we're going to say map second and then We have them all in the second. So let's just return those like string Uh as ox I'm supposed to I think I should be okay doing this It's all to out put So this is all in data dot list and data dot function. So let's import those Import data dot list import data dot function And it doesn't like this because I don't have a dollar here Now it doesn't like this because like, you know, it could be a by functor, you know But I'm not going to use a by functor here That would be too much Sol to output Let's see, uh, okay, we still have the traces Let's see, uh, you know what what trace show idea. Let's move these Move these now Uh, we'll save now. It's not okay. So find min. Yeah. Yeah. Yeah. This is This is set to find win, right? Oh, she's running the same thing again earlier. Okay now Exactly so and then we should join that By a comma who will so Intercalate I think it's called It ain't getting getting getting Ding ding ding ding ding ding ding Yeah, so we can just say Intercalate comma dollar Like this. No, what what's wrong with this? Yeah, this returns the string, right? And this is just like this Okay, I think I think we got it. I think we got it right now. Let's see Uh, yeah, this is exactly the string for the test input Now, let's get it for the real input. All right, compiling and running. Let's see. See what happens Uh, that's one again 43 milliseconds and we got This Check it We got it And you know, we didn't do any performance optimizations and was still 43 milliseconds, which is That's just how long it takes to boot the dut rts, right? So We're good. We did this one and zero point one like we say in Iceland or zero one nul etni what we say Uh, pretty good. I think Just some you know, just you know, but like this is also the cheat we're using that we know We could kind of constraints all by because we know that there's always going to be one that maps to exactly one, right? We don't have to do like a pair wise constraints off. So we're we're always cheating With with by knowing That they're gonna be generating the input such that you can do this thing Which is yeah, which is a bit odd, but you know, it's cool Okay, let's add a 21 at hs input and test input Get diff head, what did I do with the day 16? I added a new line. Okay get commit a m day 21 Get push All right, so we solved day 21. I thought it would be way harder. I thought like the I thought like the that the like the difficulty level will jump up again on day 21 Uh Have the previous days been hard to me. How is How are days, you know 17 through 20 like last three days? Were they hard? I know Timmy does these so He should have an answer I hope he's typing it right now. I'm waiting for him to type it Okay, it was a monster We can look at let's look at day 17. I don't know if I if I want to I can I can stream for like an hour or more and I'm gonna See something else, but uh, let's check day 17 Sort of Conway cubes Do What what yeah, I mean, it's not that hard. I'm like Conway cubes Oh, yeah, someone told me On twitter, I remember that this was like a another another one of those game of life things, right? Looks at his neighbors some of that stuff Okay, I'm not gonna like it's not it doesn't it's not that hard. It's just I don't know I don't I don't feel like doing it right now. Let's let's check out. I just want to see what the other days are You know What we'll be dealing with later Oh Okay, we're doing expressions expression order, right like this is This is literally What we were doing before so yeah part four isn't two isn't four dimensions. I mean Don't we just add one coordinate and then Like update it again, you know Like I don't want to do anything where I like have to do Linear interpolation figuring out. I just do it, right? Now, okay Addition still finds some yeah, okay Okay, but the operator presence of change rather than evaluating multiplication before addition The operators have the same presidents and are evaluated left and right regardless of the order in which they appear A what? Okay, I mean Okay let's Let's let's just do day 18. I think I'll think we can do a day 18 right now Uh, let me let me just take a I'm gonna I'm gonna switch to the intro. I'm like I switch an intro stop recording Then start the recording again because then I can more easily do this Let's see. All right