 Hello everyone and welcome to today's advent of code. We're streaming a bit early today Because I have an event to go to Let me just finish it. Sorry. I forgot to set up one thing But it's yeah, we're half an hour off before schedule. So Yeah, it's yeah, we're half an hour So when people then Tune in eventually, you know, I'll be fine. Huh? Hey tan up zero. Do I know you? People have all these handles. I can't remember who's who but I feel like I know 10 up from somewhere It's like a planet Or yourself Anyway, welcome to the stream. I have to admit I My friend was discussing this problem today So I know how the output Input looks not the input. Yeah, how the example looks, but I haven't you know, I haven't like worked on it But I gave it a tiny bit of thought. That's just what happened. Oh, you see the input in there Anyway, let's get started on today's advent of code. By the way, is the audio working today a Because yesterday was like working on the recording as working in OBS But it was not working on the stream. So I hope Yeah, sounds good great Okay, so today We're yesterday we had like a machine and we were exploring the virtual memory of that machine now We are Looking at tree houses Tree top tree house. Okay So are all planted and carefully in a grade The excelsis when the previous acquisition planted these trees is your reforestation effort now They're curious if this would be a good location or tree house first determine Whether there's a good enough resin of tree cover here to keep a tree house hidden To do this you might to count the number of trees that are visible From outside the grid when looking directly along a row or column Okay, good. They also already So I knew this right we were talking about some length over I thought maybe we had to do diagonals And that would be really hard, but now we're actually only looking at rows and columns Okay, so they already knows a quadcopter and Each tree for some of that single digit would well use its height Where serious shortest in line is the tallest a Tree is visible if all of the other trees between it and an edge of the grid Are shorter than it Only consider trees in the same row or column that is only like up down left right from any given tree. Okay All of the trees around the edge of the grids are visible Is there already any eyes or no trees black moves in the example? I don't leave the interior nine trees to consider. Okay top left five is visible from the other top Okay The 16 trees are visible on the edge another five visible in the interior a total of 21 trees Consider your map how any trees are visible from outside the grid. All right, let's start off By grabbing the example This is fun because a Seems like we have an actual, you know, we actually need to do work That's not just a parsing, right Okay Module main where so is everyone here to know Haskell did anyone here listen to the Haskell interlude this Haskell podcast You're recording an episode today And I think it turned out okay read input is gonna give us a file path to IO and we're gonna return a list a little something we're not gonna do any fancy array stuff today read input equals parts that over read file Where parts? Okay, so first We're gonna split into lines, right? It's gonna be a list of strings and now we're gonna do map a Dig it to int I think it's like this Let's say import data.char Dig it to int Fmap and pars. Let's say pars is supposed to take pars. It's gonna take String to a list of ints, right? Dig it to as a lines Map map dig it to int. I think it's like this. Now, let's see here read input a Example Into print. Let's see a CD8 ghc oh day 8 a day 8.js and we are gonna Day 8 we're gonna time it also Okay We got the input so What I'm going to do is so I'm gonna I'm gonna So because we're always gonna look like okay, so so like from this side, right? So I'm gonna consider it like so first like do it like like a scan like this, right? 303.7.3. So check which is visible from this You do it like this, right? And then I'm gonna reverse and check what's Visible that way and then I'm gonna transpose it and then I'm gonna trans reverse the transposition. Okay a so Let's actually now a Let's map like let's map this let's add the coordinates there Okay, so a add chords It's gonna be a list of ints to a list of list of int comma int And these are the chords because we don't want to We don't already print them right add chords chords, okay Okay, this is gonna be sip with zero Sip with sip zero. Okay Do do do do do do do so what is the problem here? Uh cute match type a zero b zero review with a Sip with a inner The type of this inner it's gonna take a list of hints. Ah What if I do? Oh, yeah, okay, because it's gonna be It's gonna be the other way around So this is gonna be it's not gonna be this type, right? So it's gonna be int comma This is sip with a sip sip zero Okay. Ah, no, right. Okay. Yeah. Yeah. Yeah. Yeah sip with this is gonna take like the Okay, this let's let's make it back into the original chords int comma int Int okay, so sip with okay, so this is gonna take the like the row index Row index and it's gonna say a Sip with okay And then we're gonna take the the column index and we're gonna take the value Okay, and we're gonna return Row index column index Value Okay, let's see here And now let's turn this into map m print. So it prints every line Let's see what that does Okay, it prints every line and now we're gonna say add chords So it's supposed to be like yeah, zero zero three zero one zero zero two three Okay, and now we made sure that uh, so we're gonna collect all of the chords Of the ones that are seen Um, and then we're just gonna make sure that we're not doing too much like over counting, right? Okay, so variations of ours Let's call this a type Coord A type chord Co-coord list Equals so variations it's gonna be So now we're actually not gonna we're just gonna join them. Okay, so that's gonna be again a list a list of hints It's gonna take a list of this and we're just gonna go like copy things so variations of x's equals a A x's plus reverse x's plus transpose x's Okay, so First we're gonna we're seeing the uh, no I need to map reverse x's Okay, let's let's print out the variations here. Okay. Okay. So first we have a axis Okay, then map reverse axis Okay, and then we get them all from the Uh from the other side, right so in the other direction, right? So it's This is the first one And then it starts with four zero four one four two four three four four zero, uh Right. Now. This is the first one. Yeah, okay, and then it starts with from the other direction Okay, and then we're gonna say plus plus transpose axis Plus plus a map reverse transpose X's So these are all the variations Okay Now we are gonna say a scene in a list of uh int comma int comma int Okay, so in one of these lists So we are gonna have the scene here scene Okay scene of empty list is empty list Ah wait, so scene equals scene. Let's say scene equals scene prime Uh, and then we have the min so far This is gonna be zero Because that's a minimum height so far Where scene prime a min so far Ah no, this is actually max so far max so far empty list equals empty list So we don't actually care about this one uh scene prime a Max so far a max so far And this is going to be a Tree and so it's going to be the cohort and the value We don't actually care about the cohort here So we're going to say a and then the rest so Okay, so are they visible if they are If all that is between on the edge of going are shorter Okay, so if it's shorter Let's see. So max so far Okay, so let's say a case V bigger than max so far equals So then we're going to say tree Add and then we're going to add the scene prime a v A r otherwise it's just scene prime max so far r And now it's complaining because I think this one is not Correct, okay Now let's so this is a scene and I want to say Print variations and then I want to say Map scene Okay, now we get a bunch of trees. So from the left edge We can see the three and then we just see with just the three and then we see the seven Okay Now I am going to take the first of all of these. Okay Map a map fsd Well map scene Map map fsd Now we're going to drop all the values. We just get the cohorts And then I am going to Make this all into a set. Let's see the task one It takes in the list of little pins and it returns Okay, now that's going to be all this stuff task one equals. Okay. Um Task one So we map and we have all the cohorts Okay, and then we do a set dot from list Import set for this import a data dot set set Import qualified data dot set as set So we map all these sets and let me do a so then we have I'll uh a list of a Sets and then we do that dot unions And we're gonna do a set dot science now Print task one Boom 19 I feel like the answer is 21 Yeah The answer is supposed to be 21 We have some zero sized trees Yes, we did have zero sized trees on the edges. So we started out with zero Okay Let's see if this works for the input. Maybe we felt Let's see. I didn't do input Okay, maybe this might be too slow 12 milliseconds. Let's see if it's correct though. All right We did task one at 20 minutes All right, that's not bad. I think that was quite good. I mean, but it was also like I suspected like the input was crazy high Uh, like I'm big but I thought okay, maybe yeah ah looking up Okay, let me check Okay and Let's see Okay, then with the amount of trees of the coverable else just need to know the best part to build the tree house They would like to be able to see a lot of trees To measure the viewing distance from Q&T look up down left and right from that tree Stop if you reach an edge Gonna be a lot messier The elves don't care about a To measure viewing distance from a Q&T look up down left and right from that tree Stop if you reach an edge or at the first tree that is the same height Or taller than the tree under consideration If a tree is right on the edge at least one of its viewing distances will be zero Those don't care about distant trees taller than those found by the rules above the proposed tree house has large eaves to keep it dry So they wouldn't be able to see higher than the tree house Anyway Looking up its views have locked you can see one tree Looking left its view is blocked immediately you can see only one tree Okay, looking right as you look up I can see two trees Looking down its view is blocked eventually you can see two trees One of height three Then the tree of height five that blocks its view A tree scenic score is found by multiplying together viewing distance in each of the four directions Okay, for this tree This is four found by multiplying one times one times two times two So consider the tree middle of the fourth row looking up its view is blocked at two trees Looking left as views not blocking is a two tree looking down is one tree Looking right its views locked at two trees By a massive tree of height nine Each tree scenic score is eight Okay, so we just find the trees and then we're multiplying That tree scenic score is eight. This is the ideal spot for the tree house Okay Right So it definitely maybe do we definitely want one that's like seen from the edge? Okay, let's see I'm not like I'm not getting an idea here like oh, this is a good algorithm, right? I feel like yeah, we can't we could just calculate the score for all of them, but that would be A waste right, uh, is it going to be some dynamic Thing all right to where like the score of tree three Is equal to It's going to be like equal to like the scores of the trees around it, right? because Yeah, so like the the the right score of tree three is going to be Uh, it's going to be like one. Let's see what we can do here some dynamic programming stuff Okay, and Anyone have so anyone has anyone solve this part? I'm gonna have like a good idea here Um, okay, let's say let's change this array. Okay. Uh, now, uh, let's see a Data tree is going to have a tree It's going to have a height It's going to have the Courts, uh, it's going to have the These are the things we're going to be updating right it's going to have the left value It's going to have the It's going to have the up down left right like up up value Down value Left value and right value Left value and Right And and right okay now. Um So let's say let's actually create these trees. Okay. Now, uh task two. Okay, so let's say two tree Takes a list of these Takes a list of these and returns a list List of tree two tree Uh, yeah, okay. No, this one takes a it's actually just takes it Okay, and a it takes a returns a tree And it's going to be c and v and this is equal to t So the h is going to be the value Let's just call this c actually C and h is going to be v and c equals c u equals zero d equals zero l equals zero and r equals zero now let's say here a read input example Example and let's print a Add cohorts and and then two tree And I actually need to do driving show and I'm gonna say a It's gonna be map map and let's see what it says Ah, yeah, of course, it's gonna be like a bunch of Let's do map And Okay, so these are the trees Okay, now Let's actually write the instance q eq tree where we're gonna say t of c equal c a c1 equals t c equals c2 is equal to c1 equal to c2 So we just compare the cohorts to make the trees, right? Let's just actually t1 equals is equal to equal a c This is from data function Okay, and now we're also gonna We actually want to make this into an oar instance We would like to be like that heights, right? But actually we have to say compare equals compare on Coort, right? And then we're gonna have all the coordinate Okay, now we have this tree. Let's write a show instance Instance show tree where Show t We're gonna do some record pun in here We're gonna derive the show we're gonna say And show this is okay, so we're gonna just so each tree is just gonna be represented by a It's coordinate, right? It's gonna be a c c plus column plus a V now this is height All right, so these are all the trees and Now how do we compute this? Okay, so we can do it kind of a So if we're computing the left value Right that will only depend on the values of ones that we have seen so far Okay, so let's do that um Let's actually add the left value then to this Plus a okay, let's not show age, but we'll see your age And then we'll show the a U d l r Okay, so this is the current state of the tree okay, so To compute the left value we only have to look At trees in front of the list Right so to the left of the first one a So the left so So this is so Looking up its view is blocked immediately getting the one tree of high fives. Okay Can we just do it that way like first you compute all the left values then we compute all the right values Okay, and what is the edge view of the On the edge it's going to be zero, right? If a tree is all right on the edge, at least you want to be using this way. Yeah, okay Exactly so, um Okay, so let's compute the a Okay, so for the right so the computer right value compute R Val this takes the This takes the tree And it takes the rest of the trees in the row The returns a compute r val Is equal to a So t T bang bang Okay, so the right value here, so this is like the height Equals v And then okay Let's see here. So it's so how many trees it can see a This is then a rest rest of Row okay rest Equals a length a filter a p at a dot dot to So if the height is less than Or equal to uh Do we really look up left down some of your change out at the first tree that is the same height or three? same time of color And it's that the the he that the Agents less than equal to v uh of the Arrest compute r val Int okay So now we want to like update a row So we write to compute r val of tree Takes a list a list of tree and Uh, uh into a list of Let's compute a Let's just say a View score right score Scores okay, so to to scores Okay scores. Okay. So if we have an empty list we'd return an empty list Okay scores of the tree of a tree And trees is equal to Uh Yeah, so we could be smart right and like not recompute everything but I think we don't need to do that I think we I think it's fine this way Uh, but let's see. Maybe I'm horribly wrong. So this is going to be a a scores. It's going to be it's going to be we're going to Score the trees We're going to score the t A t s And that's that's the new tree, right? Uh equals this and we're going to just say We're going to say t a a Uh, and we have to pick which key it is Jing-ding to tree. So now we're we're we're listening to where we're updating a Here we would need like lenses, right? But we don't have lenses Or we have lenses. So let's say let's say as scores equals X scores is equal to 0 0 0 0 Okay, and this is just up down left right up Yeah, I can actually have it like a No, no, let's keep it like this So score and then we're gonna say a What is a nice way to do this? Let's write this. Okay. Let's just have this be actually s equals map dot from list Zip. Okay, so we're just gonna have a from list and we're gonna say Up Up we're gonna we're gonna do it like this, right? We're gonna have it a bit clearer what we're talking about in each case But we still want to be able to refer it by the key And so let's paste this get into position Uh comma up down left right Up down up down left right Okay, now as a score tree and we're scoring we're giving a specific key Uh, so actually sir can just be characters Uh a core tree Char okay, and then a Right next let's let's not let's not keep involved that in the score function. This is just gonna be Score here Okay, and the score here so where So View score equals score tts And now we are going to say that the This is gonna be e prime e prime scores ts, okay, and t prime is going to be t but a s is equal to a map map dot update we'll go uh map map dot update Ah map Oh, no, okay. Give me a i'm just gonna run first This is a risk if you get into the zone All right Nothing damaged, I think Yeah, I'll clean it up in a bit. Sure about that. Okay now did it up map a This bottle is a bit wobbly Okay, a did it up map dot lazy. What is this? Okay now Insert Okay, and then we do map dot update a Let's actually have this char here We don't care about it here, but this is gonna be the key map dot insert a k a view score A t dot s So what is it saying now a parse error on input scores? Okay. What is the problem here? Ah, there we go. Okay, and then this one was t at Okay, and then this s is because I don't actually find the s here. This is going to be a s Is a map from char to a Import data dot map import qualified qualified Okay Scores ts scores. I'm going to be keeping the same key and then Now here we need a This is actually a new extension overloaded record dot Language And I can do t dot s right, which is kind of cool where we're not going to show. Okay. Yes. That's a That's a understandable Have a nice day show h.com s Okay Let's just show the elements of us Okay now So this is the scores. Okay, so So now we have the trees on the scores and the chars Okay, so now I'm going to say okay task two It's going to be a list of trees to a list of trees Okay, a task two is going to say Uh, it's a list of trees To a list of this trees task one trees equals a map a scores Of right. So first we're looking at the right ones map scores Now let's see what this one does a Do-do-do-do-do-do Uh, so for the first one It's just zero Ah Yeah, because I printed twice. Sorry about that Um, so we get the three so the first tree can see three trees It can see the zero. Uh, wait a minute. They should be two right So zero one can see no trees to the right Tree number two can see One tree to the right. Yes and tree number Three can see One tree. Yeah, and tree number three. Okay, so the end can see zero That's all correct, but the zero comma zero It should be seeing Let's actually look at the example, right? That's this a One comma two Zero Uh, looking right as you can see. Okay, right. So because it's not just filter It's actually a Take while Because it stops seeing after the first big tree, right? So two zero zero one zero. Okay, and then this one One two is yeah, okay okay, so a task two is map scores are so First we score to the right Okay and then a And then we reverse And then we map scores to the left. Okay Boom boom So, okay, and then we need to Reverse again, right? Okay. So now this a A One two is is a one two is two two. Yeah Okay, so the model if I like left It's view it's blocked immediately. You can only see One tree of height five right next to it Okay Because now we're taking while like it's less than or equal to but we have to break So we're actually want to say a take while A So now we take okay, and if it's less than okay, okay, let's say where Score funk, okay, so we have the SC okay, so we have here we have the value in scope Okay, and then we have the so we have the current tree T and then we have the rest of the trees and This is equal to It is equal to Case a v is less than or equal to so h is less than or equal to v. Oh, no. Yeah Okay, so the height is less than or equal to v Okay, if h is less than the current value Then it's equal to uh one plus SC of trs Okay, so if it's then we continue if uh otherwise it's equal to One right? So if we're blocked it's one SC if empty list is zero No, this is not gonna be this is gonna be This is gonna be sc of rest Okay, so this one Five so it's zero two two They can rise me was not blocked looking left its view is blocked immediately. You can only see one tree. Okay, so Okay, right. So It's view. It's gonna only see one tree of high five. So this is the one in one two Five, so this is five and then why is it saying two two a Let's just show the s Right. I use uh word is again the terms Keith as ox as ox I want to see like paper to mute. Okay, so Uh, it's dlru Okay, so it's d D lru Uh Uh, but these are all symmetric, right? They're all equivalent, which is a not correct, right? Okay, let's see. Uh So this one works for the map scores are right. We figured that out so Yeah, this one can see two trees To the right this tree here was one and three one two Now this one here is immediately blocked. Okay And And then Okay Now I want to see If I reverse it, right So I have to do sorry It's not reversed. It's actually map reverse Let's not have this scoring function. Uh, we need the scoring function because if we have many equal trees in the road won't stop Okay, uh So this is actually gonna be Map reverse and then we have to map reverse again. So this is d Uh, l u r, right? Okay. So now it's saying Five to the right. You can see one tree. This one can see one tree. Let me see Yes Okay, this one. Yeah. Okay. Let's go to map reverse. Okay. Now we do this one Okay Now let's say transpose this transpose Try and figure out whether that's up or down uh So you take this like this and then we go to transposition So zero zero zero zero but Yeah, so this one becomes zero. Okay now so it's going a It actually doesn't matter, right? Let's say Okay, so Map scores are and then map reverse. Okay. Yeah. Now we fix it again So then we want to transpose it and we want to a map a scores a I think this is a No, let's just check what it says A compared to the S3 Ah I guess the terminal. Sorry about that. Do do do do do CD day eight and we run it back Now so it was D L u r right now we're doing D. So that's a first key So three can see a Uh, I need to transpose to get sorry So zero zero can see a Yeah, I can see two down this one here can see two down Okay, okay So then we're going to do transpose and map scores D and then we're going to say map reverse and we're going to say a map scores U Okay, and now we're going to fix it. So we're going to say we're going to say transpose of map reverse of Now this is looking like A proper Haskell expression right map reverse map map map transpose map map map Like that Okay, let's see Okay, now let's go for the test tree. So that's saying two one two one Do So two one two one. Yes Okay, now this one should see two two One two I think yeah, this is one one So this is down so you can see one down. Yeah one down Looking right you can see two Okay now Let's see for the a Now okay, okay now we want to score them So let's say a Okay, we're going to say a max score okay, so a total score takes a tree There's an int total score is equal to a Product of a map dot elements Okay, this is a total score of a tree so task two a E let me see Uh, so let's say here. Okay, this we do all the transpositions Okay, and we're after that we want to say map a total score map total score a map map total score We map the total score over it And and then we do Map and then we do maximum No, I think it's just max and then we do a max No, what is max here max is say Oh, yeah, right. So it's not max. It's max a mom And this is going to be max a mom by maximum by a It's probably You did it on this Uh maximum by a It doesn't like it. Oh, yeah, so this is just maximum but it's not mapping it Total score it doesn't like it because Uh total score maximum by a hook. Oh, how does maximum by work maximum by Oh Okay, right maximum by a compare on He doesn't like that Oh, right. So we'll find the maximum by again a maximum by compare on Total score Of a Okay, we're actually gonna say here Okay, let's just uh, let's just ignore this Tree, okay now a Best tree is equal to maximum of maximum of map maximum of Map map total score Okay, now let's see print task to a best tree I'm gonna map it where I guess I'm gonna print it So we get eight here, uh, and that's probably gonna be the Let me see here So I want this to work right so maximum by compare on a Total score, okay of the a map maximum by This is gonna be give us the best tree Maximum by I think that was what we were feeling before on total score Okay, now we get the best tree Let's see and that's gonna be that three comma two this one And the total score a score Forest Okay, total score a total best tree Okay, it's a best tree. So task two task two is equal takes a It takes a list of list of ints And returns an int, okay task two is equal to A total score of best tree of score forest of map map to tree of add codes And score forest score forest forest Score forest. Okay. Hey, let's see here And now we just do task print task two Okay Boom eight. What does it say for the x input? boom That is a big input, but let's see if it's correct All right We got it and it took us an hour not bad Good thing we started early uh Yeah, I mean did it in 82 milliseconds. We could have done better, right? because Let's see here gets There's a git add the 8.hs input Example git commit Git push All right, we could have done better How um We could have been no more like dynamic Right, we could have been a So we always look at all the trees, right? But you can kind of say that okay the the tree on the edge has scores zero Right, and then the trees like this tree And then you can say like, you know, if it's bigger than the tree next to it Then it has the score of the tree next to it plus one Right So and then we will kind of you know, tie the knot we start off here and then kind of compute it backwards But we didn't do that. That's just You know, we we would have done it if we had an even bigger input and we would have been running it for like multiple minutes or seconds But at this point, it's just a premature optimization, I think But I like it. We did it all point free. It was all map map dot map map scene map Transpose map. I like this. This is a You know, this is a work of art. I have to say Right map reverse so we score right and then we boom and then we score again And so always but I think that's the key to making this as simple as possible, right? We have to keep Um We just look at one in a row and then we just flip it around, right? Then when they just kind of update them by Yeah, but okay That's gonna be it for today. Thanks a lot for tuning in and I hope to see you all tomorrow. Uh, we have a Christmas party at work so Maybe I'll skip tomorrow and do both on saturday But I have visitors for the weekend But not on sunday evening. So they leave on sunday evening They arrive. Yeah, okay. So we we might do Okay, this is what we'll do I actually have free time in Between 10 and 12 tomorrow So we'll do like a stream, right? We at 10 You know, this is pre-lunch We're gonna be fresh fresh We did like a pre-lunch stream Uh, hopefully we managed to solve it in like an hour and a half And then I can do lunch and then I had because I have three hours So I'll be able to solve it. Uh, I hope it's not super hard And then we're done with that, right? And then we can do the saturday one early as well And then we do sunday one late. Yeah, because you know, I've had it where I do a couple of you know Many per day, but that can sometimes say I mean, it's okay, but then like they pile up and if you get two like one hard one, it's really hard to get into a A You know another hard like another one after that. So let's do it tomorrow 10 o'clock And uh, I hope to see you then All right, thanks for tuning in and uh, see you early tomorrow if you're there But if you're not there the recordings are always on you too. So It's gonna be there. All right. Thanks a lot and