 And we're back with day 18 of the advent of code We just finished day 21 But we like the look of the day 18 problem So we're gonna try and solve that And see we can solve it like within an hour And then I gotta eat But let's see Now parentheses can override Okay, so we just parse it, right? Do-do-do-do-do-do-do-do-do So we just have to write a parser, right? Let me see the input Okay, it's always just a single digit Okay Okay, let's see. Let's see. Now. We're gonna say here day 18 New file input. No, that's Hey, we're gonna say new file Day 18.HS. Now, let's see Module main where? Yet Okay, so expression. Xper. What is the expression? Data Xper is going to be It's gonna be add Xper Xper or It's going to be Mall Xper Xper We're gonna be par Xper Deriving Show, okay, so parse expressions We just write a parser, right? import text dot par sec We don't need a fast parser actually let's just do import text dot parser combinators dot parser dot char What is it? Text dot parser. No, this is I think we're looking for the read P1 Because I don't even go to parse. I don't I think we don't we don't need it So I parse an expression So a parse Xper Pick a string and gonna return as an expression parse Xper stir equals This is gonna be no, this is yeah, this is a read P Xper So it's always gonna so or yeah, okay, so or digit int parse string, okay, so is it gonna be a Symmetric choice. Yeah, okay, so it's gonna be Let's see So this is gonna be Yeah, it's gonna be a Digit like there are pars and digit It's gonna take a string and return an int an Xper and pars and digit is going to be a Digit and this is gonna be a parser Xper right now digit It's going to be This is gonna be, you know, it's gonna be get Actually So I think I think what I have to do is I have to do do Yeah, I'm gonna yeah read P so do so skip spaces, right and then get and we're gonna say See It's gonna be get and then we're gonna say digit So return digit of read add int See we don't have to see the ad Okay, so this is gonna be pars expressions gonna be do Skip spaces now it's gonna be choice. Oh, so it's either gonna be you know It's gonna be Pars digit and what's true says char pars digit You know, it's gonna be it's gonna be here now, okay, so Pars add that's gonna be read P expert, right? Pars ad is gonna be do so it's gonna be digit D is gonna be, you know, the number and then we're gonna say, you know skip spaces a Char plus And then e and then skip spaces and then e expert He's gonna be pars pars expert and we're gonna say return a D e pars is gonna be up as it is gonna be It's gonna be jar Okay, so pars expression Is it gonna be take a char to a function of You know a function of expert expert and is gonna up to e Up to e, you know plus it's gonna be add up to e times it's gonna be mall and this is gonna be up To e and it's gonna be up to e and it's gonna be of the op Applied to D and E. Yeah, probably right So Here parts expression is gonna be do There's gonna be a choice of We will you know parrs Plus so okay We can either parse a so I think you know, I think it's always gonna be an expression, right? Yeah, yeah, yeah Okay, so we're gonna parse a plus we're gonna parse up a Star we're gonna parse Again, then we're gonna say here par par Is gonna be you know, it's gonna be of a read p expert And you know par is par. It's just gonna be between Char like this and then Char closing And then we get the yeah, and this oh, it's gonna be it's gonna be here And now this is gonna return an expression So we're gonna say par is expert And now we're just gonna say, you know f map You know, it's gonna be whatever this returns injected into par, right? No, yeah, yeah, like we're gonna we're gonna say, you know par dollar like this I Think that's okay. So we're gonna parse parse par or pars and digit. Is that crazy stuff? Oh, no Where did we go wrong right here, okay now pars par Par's opposite applied to two arguments char read p is only one. Oh You know Okay The yeah, we need to apply this right to so I main is IO main equals So we do so we're gonna say yet input that's gonna be you know, it's gonna be a file path to a IO of expression and yet input is just gonna be It's gonna be you know now, okay, so we say so the instance read Expert where Reads reads It's gonna be a where it's gonna be just Well, how do you how do you again use this? Read P. Yeah, okay. So it's just a read. I forgot how instance read Making a read instance that go flow. Okay. So what do we have to define again reads Prack and This is a read s right? So what you're gonna say Read reads Prack Care, but the is it's gonna be read p to s I think let's have a typed whole par's expert Get input is going to be read fmap read over file and this is gonna be get input Test input into print Now here read p to s. Yeah control period repeat s nice Digit, okay. Yeah a digit Parse op the digit digit, okay a pars digit Exactly now to express okay, but D a pars up to e. Yeah, this is supposed to be like this. What happens here Oh, I don't have Test input. Yeah, make sense. Okay a two plus times three Times 45. Okay. Let's see here Let's just see what this becomes Test input. Oh, yeah, and I had to run it also I'm not gonna measure this. I mean, this is not a Speedrun, right So we're adding one and then we're multiplying two with adding three and we're multiplying Okay, and now we're just gonna say, okay. Oops. This is say 16 Now, okay eval expert Eval expert expert to Eval expert D So digit I it's gonna be I eval expert par par e is gonna be eval expert E eval expert at a B is equal to eval expert A plus eval expert B I mean, this is like parsing except like we don't even have to deal with presidents. That's crazy, right? eval expert mall AB is equal to eval expert A Thanks expert B. So, I mean, this is like Haskell at its finest, you know, we're doing Haskell Just writing the parser. That's what Haskell is good at Now what did we get for that question 95? That's not good What how did we get that so add one times two That's it. Let's trace here And there's no parenthesis here, right, so oh now I think I'm doing the I'm doing what you should do, but instead it's it's it's not doing that, right? So digit I and then party so add AB Wait, let me see. You're going right to left. Oh, yeah, so it ends up being Am I going right? Am I going right to left? Are you sure? So we have add one digit to let's okay. Let's let's just check actually We're gonna have a list of expressions And we're gonna say Read Or here a read file print map, you know expression now Do now that we will I just want to say preload par no pars. Damn it. I think it's some white spacer. Oh My god Jam it. It's not parsing the ones with the parenthesis, right? Okay, so one plus two is Doing it be so okay, so let's see. Let's just fix the evaluation, right? Fix the evaluation first Okay, so it's not supposed to be 95 So one plus two is three Okay So we get we get add digit one Mall digit two and three, right? Yeah, okay, so that's the thing, right? It's not actually in like an expression, right? It's a It's just like a series of commands, right? So this is how you would actually evaluate an expression, right? But so add one Okay, yeah So for parenthesis, but for ads so these are always gonna be a digit, right? Let's just do like this. Okay, so if we have add With a digit How are you gonna do it? Let's see Maybe we'll just fix it. Let's just say an expert to list Okay, this is gonna be a data op is gonna be a No sheet This is also the problem with using read P, right? Because it just says No, you know no pars and then it doesn't actually tell you anything So add no, so we wanted to like this add add a See that's gonna be, you know, it's gonna be a plus B so it's gonna be you like for a you like for B and then Times I think like this and then from all if it's gonna be yeah, and then it's gonna be times Can we get away with doing it like this? No Damage, okay, let's see So open close open close parses open followed by a P followed by finally by close only value of P's return Okay, so we parse op plus so it's gonna be, you know Parts digit skip spaces Let's sit right here That's it but here and here we had this right Okay, let's fix the parsing and then we can look at the other stuff So how do we parse a par? Well, we said just say do I mean, okay, so you're gonna be you know, do we skip spaces And then it's gonna be char prime and then par is expert and then and then char like this Journey, let's take a no par is I guess it will skip spaces and then One plus, okay, and then it's trying to do this, right? So skip spaces skip spaces But then I mean this is the equivalent to the previous one, right? But that one does not parse Okay, okay, I think I think I have to fix this because this is like how you do regular expressions, right? Like I'm not not the regular regular expressions But like this is how you do expressions usually like you anybody one part with a second part you Add them together, okay? Okay, so okay, so evaluating add a B One plus two times three, right? And I'm not I'm not doing any president stuff TV says right you're going right to left and find that hard to believe I'm going here six times five So we'll do one and then so if we want to do one plus two times This restrict So, okay, so let's see How you all doing enjoying Christmas so far is a bit of different Christmas, right? Not the nicest one. Yeah, it's something. I was at this work. I Can't just flip these, you know, oh well, I can actually So I flipped it and it's still not parsing this one. That's okay Yeah, but I still get 95 right because it's associated. Yeah, okay, let's try that Let's just literally reverse the string. I still get what the why isn't it? Why did it parse it the same way? This is Why doesn't this reverse? Oh? Wait, I remember seeing I'm reversing the order of the lines not Not the actual string first. Yeah. Yeah, this is like six and then Okay So we can we can just reverse the string. That's funny. I mean, and that's a correct approach, right? Because you know It's gonna be you know, whatever this results in Plus six, right? So we just reverse the entire string What it's gonna do is that it's gonna calculate this entire thing And then add it to this so that's that's that's a cool cool solution to me. I like it No, let's make it hard parentheses I'm gonna say what is going on here for this expert? Okay, this works. Let's see. It's not good It parts. Yeah, so you can't we can't just do this, right? Okay Let's say what we're trying to do is that also here here I said parts Digit now that doesn't work because choice Pause par come on par is good. Good. Nice. See this is what happens on gsc on windows, right? Sometimes it just crashes Okay, so it works on this I'll copy paste these It's just like here. I was only pausing a digit, but it might it could can be a it can be a Okay, yeah The the arguments are either a digit or an operand run. Let's see So anyone fifty one twenty six four three seven twelve thirty four one three six three two Okay, it works Works for all of them Now can I do away with this stupid? Reverse thing. I don't like it How can I like, you know fully evaluate? I want to fully Evaluate the expression be here All right, let's just let's just run the sum on this thread. So we're gonna print the sum Evaling the expressions Already got the input and now yeah, so we just parsed the input backwards But the key thing is what we have to rotate the parentheses as well, right? That worked. Okay, let's see if this is our answer. Hey, we got part one Whoop whoop Let's see if part two works Okay, now additional multiplication now additional like a different present levels, but they're not the ones are familiar and said additions value it before multiplication 231 didn't we get 231 earlier? When we were doing like the weirdest thing here, so it was like add, you know, if you're adding All AB then you first I was like this, right? But I feel like we saw it before right? So 231 51 46 Okay, this is supposed to be 329. Okay two times three plus four times five So that's supposed to be we're supposed to do Three plus four times five So number it's supposed to be two times three plus Four times five Well, thanks for subscription who's saying Co-colly cool No, it's nice to get or not it's not subscription, but I'll follow but you know, I appreciate follows. They are Poor man's subscription. You always need more follows, you know, what is wrong here? So if I think it has something to do with parentheses, so we got it correct if there's no parentheses Now if there's parentheses between so Okay Oh, yeah, that is pretty hacky Timmy that is wary hacky I'm gonna see See one south Let's see because now we have to care about presidents, right? Let's see Okay, let's see here now parts expert to let's just write it again. I Instead of you said so let's write it differently, right? Parts expert to so I because I don't I don't like I don't I think this is a bit weird So, okay, so parsing the expression. So, okay So we're gonna do So it's gonna be it's gonna be a pars Hey So it's gonna be you know It's gonna be D. So we won it's gonna be digit or par. Okay, and then we're gonna say choice No op is choice a Char plus or char Star, okay And then you do it's gonna be digit or par. Okay, so and we're gonna skip spaces Skip spaces, okay Now case op of If it's if it's a plus Then we are going to return add We're going to return a mad e1 e to If it's star we are going to return And let's see. I think this should work for our original case. Okay, let's see Then we don't have this and let's see what this returns Now we need to fix it to parse expert to It should be pars expert to really no pars Okay, so it's it's a So e1 digit or par then skip spaces then Choice char plus or charge star and then skis based on a to digit or par now, this should really be a Yeah, yeah, yeah, yeah, yeah, okay, so this should be Pars expert Really, right? Let's not let's not test this on the input. Okay, let's see. Oh Because this is supposed to be part cycle to read. I want to do this up without reversing Okay, let's see here. Okay, that's not that didn't work. Why didn't that work? so digit or par and then so this is just digit or par right and Then it's parse expert to and like if it can't How did I write pars expert? Okay? Yeah? Yeah? Yeah? Yeah? Yeah? Yeah? I mean, it's always gonna be an operation, right? It doesn't actually do anywhere, you know, just one number. I Don't think so. No, okay, so it's always gonna be an operation. Oh Yeah, the second one might be just a number, right? Digit expert to okay now you get this One plus one plus two Okay, now we're gonna say times two and Now it's gonna be one plus Like this the parenthesis. Okay, and now we Evaluate that so the trace is 9551. Okay, that's the wrong So is it gonna be I think the par E2 we don't want to trace here Let's see But yeah, the flingless. I want to say par e1 E2 right and then I want to go left to right, right? Does this even change anything? I mean, it's not changing anything, right? Okay, let's let's do this do the same thing as before reverse thing or can I just do Our e2 and then e1 here that shouldn't matter, right? because it's it's doing Yeah, yeah, yeah, yeah, okay, let's just reverse it then again reverse here Let's see what happens. Yeah, we got the correct results again But with our like custom rule parser Now I want to change the Presidents here So we say case op of So how do we change this? E1 2 it's just gonna be E1 e2 Now if it is so if it if you want if you always want a Can we say, you know par Here I mean that doesn't matter for these right it's it's always as if the two bottom ones were parenthesized, right because now if the sub expression is a If it's a multi if it's a addition If it's a multiplication, I want to do here more if this is just I Just I'm not doing anything useful here, right? This is just incredibly. This is just not doing anything, right? We should rather we should not be changing the parsing. I think we should be changing the eval expression eval expression to So if I'm about to multiply Hmm What am I trying to do here? So in this case, I would want to compute a 1 plus a 2 Times a 3 plus a 4 right instead of saying a 2 times a 3 like it's not a 1 plus a 2 times If you proceed for it. Yeah This is gonna be fine. So a And b now I'm supposed to multiply, you know Moll Then I want this to be eval add add a 1 mall a2 Moll a3 a4 It's okay. So here's this is a 1 times a 2 Times a 3 plus a 4 This is A1 plus a 2 Times Don't hear I want to add first right so this is gonna be I'm doing it exactly the wrong way. I think I think I should be not changing this one I think I should be changing this one eval expert to add a Add add a 1 a 2 a 1 a 3 a 4 this is a 1 plus a 2 times a 3 Times a 4 All right. Oh, this is like this I want to change the presidents of this to be I want it to be actually, you know add a 1 Add a I wanted to be add a 3 Add a 1 a 2 mall This was a 4. I think so. I like this It's actually add a at this It's actually gonna be multiplying of adding those two together first and then the last one Now add and if you have mall here and add here this is This is a a 1 times a 2 Plus a 3 plus a 4 This is going to be This is gonna be mall. We're gonna mall a 1 with adding a A We're gonna be this is gonna be hope So this is a a 1 times a 2 plus a 3 a 4 and this is gonna be multiplying a 1 with adding a 2 a 3 a 4 Okay, and so and then it just goes to regular mall. Okay. Let's see what eval express 2 does If it actually does anything different, let's see the CCC Not exhaustive patterns. Okay, and if it's something else eval expert add All right, we need a at mall a 1 a 2 mall mall a 2 a 3 This is you know a 1 plus times a 2 plus a 2 times Plus a 3 times a 4 And in this case we actually want to say This is gonna be the same as eval expert. Okay, and then we have Mall a 1 a 2 a 3 a Add a 1 a 3 A 1 wait, this is gonna be mall a 1 a 4 Right, what's wrong now? Oh, this is evil expert to write and now it's complaining that I to write a 3 a 4 This is actually the same as doing this. Okay You well expert to Add a B equals eval eval expert to a plus like or to be Non-exhausted patterns. Yeah, it's because it's supposed to be just Ain't be okay Okay, it's just doing the same thing again. Damn it. Damn it. Damn it. Damn it. Okay, let's see It's weird though that this doesn't work Is it because I'm doing too many bars here No, I'm not doing any bars. All right, let's let's Change it then. Okay, so did you just gonna be a digit, right? Did you just always gonna be a digit? I mean, I don't I think we have to change the presidents during parsing, right? That's like where you do it, right? You don't you never do it like this We don't want to transform the expressions. I don't think that's clever Okay, so we have digital par and then we skip spaces and then we have choice char plus char star It's key spaces choice Digital par par's expression too Yeah, but I'm doing it like that. Okay Let's see Show ID. Okay. It's only a test input, right? We reversed it. Okay. Let's see school it parsing with Residents, you have this read preck combinator, right read preck What is the read preck? Let's see now I'm doing it like this. I want to change it to Like this, right? But it computes in the same stupid way, right? Let's see parse expression to Now for that one, we're not gonna we're not gonna reverse it. Okay. Now we're reading we're doing read Get input, okay, but now we're not gonna reverse it. Let's see Okay, which means we can't yeah, we shouldn't reverse it exactly so now they have presidents, but Now it's being parsed normally, right one two, okay 51 46 1445 90 Because this one should be so now it's 95 but it should be 231, right? Can I just literally Like try to parse and star before I pause it Okay, now let's try and write it a bit differently parse x for three Do okay So you be parsed you want digit or par and skip spaces and then up it's gonna be choice of Char Star Or Char plus now, okay, we have a star we have like a one so we have like you know something like Plus or we have e1 Star okay, so now if it is if it is If top is if it is if it is Plus then we just do Then we do parse expert Three Let's like this so if it is a plus One plus to have the highest presidents, then we say, you know E2 is We just do do E2 is parse expert Three It's gonna be dig it. Okay, let's let's just fix parse expert two Actually, let's see here. This is supposed to be a comma. No, it's just basis case up Okay, if it is a star One star, but if it is add if it's add Plus Then we're gonna do. Yeah, I mean I would I would yeah, but I'm like I'm still I'm still doing it this way You know, but thank for that suggestion pick. I mean, that's that's what I should be doing, right? And then I can just change the president's but Is this wrong? This should be star and If I if I parse the plus I Want to like finish No, this is more again. This is a plus, right? And this is supposed to be Now if I if I return to plus I want to kind of keep going I Want to keep going until I find a plus See here Many okay, so maybe this is not digital porn. This is actually gonna be Let's let's let's let's say differently. Okay, let's put it up differently, right? Okay, so let's see This is gonna be parse. This is gonna be exactly the same as parse expression. Let's just Kill this and we'll keep this as parse expression Instead of being like this, so we're gonna say, you know It says it's gonna be projects for two And now this is gonna be, you know, a do so e1 is gonna be a parse So it's gonna be, you know, it's gonna be choice dig it or par and parts of deep spaces HR plus e2 choice Dig it or par Come on, par is op Star. I think I think this is like this is the default president So I think maybe we'll need to I think we'll need to like flip it up and like say the the opposite, right? No pars. I think it's because we don't reverse. Yeah, let's see if I can do it like this Or is expert 2 and now it's doing it the normal that way and now it's gonna be This yeah, it won't work if there's no plus in it, right? Okay, we're actually gonna have this as plus And plus. Ambiguous parse. Let's undo not this sort of read. Let's just do Yeah, and then it just doesn't parse the other ones if they don't contain it Because it tries to say, you know, parse something like a digger, par and plus and then Star and then, you know, that's okay. We have to do skip spaces again, right? Okay, so and then it tries to find parts op plus Which this should be I think it's actually be this one much choice digit or par and this entire thing Okay, so we parse op and then so we try okay, so this is gonna be, you know Parasex, but true. Okay, so this is not the choice. It's your part. We already have that So we're gonna like on the top level parse like this. How is it looping? damn it Cuz like it tries, you know, to change all the rub another up blah blah blah. Yeah, this just doesn't work I just don't know how to write a parser. I Think that's the thing. Actually, I have to go now Cuz dinner is here and I have to go get dinner So, yeah, we're not gonna we're not gonna do this today Sorry about that I'll come back to it tomorrow, I guess But it's you know parsing is hard, right? And doing it with different presidents and stuff Special expressions. That's like I don't I never write a parser. I always just like yeah, I've written parsers But then it's always like just hey, you know ignore our presidents. We don't care about it Now this is just dead So we're gonna look at it to later Yeah, it's not going to well But I'm gonna have I'm gonna have dinner now because like the pizza is already here. It's getting cold sad, right all right Yeah, first time we've given up on a task, but it's okay You know, we usually a lot two hours to these things and we kind of already did one today So, you know, and then we were like gonna try and do two, but then the second one is just It's just rough, you know, I spin It's just not going that well Which is a bit disappointing actually, but you know Let's see. Let's give it a year. Let's give it 10 more minutes. I Think we can do it now Did it did it did it did it oops? The jaded emperor It's a pretty cool name actually Yeah, we have so we parse it, right? This is adding except when it adds with you know add to mall did it for nine, right? I mean, I think that what what happens is that you know, this will all Okay, let's let's go back to trying to do it like like modifying the expression. So Because you know, then we don't have to deal with par's errors, right? Okay, so this is this is let's call it transprec now, okay and add He okay add a B So what what we want to transform here, right? We want to transform You know Add mall to mall add, right? PC to Mall is gonna be add Mall a See So we want this one here. So it says so here says, you know add One plus two know that this is incorrect So one plus two times three should be parsed as Okay, so One plus two times three. It's going to be parsed as add one To mall to three, right? We want it to be Add now we wanted to be mall add one two three, right? So add this is gonna be mall add a B See Okay See here Transprec so okay, so it's gonna be transprec this and then we're gonna say transprec Yeah, we're gonna like recursively apply it right transprec B Transprec C Okay, now we want a One times two plus three, right? Which is gonna be mall a Add B C. This is gonna be notice. Yeah, this is gonna be mall B C we actually want this to be Now this will be okay, what does this do now, okay works for the First one But then I think it's not like recursively applying it now. This is gonna be add mall B C Okay, so if it's add If it adds anything else It's just gonna be Which other correct structures we have add power mall okay, so we recover the base cases, right? We don't want to change those So two three one fifty one forty six This one should be fourteen forty five Okay, let's see So I think we should also apply it in the other direction, right? Oh, I think I have to recursively apply it here. Is that just scratch? probably right Yeah Nice Okay, now we've done it We get we get it correct So we so we so you kind of always when we're doing adding and multiplication. We flip it oof That took some effort Now let's see it works on the Input oh shit It's not gonna be too bad, right? Let's see if it's correct We did it we done did it That was a rough rough one Took us two hours Or you did it whoop whoop All right, I'm gonna go have my pizza. I'm glad I gave it 10 more minutes Would have been shame to not finish it this time All right, thanks for tuning in I hope you like this double feature Even though it was a rough one like because I was just like I had to think right I I was just not I was just not getting it. I mean, I think I think the thing was like I wasn't doing this Recursive thing, right? But we kind of you know changed the presidents and we kind of do it for every level again and again again And then it just works. I mean, okay, so yeah, thanks for tuning in. I Think I'll be back tomorrow. I'm not quite sure, but I'll probably be back at like six o'clock if I'm back tomorrow So, you know But we'll see We'll see I'm not I mean and then we can also you know, we'll do these just to have two words, right? You do them all later. All right, thanks for tuning in and see you tomorrow. I hope All right. Bye. Bye