 All right Welcome to the stream We're doing day 16 of the advent of code I Took a break yesterday To go to a wedding, but we're back. Let's see if we manage to catch up and Solve it quickly, maybe we'll manage to catch up today. I don't know I'm waiting for maybe two hours Not too long, but let's see Let's get on with day 60 All right, the floor will be lava. So we use the lens library. I like that shout-out Believe light is complete focus on where law of production facility Relative to K. Okay Finally the heart of the mud you see bright lighting coming, but okay, we focus the light on something Camera closes facility pulling. Okay The attraction should be a flat two-dimension square grid Getting empty space mirrors and sliders Okay, the contractions align some of the beams on set around the grid But each tile in the grid converts some of the beams light into heat to move the rock in the cabin We have them layout comes in in the upper left top left corner from the left and heading to the right Then its behavior depends on what encounters as it moves If the beam encounters empty space It continues in the same direction the doom encounter is a consistent mirror The beam is reflected right so it goes like Ching It goes through here and here So if it goes here, it would start going downwards. Okay If the beam of contrast the pointy end of a splinter That being passes was better than today's okay, but Flat side of the splinter is here The blim is fit into two beams going in each of the two directions on the splinters pointy ends are pointing Uh-huh. Okay One that goes up and one that goes down Cool Okay, let's parse this quickly into something that makes sense. I Think we're gonna use a an array Because we're gonna be navigating around and We want that that random access for all It's the best kind of access well You have to Keep it all in memory though But that's okay. That's what we're gonna do. Anyway example Okay, so my mouse is working again It's not working for a while But now it's working again. Let's all celebrate that. It's also third advent This Sunday Because Christmas day is on a Monday, so like next so the Christmas Eve is actually fourth advent That is crazy yo Anyway in Iceland we lit light four candles. We haven't been home these Advents, so we haven't been doing that But that's okay We're doing advent of code instead Wow I feel like my laptop is slow today I get the rendering is just a bit slow, but it's okay. We're still we're still on a server The solutions time shouldn't be affected but I'm gonna go ahead and Save everything here Now how do you do an array? Let me see so The things in this Great are gonna be data G great entry, so we're gonna have a Empty space We're gonna have Reflect 90 Okay, so let's have another data type that just says data angle. How is the audio by the way? Is everything good? I feel like Like my laptop is not in a good mood today. I hope it's going. Okay. Oh, yes is reporting things. Okay, but yeah You never know. Let's see up down left Okay Mm-hmm I'm actually gonna just make it an array of characters and Reflect 90 and then it's either It's going from Left to up left come up It's reflect a and then it's angle angle. No, we're gonna have the Left corner and upper right Right corner, okay, then we're gonna have a splitter And that's also angle Left right Left right or Right or down I feel like and I feel like it's actually like You can imagine these slashes Also as a splitter, but it's just Just it's changing the direction, but it's not creating too anyway So, let's see who go Did it out array I Never remember All these things work We're not doing by the arrays If it's in container, I think it should be in containers like IRAs and MRAs and Pascal container package What what is going on today Jesus will go so Map dot strict data dot map In containers Yes, okay good Canators don't have arrays data dot Ray Yes, and you reach a ray package make sense Let me see. Um, I want to just say here Let's just import data array. I'm just gonna make it figure out the types itself Do print so read file Example Into print dot bars, so bars is equal to Lines a parse is equal to Map parse entry Parts This is strings into this is you can be a list of this the Great entry. No, let's say pars where parse entry Dot is equal to empty parse entry Slash is equal to Reflect a from a Left to Right, so it's either left to right or right to left, right? How do they describe it in the text? So right from the customer. Can you continue upward? Let's actually make this just an angle, okay? So this one I'm coming from the right. I'm gonna go up parse entry I Think these are all the same. Okay. I think it's it's actually, you know, it's like this so It a great entry is just a GE of list of angle. Okay So this is GE empty This is GE up and If you come from the left, okay This is GE down Maybe these type selections are gonna bite us in the end Let's say we need to do it like this because otherwise it's gonna parse entry So I think there's a nice choice of entries here that would actually let this Up down, let's kind of say if you go through this Each of the copies is supposed to Create Okay, let's say It's a 16 15 It's a member. Oh my god a ball install leave This is what it's a bit annoying with GT up is it doesn't come with all the batteries included So now I feel like See and then we just installed it because it was already there. It's just kind of not exposed Globally, okay Deriving show Mm-hmm We should probably add you know EQ or whatever here Okay, so we have empty list up down dot dot. Okay Now I want to make this into An array and there's gonna be a mutable arrays Let me see bounce on the ray specifically high also the lowest and highest boundary very effectively And a one origin okay Okay, then we're gonna just say Let's make this into into common and then these are gonna be into comma Okay, and this is gonna be a map So we're gonna sip and then we're gonna sip Map is a pleasure. Okay, and you do dot here Mm-hmm Okay, and the sip with okay, and then instead of Doing like this, I'm gonna say here I Who I Ls to and We're gonna map a J comma e to All right, comma J comma e less Okay Right, and then it's like this Mm-hmm. Mm-hmm. Let's say again Okay, now have the oldie associations and now let's just Why can't I Concat all of these and Print bars and then concat. I feel like I should be able to do concat here Because I didn't change the type here. Okay Mm-hmm Nice, okay, and now I'm gonna make this into an my array What is this you array is that? Unboxed, okay array and Let's see array int int Grid entry, and then we are gonna say here import qualify data tree as R import data dot array array Then what I'm gonna say here Is I'm gonna say R dot from list is a list array. Yeah All right at array and we just say that the bounds should be Zero comma zero and I'm coming on red Yes Dot nice Okay, and And then I can kind of move around in the array, right? so now let's see Do so a are is it just this and now let's see return here print R and actually be able to do print R and 1 1 2 Yeah, okay, so as now I have fast access to To the values in the array cool It's all quite nice and fast so now we have the Layout in here So now I'm going to Let's see now I need to write the opposite function Opposite Up is down Opposite is up Opposite Left is right And the opposite Of right is left So we will let's just so we start We start at zero zero And what we're gonna do is that every time we are supposed to split the be we're just gonna save that in a list of Beams that we have to process next. Okay, so we are gonna say here Part one and then we can probably memorize it later It's gonna be go So it's gonna be Empty so we're gonna start with The beam zero comma zero that's going right. Okay And that's gonna be our initial one So where go and what I want to see is If I so what what tiles have I Seen okay, so we do and We have to have a set for this Import Did a dot set set import qualify Set set So we're gonna have here Set empty So go So if I don't have any beams anymore I'm just gonna return set dot size Seen now, okay go seem Beam coordinates and beam in direction And these are the other beams Okay, so here oh Okay, so what I know is actually that and Let's make this a map dot empty actually map dot empty and We're gonna make this import data map Map import qualified So we're gonna make this a map dot empty and map dot scene Okay, so here we're gonna say a go scene case Just a Visited so scene Just At PC Seen map dot bang question mark Let me see if I've never seen it before case Seen map dot bang and BC of So just there's okay, so now I'm gonna see if If I've seen it before And it was going in the same direction is not gonna change anything because it's just gonna hit everything again so If BD Set dot member You see set up What is the type of set on member again? I never remember it never remember Which comes first the map or the okay the element comes first if BD That dot member Cheers Then go other beams Otherwise then go seeing other beams else Okay, so it's basically this okay, so let's just do it here just there's Seen Seen map dot BC so we have seen this tile before first of all and BD set dot Member Cheers is equal to go see other beams Otherwise Okay, otherwise Okay, otherwise we haven't seen it before Then we have to create Okay otherwise So then we say let Seen prime equal Map dot singleton a How does map of singleton work? Do-do-do Okay, so ski value, okay map that singleton PC and then set dot singleton BD in in a mean Okay, so say map dot insert Haha, okay, I don't need to do that. So map dot insert with this and then PC and And Set dot singleton PD And this is a set dot union let does is seen Prime Is equal to this. So this is a new set. So just gonna add this new direction. Let Seen prime equals map dot insert with Set that union and then the coordinate and then this new and then seen That's the In Go seen prime Beams prime Now we need to say beams prime. Okay, so beams so Now you have to add new beams so Let's say we actually we figured out the new scene prime. So let's say case And then now we're gonna look up in this array right our We're gonna say case R R dot bang BC of Okay, and Let's say here move X comma Y So we're moving to the left is equal to But I Yeah, I zipped it So it's actually Y comma X as usual X as usual. So this is gonna be Y comma X minus one If I go right, it's gonna be X plus one If I go up, it's gonna be X minus one actually and If I go down It's gonna be Y Okay case RBD of G E empty So if we hit the empty list then The new beam then we're just gonna say Let's see New beam It's equal to okay, okay if we Hit the empty list then this is gonna be the same as move BC bd and we say data dot array we look up and What happens if it's out of bounds? Probably get an error, right? Okay Okay, so the new beam is just We just do it we just have one element Okay, if I hit the One with that one case Then You see I think they Let's see, okay, if I hit an up and that is a case PD of so if I hitting one that goes up which is This one, right? Yes, so if I'm coming from the Right, so if I'm going right If this is so case bd of right The new direction is up. So we are just gonna say move BC move BC PD and Then the next one and it's also bd. Okay move BC Okay, if I'm coming from the Left it's move BC Down down If I'm coming from the I'm coming up Then it's Move BC Then we're going we go right then we go left And the other case here is down so if I'm coming down, okay And if I'm coming from the right, I want to go down If I'm coming from the left, I want to go up if I'm coming if I'm going up I want to go left and if I'm going up down, I want to go right Okay, now there are two more cases the splitters Oh, this one is up down. This one's supposed to be left right Okay, so if I'm coming from the From coming from the right or left if I come in the right or left the D Is left or D is right Mm-hmm Then I want to go One that goes up And Otherwise it is just move BC bd Same here with left or right left Right If I was going up This is one this one was going up or down Then I'm going to go down and I'm going to be left and left And right and right Otherwise, I just continue going and this is the new beam In Let me make this a bit smaller maybe Do-do-do-do-do-do Mm-hmm Okay in go sing prime And we want to do a new beam other Eames Okay Let's just be here depending on what moves Okay, I guess if it If it If it escapes the bounds Then if it escapes the bounds then we just Let's just first make sure we remove Anything that's not within bounds, okay and BC BC PD and I don't care about this And I want to say case So let's say these are and then let's say Why okay, let's just make sure that So a bound so are Got bounds you see a XY so let's see X larger than equals zero and Y isn't larger than equals zero let's get a BX comma BY out of Ray Bounds of are And then let's check that X is larger than zero and Y is larger than zero and X is less than Or equal to BX and Y is less than or equal to be why right and then this is just RS so if this is if not this then go Don't just keep going go as RS You see probably gonna That's import preload hiding left right These are from either so language gt-2021 Num angle move Incumbent and go to Incumbent Let me see and Go is supposed to be Go is a takes a map of Int comma int and set angle And call it angle no yeah to a List of Incoma int Comma angle These are the beans and will return and it's and it's because I looked at the wrong thing here. Okay our PC Okay part one Array in comma int Um great entry to Um It's okay, so that be I Because it gives the lower bounds and the upper bounds I get it. Okay LBX LBY UBX UBY Okay It does do something now, let's see print art 1 Okay, I guess 46 for the example and does so quite fast Let's see in part one regret entry and I just need to print it Doesn't seem to work for the example now, let's just see if it works for the input Before we start debugging I think it should work Basically, we kind of memorize it at the same time as we write it with this Error in array index, okay Mm-hmm Do let me see Trace show Trace show and Just a treasure actually a PC Intentations goes brrr Yeah import Debug that trace Mm-hmm, is it just not even Print dot part one, okay, it works for the example. I thought I specifically checked the We First parse it and say are to print Or not bounds are to okay Return return Error in array index. Is it just failing on the parse then? Okay, that's funny That's okay. That's the problem It should start with zero comma zero And it should end with Mm-hmm red equals f red equals f F stress Okay, so to start with zero zero and we should end with the first of the last of Race Race This is stress Okay, now we have the actual Index here and print Part one Eight five three nine, okay Seems like a reasonable number. All right, we did part one whoop whoop Take us 40 minutes though, but we did a lot of work The render talks in your shirt. Let's you know about the collection of buttons Any edge tile and heading away from that edge Friends of the bean starts in the bottom right corner start heading either left or upward So the beam could start on any tile any tile in less Then the bottom row and you tell the left most column or Any tile in the right most column To produce lava you need to find the configuration that energizes as many tiles as possible This is gonna give 51 tiles are energized By the initial beam configuration that energizes the largest number of tiles Okay Let's just write here fun takes in Income my int and angle and returns and And then takes an array And then a list of this Returns an int, okay And we're gonna call this part fun our beams Beams Art one R is equal to fun our Zero comma zero right No, this is work Yes Okay now and I think I'll just run it over all of them. Okay So part two Heart is gonna take So we're gonna have a where LBX LBY UBX UBY is equal to our dot bounds are Okay, so The edges are gonna be Edges are gonna be It's gonna be how do I do this so we're going to have the Left edge is gonna be map to so zero comma I comma right a Edges is equal to that this mapped over zero No, it's actually but it's gonna be this LBX and then this is We're gonna have this be why and this is why and this is From LBX no from LB Y To you be a Uncatenated width So these are the ones going right. So this is the right edge and then we have the left edge and then we're gonna have the Going left and that's gonna be the UBX I think I need to say yeah What's one actually to make it do the end And then we're gonna do for the X's we're gonna do X and Lower upon Y and this is gonna be going down and it's gonna go from LBX to UBX plus one And then we're gonna do upper bound X go up You and then it's gonna be lower next. Okay, let's just write edges here first and See what happens Part two so we got all the right ones from zero to nine It goes to ten also Okay, so we don't need this plus one here. Okay This one goes to zero to nine and then we have a I Think it's only edges. Okay Maximum maximum of map Fun R Fun R Oh, let's just I can make this This is just fun or Probably be better if we like share the share the caches between them But whatever Let's see if this is super slow or not What are you I just Guess 51. That's not 51. That's 48 Being considered any time in the hop around heading downward I messed it up. These are actually The coordinates are flipped right so it's actually These are supposed to be wise Why why there's a B. I'll be X and these are gonna be X and X But I think they're both actually We get 51. I think because they're all square these things here actually don't matter Let's have it correct Let's have this correct. Okay So now this is the right end of the left edge, but this one should actually be going from a Yeah, they're correct Let's just see how bad is it for the input It could take some while Okay, seven seconds. All right, we did a 16 Whoop whoop Didn't take us too long but all through the power of Ray Let's see. It says Get at day 16.HS You commit M Day 60 Did push Okay, let's go to day 17 then I don't know if we're gonna finish it today But it'd be good to get some progress on it. Maybe we'll finally caught catch up, right? Or not finally like we're just one day behind Because we skipped one day. Let me see. Okay day 17 Anyway, I like the solution here. It was just a raise and let me just brand of thing. There was no cleverness. It was just about getting a correct Memorizing with this Realizing that you're getting have a beam Going hitting the same tile going in the same direction. It's not gonna do anything new Okay, but now day 70 Oscar Allen says it wasn't too bad Ooh, look at these CSS effects. There is lava coming down This is crazy stuff. Okay, let's say clumsy crucible Lost a flung long the okay, that's a parachute because we get to gear island And so giant factory city Okay, we have a lava fall Large crucible top heavy unfortunately Difficult to steer and so I can't go straight line for a long Okay Need to find the best way to get the crucible from the lava pool to the machine parts factory To do this you need to minimize heat loss While choosing a route that doesn't require the crucible to go in a straight line for too long Fortunately the elves here have a map that uses traffic patterns and being damaged a hundred parameters Okay, this is gonna be some breath for a search. Yes. Anyway That's I think we implemented it so many times In the past few years and it's high time we did it this year, right? There's a module main where main IO main equals return And First let's parse the example I feel like each city budget might buy a song single digits of the amount of heat loss The starting point of left city block the destination The machine parts factory is a bottom right city block Because you already start in the top left block you don't Encord that blocks heat loss unless you leave the block and then return it to it Okay, okay can move at most three blocks in a single direction before it must turn 90 degrees left to right So that basically says Hello, yeah day 17 little Annie. We finished day 16. We're gonna take a quick look I think I'm gonna stop in like 40 minutes. I Need to sleep also, okay One way to minimize heat loss is this path That Never moves more than three consecutive blocks in the same direction You can cross a heat loss only 102 Okay, so because it's not just breadth first search, right? It is also We also need to be careful that the path satisfies the Property it may only turn left and in a straight or turn right Mm-hmm Okay, let's just start with The breath first search then how do you do the breath first search again? We also we don't need to Mm-hmm Just remember what it was for it search three straight vector node that says like in property Okay, and then we do they once explored and then route and if we use a call that we turn we Maybe I should like for every note say that you can do I Getting some milk delivered Well, Q is not empty too. So we need to like a priority queue and then we just need to be careful With the path that we take right Mm-hmm because it's not valid to just say I take the shortest path So what if I do breath first search and then For the neighbors as a thing, right? I can't just say up for the neighbors just if you've gone three times to the right just throw out those neighbors, right? Because The path it's path independent, right Hmm because I'm not like I'm also, you know, we're also in that case where I I just have like half an hour left and I'm like, am I gonna start doing breath first search and then that's not gonna work So I want to kind of do is just pause it here and Let's just do day 17 tomorrow, right? And just give me some time to think about it. I feel like I Feel like I need to think about this a bit So let's see Path here, so it goes three four and then Three to three one We could also, you know, try Just the super greedy algorithm, right? It's always just chooses the Chooses the smallest one it can Just to say kind of you know the shortest path to this point Is the shortest valid path to all of these points, right? And then the shortest valid path To any of these points and so on, right? So you will kind of do it recursively But backwards Well, that's problematic by saying that Yeah, maybe we can do it like backwards like backwards shortest path kind of Okay Okay, let's say but let's start at least by Harsing it we're gonna do something similar as in day 16. We're just gonna We're just gonna have all these Things, you know, we're gonna talk about directions and Angles and stuff let's call this dear instruction and Then we're gonna do import prelude hiding left Left right, so and we're gonna we have to import all the all these dependencies Okay a grid entry here, this is actually not great entries, it's just numbers And parse entry See is gonna be read at int C and we have the parse and they are directions Let's do read file sample Print dot parse Nines Gz o day 17.hs 17 day 17 17.hs and time Okay, then we have this grid Array, all right. Okay, then we can have something like paths and like scene. I Just don't know how to like you were just supposed to find the shortest that would be easy, right? but I don't know how to It's quite hard to put this extra constraint on the path of that Mmm So what I want to do is I want to Yeah Okay, but at least we have we have it parsed for now and we can move around in it, right? Yeah, I'm gonna pause for now. Sorry about that. I am just not Getting like, uh, this is how we can do it so I'll pause for today and Hopefully come back tomorrow with you know something written down like I had for a green serum and Yeah, we'll see how we fix it. All right, but thank you for tuning in today and Hope you enjoy the break and I'll see you again tomorrow for well day 17 Again Thank you, Oscar. Okay. Bye. Bye