How José Valim Solved Advent of Code 2021 with Elixir and LiveBook (Day 1: Sonar Sweep)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we'll be solving advanced code using elixir and i'll be showing a little bit of elixir live book and everything that's related to it all right let's get it started okay sonner sweep you're riding your own business on the ship at sea when the overboard alarm goes off you are so see if you can help apparently one of the elves tripped and accidentally sent i don't know how to pronounce this world this word it's lee it's lie key is flying to the ocean before you know it you are inside the submarine they'll skip ready for situations like this it's covered in christmas lights then we are in the submarine it has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough there is a little meter that indicates the antenna signal strength by displaying 0 to 50 stars your stinks tell you that in order to save christmas you need to get i like those all 50 stars by oh this is just the setup for the stars yes okay okay yes one star and the submarine drops below the surface of the ocean it almost automatically performs a solar sweep of the nearby sea floor only is my screen this honest report report your proposal input appears each line is a measurement of the seafloor depth as the sweep looks further than further away this is the report some numbers it goes up and down up and down okay this reporting case that is cutting outwards from the submarine the first part of business to figure out how quickly the depth increases but so you know what you're dealing with you never know if the keys will get carried into deeper water by an ocean current or a fish or something to do this count the number of times a depth measurement increases from the previous measurement all right this is good i let's let's get started i'm actually a little bit anxious i i don't get anxious a lot but i am today let's do it manually for now let's copy and paste this input actually i'm going to save the notebook we don't want a disaster so i created a nice folder oss and then advent of code 2021 and day one let's do zero one because um so let's save it uh we don't want to persist the outputs this is fine and we are saving the file you actually can't see the indicator that i click there is an indicator here i'm just going to copy the input for now we are going to try the the input they gave before we go for the the real thing and i'm going to start the runtime so the first thing we need to do is is that we need to parse this input so what would it do is that we want to split this input on new lines and i think we can and and the nice thing about live book is that we can just try things right and get the result immediately you can see even in this case i would not consider this there is a training line at the end so i think we can pass trend true that's gone and now we want to convert everything into an integer so i'm going to write the all right both styles so we can do we can just do a string to integer because we are going to assume that the file only has like valid inputs right like he's not going to have a a letter here that is going to throw things otherwise would want to parse maybe reject bad values but we can use this function that it's going to raise if the if the thing is not valid and we can also if you're just passing a function of of one argument we can do this and now we have this list of numbers and now what we need to do is that we need to put them in pairs right and we need to know if but in paris if the number on the left is bigger than them on the right so there are multiple ways you can solve this problem since i'm already i already started the pipeline i'm going to do one of the ways which is just all pipeline driven elixir has a thing called chunk where we can say i don't remember exactly the api but i think we have mouse over here this is private api okay this is a bug oh it's because i'm on elixir master i think anyway we are going to i'll investigate this later so we can use chunk average to give us the pairs but you can see that this is not good because the pair should be 199 200 and then 200 208 so we want to kind of have a have a roll back and there's a configuration to it the docs are not working so i'm just going to oh i i was going to guess and i guessed good so you can see that this gives us what we want but we have this thing here at the end and then i know this i remember there is a flag named discard we if we want to discard chunks that are not complete and now that we have the pairs it's a matter of somebody recently requested some buy but i don't think we added it did we how do i get the autocomplete i don't yeah we don't have somebody so what we're going to do is that we're going to map those pairs okay so we have pairs of left and right actually we don't need to map let's filter okay so we are going to filter and we want to keep only the pairs where the right is greater than or greater than or equal than uh increases okay so is greater than the left side okay so this is going to give us those pairs and now we need to count them and boom it works amazing okay one single beautiful pipeline so this is the solution for elixir so we filter them to only have an increase and then we count i think we can actually use count to as well yes now we don't need to filter we can because count two allows you to count only the things where the things return true so this is it now what they're going to do is they usually they have a file to begin get your puzzle input and is it a file okay it's not a well it can be a file so here's what we're going to do uh let's start with doing more keynote specific stuff at the end i want to have this notebook i'm going to put it like on github and have people try it out and let's say that we want to make it easy for people to upload their own solutions or something like that so what we're going to do here is that we are going to install kindle so livebook is like is this web app right is this application and kino is how you can control live book from our code so i'm going to do a mix install kino here so this is not part of the solution i'm just getting fancy i could just as well get the input and copy and paste there but let's be a little bit fancier okay so i'm going to download kino yeah i need to install it and it's installing kino and now we are going to have a kino input and functions wait there is something wrong in here it's not listening what i thought it would listen oh maybe maybe this module doesn't doesn't exist kino animate data table ets wait should i be using kingdom mass the main branch we're supposed to have an input thing yeah that's the issue with using the latest branch of everything right i don't know i think i should be using kino from a branch i'm sorry well this is kind of annoying for because then let's try this so i'll have to restart the wrong time [Music] what am i missing here okay yes it needs to be a tuple github live book tab device not configured wait i'm running for my machine we should have or is live book dash dev ah finally all right okay um okay i get the inputs model but i'm not getting auto completion let me check something out so what is the text area is undefined kilo inputs is it's unavailable does this module exist no this module doesn't exist so which modules exist here uh output input okay okay yeah we need to improve the selection when there is nothing this is a report i want you to later all right so now we want to have a text area please paste your input file it is going to return an input to us and this is going to render this beautiful text area here okay it's working it's working so now we're going to do keynote input read the input and let's copy and paste the the thing we had here before see if everything still works and let's evaluate it and okay it works let me show something really cool okay so yeah this is this is working uh after evaluating oh the great question so as i'm changing here you can see that there is a star i was pointing with my finger but you can't see my finger you can see there is a star here let me just show you something cool so right we evaluated everything to works i can come here and mark this cell as re-evaluate automatically oh we can make the pipeline even nicer hold on bigger stronger right and now as i add number here so as we are changing the file it's automatically computing the result for us which is pretty neat all right so let's do the copy and paste thing here and there is an external line but that should not be a problem this is allegedly my answer it computed that for us and [Music] boom it works this is really nice all right all right this is really cool i hope you are enjoying the the live book things a lot of neat live book tricks to learn it's just important that i'm using bang so if you if you go check out this notebook immediately after i put it online you have to be using live book main as well but uh it is it is what it is let's put this in a new section which is part one right so uh we're gonna create a new section which is part two what happened to that input yes yeah so that's a very good question if you're using the release live book i believe that there was an input thing here a type of input but now we moved everything to kino because that allows us to do a bunch of like nicer stuff and like programmatic inputs like select where the information depends on the data set that you now load it and this kind of stuff so we move the input took in part two okay so let's get started so um considering every single measurement isn't as useful as expected there is too much noise in the data instead consider sums of three measurement sliding window okay considering the above start by comparing the first and secondary measurement windows the measurements of the first windows are marked with a their sum is total there are second windows marked b okay [Music] okay so we want to do this is just visual thing right we we it's just that we should do a window okay so compare it with b okay all right so it's the same input do we have something to duplicate cells i don't do we i don't think we do um or maybe we do this is amplify output so we can okay this is a link no we don't have a duplicate thing all right so uh can we duplicate a section now we can branch we can link we can duplicate so copy and paste it is so we are going to add a new cell for the input and we are going to add another cell for the solution i'm just going to copy the solution here for now the this part is all the same okay so let's render this new input i'm going to copy and paste the original solution and all right so what we have so far so uh we want a sliding window and i think innum chunk is going to give us right because we already used the chunk right so uh we want to chunk every three ah chunk avery thank you and we want to step one is this is this it yes okay so wait is this it can you share the input between the two sections oh you're right i can i actually can so i don't need this well we are going to delete this later and then we will see if it works for the final solution we will use no you need to use two elements of the chunk with the next all right so so let's go back to here this is one window this is one window this is the other window so i think the windows are correct or maybe i just don't see what is wrong um let's use the discard because before it's like we had the window but the window was a window off true we want to change it to be a window of three and when we change it to be a window f3 our account oh i'm so lazy copying this um it's going to be [Music] wait we now we want to chunk again right actually now we want to chunk again into pairs of true and now we are going to do the count where you know we have again the left the right and we want to do if nx habit uh is more than the sum on the left i think this is it we got five okay so now what we're going to do is that i'm just going to delete this i'm going to delete this cell and it should reuse the input and then should be the answer to my second question and [Music] it works it works amazing [Music] you
Info
Channel: SEKUN
Views: 1,010
Rating: undefined out of 5
Keywords: elixir, jose valim, livebook, elixir-lang, advent of code 2021, advent of code
Id: mDxJjqx5-ns
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Sat Dec 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.