 already reduced matter of code so i haven't opened the task yet so i have no idea how it's gonna be but i've opened the github repository and as we all know that's the most important part of starting the project it's picking the name the github repository all right let's go advent of code 2020 yeah i like this you can hear some sweet low-fi christmas songs just to set the mood all right so okay so we've saved christmas before through previous advent of codes so we're gonna be doing it again but we decided to take a vacation okay and on currency cash on me okay and then we get stars i think these are the advent of code stars okay so okay so we need to get all the stars to save our vacation that's okay um let's see i think i'll lower the audio a bit for me it's quite loud in my speakers like this and then i'll have to put it up again for you guys yes outlifts on uh i'll have it on like twitch unless yeah i mean there's no copyright so these songs here are copyright free so i can just put it up and everyone can watch if you're interested in seeing how haskell is it done by a self-professed haskell expert all right um so we have to get all 50 stars we get two stars per puzzle and we need 50 stars by December 25th so um yeah so in the us they celebrate christmas on the 25th all right so but that's okay we can work on christmas future employers remember that no okay so uh before you leave you need to fix your expense report apparently something isn't quite adding up specifically i've divided two entries that sum up to 2020 and then multiply those two numbers together for example suppose your expense report contained the following okay and then the two entries that sum up to 2020 are 1721 and 299 so we multiply them we get our answer okay um let's see let's see how we do this um so i have here um a directory pick out with uh two code and let's say module main where a demo input let's make it a bit bigger oh no that's not bigger at all module main where uh test input equals and let's just do it we're doing it live this time i don't think we actually need a you know module here let's just say this as a day one dot hs all right um we're going to put it in okay so we're supposed to find the one that sums up to 2020 multiply those two together i think a good way to do that is um for each number we simply deducted from 2020 and then we check whether the other number is a member of the set yeah what i mean so so what we're going to do yeah let's fill this in test input test input integer so i think this will be quite easy actually um might not take too long but i think these will get they get harder and harder as as the day goes by so we have int we're gonna assume they're ints i mean we have integers in classical but we don't usually need like you know unbounded integers and um i think i think that's the case for advent of code essentially you never get the never actually have the um the what am i trying to say here yeah yeah you they don't actually use like unbounded integers usually okay but let's check it out so how do we solve it so for the test input we can just do um we just do um so yeah so so what we're going to do is we're going to okay so here we here we have the formulas okay x so let's see x plus y equals 2020 then x and then y equals 2020 minus x okay and now we want to go and we're trying to calculate x times y um and we're going to rewrite that to be um so i think because because we don't want to figure out the number again so we'll just write here uh x times y equals x times 2020 minus x equals 2020 x squared now 2020 x minus yeah it's gonna be like this minus x squared plus 2020 x that is going to be our solution um so how and so that's so yeah so we're going to calculate okay so um so we're going to say so first we check for set membership right so we do um lambda so okay for each um okay so uh f fx equals okay so so let's have input take the input and then we say case uh x like this and then i guess that kid x member input yeah okay let's just do it like this if x member input then uh return uh mine then just minus x x times we don't we just we don't need to do anything like this then just x times 2020 minus x so f takes a list of integers and actually it's going to take a set of integers and it's going to return a maybe int okay and um so let's see what's what is it complaining our person for or yeah okay it needs to be like this so set yeah we have to import data dot set um and now it's complaining that okay yeah so we take in an int and then we so we take in this int and then we take we take in the set and we take in the integers like in the set so uh so and then we just I think we should just write a tight loop I think that's the easiest way to do it um so we will just do loop equals okay so we say a some loop input okay um so okay if we if we have nothing at the end we will give nothing loop and if we are a input so what else do we know here we know that we should probably map yeah we have to map we need to map 2020 over x for everything okay so let's do it this way let's just imagine we have list okay f a loop equals okay um let's do it differently okay uh okay let's let's just write the loop input equals where mod input equals um map 2020 minus over the input okay so solution takes in the input okay so uh the modified input is going to be map 2020 over solution takes in a list of ints and returns a maybe int okay uh so set input equals from list input okay and then here it doesn't want to map it oh yeah uh import data that's set set import qualified and data set as set uh so here this will be set dot member and this is set from list okay and then we map 2020 over the input and that's that's the one we're gonna do set dot from list on actually um and then we do uh so loop nothing equals nothing loop uh looking at the first element we say a case a a f mod input x of just a just solve we return that just solve otherwise we do loop if it's not a solution we just do loop xs okay and wow i can buy followers um okay so solution and then our our solution is just to loop over the input this is just gonna exit tightly i mean there are functions in the library for this but we don't actually need to do it that way right okay so and then we say main io main equals uh print solution of the test input and let's just see if this open up a terminal here uh jc jc day one it's gonna take a short time so my setup is uh i have a vs code and then it's so it's like actually running in a virtual machine um like the vsl machine um and then the vs code just kind of hooks into that which is actually a pretty nice way to do development these days so like i have windows i have like all the email stuff um i have all the graphics rendering stuff you know i have chroma windows which is just and you know it's i don't know things work but i can still use my linux development tools which is like best of both worlds okay let's run it day one just five one four five seven nine which is the answer to the problem um okay seems to work um so now we need to do some reading of files so let's write that function uh from file uh it's it's going to take in file name and it's going to return what's the integer so from file equals read file this is actually file path file path read file okay so we read file i don't quite remember what read file does let's just run jci say type read file so read file just gives us a string okay so we just uh we read the file which will give us a string i'm gonna i'm gonna try and do this in a one-liner okay it doesn't count in ask what's not a one-liner right okay so the read file will give us a string so we're going to have map a big function so unlines first no lines so this is going to have a bunch of lines with each each number in it so we will get the lines then we will map uh read add int over whatever sort of lines and now this yeah we need type applications because we don't want to do we even need that i don't think we need that uh i think i think we have enough type information already okay so yes what is it saying now yeah okay so it's complaining that so expected take yeah from file uh f so because like if i write this f here and then i would dollar f they should work right so lines goes from string to less than this go go from string to any and then this f maps it over and now it's complaining that okay let's not do it in a one-liner i'm sorry guys so uh where in let's let's let's let's cheat a bit let's do it in not a one-liner and then we merge it all into a one-liner for i equals read okay so okay a s so do s is going to be oh yeah okay okay that's what's happening i didn't mark this as i o now it's a problem one-liner okay um so now if we do print solution test input so we read the file um and then so what we're gonna do is we're gonna do from file input and we're gonna pipe that into a printout solution let's uh let's try it out before we actually download the thing uh don't don't be okay vim oh we should probably open it here right new tile input and then we paste the numbers we got originally we compile and we run and it just works uh all right now let's download the input so here i get your puzzle input copy link address what's your favorite way of downloading files on the internet um you can i usually use curl ssl and then you know the name of the file and then i say oh and where i wanted to go oh puzzle oh no okay i need to actually yeah they're using cookies anyway let's just do this with it okay two nine nine two nine nine let's see if it works work for our tests right that's the right answer all right we got part one so i like getting that part one right so we done the expenses now what's the part two okay a the elves in accounting are thankful one of them even offers you a starfish coin they had left from a past vacation they offer you the second one if you kind of find three numbers in your expense report that meet the same criteria okay yeah this is like here we're being punished um with um we'd like optimize it too much we use the math to kind of simplify it so now we actually need to say i think we have to compute the sums we have to compute pair-wise sums of all the numbers and then we have to figure it out so let's see let's see let's see here i think let's let's let's do the math a bit part one okay so what's the part two now we have that plus y plus z equals 2020 and then and then uh and y times z is going to be 2020 okay um let's just rewrite this i'm just wondering if there's like a shortcut here that we could use to just do it in one go instead of having to transform the set into like pair-wise combinations huh um okay let's see i mean we can also just do the combinations right this is task one it's gotta be premature optimization if we try to do it anyway different i like it's not gonna take that long okay so uh the way you do combinations in Haskell is i think like there's a cool there's a cool trick so you can do like one two three uh i mean yeah okay import i think i think there's just like a full literal function that does this uh combi what was it was it like one two three and then like i think we monatically inject it into itself this monad okay let's just go like i don't remember this trick there's like a uh it's like a because there's like a let's just let's just make it let's just do it let's just do it right away so um combinations i mean it feels like cheating if we don't do it if we do it some other way um it's gonna be int int right but we just want the multiple the sums okay so then we do combinations okay so we're gonna do this prime where combinations prime uh that's so far equals and this equals so we're gonna do like this so if we have this is empty we do which is returned so far combination let's call it call it some and we have access we put here and then we need to put here uh let me okay let me just say no let's just do it with a map right so uh map uh what we're gonna do here okay so let's let's write it in the terminal okay so we have this one left l equal one two three right so if we map um map a star let me do oops map a x star a this will return us a list of functions right uh yeah this is like this will return a list of functions right so this is like this is gonna be you know this is where like we could use some applicative magic to do it right um but it's gonna be hassle so we do map i keep pushing enter when i'm yeah i got a new keyboard so it's filling up a bit the map star x x star uh and then we're just gonna join them okay and now okay yeah this is from controlled on mona okay and now let's say sums test oh now i'm creating all of them right that's why i wanted to do the that's why i wanted to do it differently so that's why i want to loop um so can't do this and empty equals empty sums equals and map lambda x no we're gonna do lambda y here x times so we're gonna have y we're gonna have x comma y comma x times y this okay and then we calculate this plus plus sums oh no okay yeah it'll be like this uh so what are some some sums sums prime takes in uh but this map here oh yeah no it returns like int comma int comma int that's why it's wrong uh okay so what is our test input again uh one okay so we have one comma two one combined with all of them 14 56 and then we have the the rest right so now we've gotten all the combinations that's cool okay um so right okay um but this shouldn't be the sum it should be the the plus here right okay and then yeah so here we have all the sums and now we want um because we want x plus y equals 2020 uh x plus y plus z equals 2020 right oops um x plus y plus z should be 2020 what does it say here 9793 to 66675 i think this is still doing the still doing the yeah okay so here we have yeah 979 1345 right 3 to 6 and then it's saying 675 okay so um so we take these pairs let's actually let's actually flip them around right because we don't actually need x and y so we have x plus y and then we have x times y like this um and then we will put that into an int map so this will be a list of pairs of x plus y and x plus uh so we're doing parentheses here um this will be an int map let's import that import data dot int map int map so an int map is just uh uh it's just a map specialized to integers right because they are so common um okay and then we import the int map and here it's complaining that oh yeah okay what this was sums almost wasn't doing all the whole oh shit okay i think i was doing it wrong um yeah okay but now this should work okay and then so this will return a list of integers so this will be i am dot from list okay so now let's see see 655 665 so here you're saying here 1345 okay so now we're going to find a solution input it's going to be uh where i am equals um sums of uh the input and then we're going to look up in the int map so we are looking for a function here i'm going to show you some of my research actually uh so uh so this is solution two solution two takes in the list of integers and returns a maybe um int and then we want to apply something to the int map we just made and this we're going to use typed holes which i so i added um suggestions to typed holes so if you do here you do control period well there's no code actions available uh let me see uh because this will be an int map and okay so four so it's essentially okay so we are going to say here search empty equals nothing search x axis equals uh so if we didn't find anything that's just bad but if we have something we will say uh okay something and then 2020 minus x and then i am okay and then it will suggest to us find a hole um so it doesn't it doesn't realize that this is an int so it was let's say this is an int to um okay where we are saying here so we're looking for something that takes an int and a map of ints returns as maybe anyone say valid whole fits look up which is exactly what we want so case look up this of just uh res is uh just and then we do x times res otherwise we search need rest so let's uh load day one dot hs again okay um expected type uh okay so this is i am i am a sums of input oh yeah this would be i am dot look up yeah why did you do that huh so because this one suggested um sorry i'm finding a bug in my own compiler code uh valid whole fits okay it seems to be the scope is wrong uh okay i think it's because the the module so we've done it correctly here this should not be happening scope look up whatever because it does suggest it right no okay here it says i am dot look up it's just okay i think this is a ghc ide issue i have to tell them about this anyway um i am dot look up so we compile it and run it now this will run solution two and let's just solve number one um uh actually let's uh let's do like this and load it and we let's run solution two on um um two four one eight six one nine five zero two four one eight six one nine five zero it's all about the christmas music um okay so we solve number one from file dot solution two let's run it two eight seven seven three zero one six put it in here we completed day one all right so that's the end of the show today um i assume these things will get harder as time goes by and let me see here get status get add day one i just let's just add the input as well was it and let's see here um yeah so you can find my solution right here you want to check it out yourself um let's so we have a bit more time so i i'll get like an hour for this so so the thinking here is it's it's just classic program you know it's lists you put it into lists you put it into maps when you look up in maps right so we were we had a good time here in solution one because then we just we could just use a set because we only needed to find the other element and then we could calculate from that what it should be but here we actually needed to we needed to find two elements right um and so instead of like doing an n squared algorithm or like n and and cubed algorithm or you multiply like you take one you try adding it to every other number and then multiplying you know we just we took every number we we added them together and we multiplied them put it in the map and and then we you know just do it so there's like a reddit i think gaming there's probably better solution to this but this is like uh so i would think of it right and you can see it's all functional but um it doesn't look you know it's not too bad right it's just you just follow how what do you think about it right we found a buggy at cide i will link them this twitch i could just show them right away um how it how it doesn't work and uh yeah so any questions two or three minutes just write any questions in chat um or you know just send me a message on on twitter or facebook or something if you want if you want something you don't understand here but this is it's the first challenge you know it's not gonna be it's not gonna be so much um not so much work actually but um but yeah i hope i hope this is clear and then um i saw i really liked last year's sediment of code i think last year the year before that where you had to write this machine i think it was last year you had to like program your own virtual machine and you had to like compile assembly instructions and then a lot of the tasks were like you know make this work um in the like writer program in the machine you built a model off and that was and that was a lot of fun but i was like so the problems usually with these things is that they're very unfunctional you know it's not so much hey parse this or do this in parallel or like here's a list-like structure you can map over so we usually end up you know writing these kinds of tight loops to to do things right and this is just a loop in a functional loop right you you do something and then you uh you uh you keep you kind of do a function call at the end and then like it's just because the tail request tail call optimization this will turn into a loop behind the scenes and it just i mean it runs super fast right i mean i don't know like seven milliseconds you could probably go faster right but you shouldn't you don't need to optimize any further all right thanks y'all for tuning in um the poll will be available on twitch and uh hope you to see you tomorrow at the same time um where i will do part two or day two with the advent of code all right enjoy