 All right Welcome to today's early stream and YouTube video We'll be doing day 15 of the advent of code and the problem with doing it so early is that well We don't know how hard it is. We don't haven't heard any buzz. Good morning a little any And Let's get the right to it. Also, you get to see early morning Matty I've had coffee but Hasn't kicked in yet Anyway, let's see if we can make it work. So day 15 lands library This is a Haskell shout-out. You know, there was a camel shout-out before Now there's a Haskell shout-out. Whoop whoop or as I like to defer to it as ghtc plus Haskell No, that was a joke on my Twitter. Anyway So we get the data and light is being collected large facility, but in the mountain side So there's a lava production facility danger of personal pregnancy Okay loose fitting hard hat This is one of these Exactly and then there's a Trebuchet. I think they can shoot 300 kilos and no 90 kilo 300 meters That's what a crappy shake can do anyway Push for help in more warning signs Initialization manual Doing a lot of okay a hash algorithm is a way to turn any string Well, the holiday ASCII string help your algorithm The single number in the range of zero to two five run the hash algorithm string start with current real zero never each character in string from the beginning the term the ASCII code Insert a current value by the ASCII code you just determined Okay, it's at the current role to itself all the way by 17 Set the current value to the remainder of dividing itself By 256 Okay, let's say how it goes Let's get started what is our personal input There's gonna be some sort of fold right Let me see example Okay, this is the old one we're moving this one here and We are going to do Module main where This is the input Still not quite awake module main where main IO main equals Print Hash On Hash, okay. I think I'll need data dot Char right here and we're gonna do hash Have the language problem here language gg hash now Gonna take a series of string now series of char Then he's going to return. This is a string right an int A hash ALG so set the current value To the remainder of dividing itself by okay now, let's see GGI We do import data Char now we're gonna use typed holes I Added that feature. It's so good Did you do it? Exception not a digit. Okay or 72 nice, okay So we find the ASCII value of the current character of the string is the empty value empty list. It's just gonna be zero No, let me see So hash ALG is equal to Hash ALG prime Zero Where so hash Alc prime and then we have like the list. We have the current value is equal to curve out. Okay now hash ALG Okay, and I Wondering if I should just make this into Let's just do it let's just make it into so let's see so we have the Increase the current value so we're gonna say curve out plus order of C Okay And this is gonna be a fold All right, let's do fold our Fold our and hash ALG prime And we're gonna do the initial idea is zero We're gonna. Yeah, that this is the algorithm Okay, and now hash ALG C curve out because we add the current value And then we multiply that by 17 and then we take the remainder Dividing it by 256 Now what does it do? day 15 GCO day 15 day 15.js gives us 84 You see So it's multiplied by 17 to become 1224. Let me see. Okay So we did It's supposed to be 52. Okay, so we did And Maybe this is just because it's fall hard Sometimes I mess it up because it goes the other way around Okay, it's supposed to be 52 Because it's not the same right if it's not a the operation is not important data at least Actually, we don't need this we can just say fold now and then we do need it right Oh little prime Okay, and So the hash is correct. It's a comma-separated list Ignoring like answers for the initialization sequence To verify that your hash algorithm is working the book offers some result running the house algorithm on each step in the initialization sequence Okay, so then we gotta do Let's take this example here And let's go to day 12 day 12 we're gonna catch this split on we're gonna split on all the commas So we do This now, let's see read file example okay split on common and filter not equal to Supposedly ignore the Move we're gonna we're supposed to print out Okay, and now let's Map We do the hash algorithm everything, okay 38253 97 47 14 189 1 8 7 48 214 231 Okay, some okay. I got working for the example. So here's the input now, let's Paste it okay Show more Okay, so it's all one line. That's good We don't need to filter this might as well though Okay, and let's just see what it is. That's what you input 517 551 All right part one turn Let's see Okay, so the hash is working now. What goes on to describe a series of two physical boxes numbers here 255 Okay Focus line friends on the south which was a potential insert remove lenses and necessary Long warning for all boxes a large library containing that sort of a focal length And you want there's 212 9? It's a hash map nice Manual arrangement procedure, okay So each step begins with a sequence of letters to indicate the label of the lens On which this step operates The result of running the hash algorithm on the label indicates the correct box for that step Okay, I don't equal sign or a dash Let's see the example It's close to 12 here. So equal one minus equal three equal two minus Okay, if the operation is dash cut the relevant box and remove the lens with a given label if it is present in the box Then move any remaining lenses as far forward in the box as they can go Without changing the order Mm-hmm There's not already a lens in the boxes in the label and the lens was meeting behind any lenses already So just in remaining a hash map Okay box zero Okay, so it's like a list of values cm equals to Let's see, okay, so Hash map, okay, so we're gonna be maintaining an actual hash map in map Not strict as I am As I am Import call no, so we do this qualified But we're gonna do import data.intmap Just need to work here. That's true. Okay Let's say this is again phone L prime over the operations Update update Okay, and we're gonna start with I am not empty where Update and then we're gonna take in Hold L We're gonna check taking the curl map And we're gonna take in the up and we are going to say okay equals so let's see and It was also first to With a label would be Each set because of the sequence of letters indicated the label of the lens The label people follow the character indicates that operation to perform Let me see in the input Yeah, there's not just two letters, but there's there's no digits in it, right? It's always just letters Is it day 14 Day 12 Sure done some cleaning before okay, so case. Okay, so label label Rest It's gonna be Spann off is Alpha on up it like let's make this E and D and then this is up and alpha up and This is gonna be an equal sign as Okay Let's make this aware of thing actually where Okay now here we're gonna say a L hash is equal to Hash and look on Now this is gonna be case as of This is supposed to be CMD Right, we need to import it and it doesn't want us to do right, okay Complaining about the type hash map Takes in a string and returns Map Okay, so we actually keep the label around Okay, and Right, it's a list of strings We split on the Okay, so now we have a label and okay, so if the sign is equals Then we do something if it's minus We do something else. Let's make this into a card case as So if it's say this then we have to do And is read at it Are and then we do something otherwise We just make sure that as is this minus time And that's equal to something Or otherwise Ash map show we show something here CMD, okay So If there's a dash go to the relevant box and remove the lens with a given label if it is present in the box then With any remaining lenses far forward In the box as they can go Without changing The order so I think this What this map is gonna be Is it's actually gonna be a Upd it's gonna be an int map of Of a list of string And we're gonna take in a string and we return it also string comma int And then I'm just gonna Fail wait Didi didi didi didi Okay, if it says equal look if it says dash Go to the relevant box and remove the lens for the given label if it is present within in the box Then move any remaining lenses as far as forward in the box as they can go without changing their order and So then we just we just a What we do here is we do curl map map I am See and is Just so the default case here is going to be Just return curl map Otherwise, then we do nothing Okay, um undefined. Let's just say that here the minus is We're actually gonna look up the the so just E No, I think we could do update Int map Mm-hmm Say source Strict let's see update update the expression I'll set the value at X if it is in the map if as FX is nothing element is deleted Okay, let's just make it. It's okay. We'll just insert it again So just Entries is Curr map I am dot being a Of the L hash Okay, and then What I want to do is I want to say Curr map Okay, this one is just update so update And we're gonna update the updating the L hash Of curl map Okay, so here we have the entries so we have a We have we're gonna say just Adjust does not update. It's just I am not adjust and then we're just gonna go filter Are we're gonna remove all the ones that are Be mapped. Okay, that label filter First Not equal to label dot first And it doesn't like this for some reason Okay, so the I am just as easy. We just filter. Okay. We don't even need this Dash really more box remove as we give a label if it present if the operation character is an equal sign And we follow up by the number beginning the focal length of lens and needs to go into the learning box make sure Use the label markers of the market lens with the label Okay, if there is already a lens in the box with the same label Replace the old lens with a new lens Remove the old lens and put the new lens in its place not moving any other lens in the box. Okay, and And maybe we should we maintain like a set Let's see. So we're gonna do all this One plus the box number of the lens in question So be very nice, right if we didn't have to Loop through all the entries at the same time Let's see, but it's okay. I think I'll replace the old lens with a new lens. Let's just see how fast it goes Put the new lenses place not moving any other lenses in the box Okay, so here we are gonna say Map so So then we're just gonna do we need to curl map. Okay So here we need to say Yeah, okay, if it's not in the map we need to add it right curious care map. I am off If it's if it's nothing Curve map I am L hash So if there's nothing there, then we want to just add the lens of a label comma and easy and it doesn't like this because Then this is gonna be Hmm Curve map I Am not insert Maybe you need to do it the other way around I am not insert I need to purchase by the key also. I am not insert A hash Curve map So I insert it into the map if there's nothing there Just Entries Okay So first of all I won't I'm gonna go I'll just write up up D entries with the Update entries entries and here I need to provide a Label and and So I'm gonna write here up the entries L comma and So if I have the empty list, so if it was not in the list Then we just return We add it to the list, okay, and It is not already a lens in the boggling label, okay, and then up the entries comma and Other label L2 We don't care about the entries for rest Texas else. We don't care about the rest So if L1 is equal to L2, this is gonna be Then it's the same L1 Ln less and we don't touch the rest and If it's if this is not the case Then we don't care what this is or let's just say here, okay Yeah, we don't care what this is. We care what it is It's L and these are not equal And then this is gonna be L In front of the rest of the Entries L1 L less Okay, and so these are the new entries we do Opti entries label comma and and we do I am Adjust L hash So we actually just need to check and If we have the member update remember Yeah, okay, we can do if L hash I am not a member then Else I feel like this should be some I am not up The expression updates about UK if it is in the map it updates Let's see alter The expression alter Alter can use insert delete or update the value in the short look up Okay. Yeah, so I think we just say alter here Okay, I mean this works also so either we adjust or we alter now. Let me see what the print Hash map on the example is so we have a zero Rn1 cm2. Yeah Box one doesn't have anything box three 57 a b5 PC 6 Okay, good The focusing power of a single lens Okay Out of the focusing power of all the lenses, okay, so now we need to We get the elements of the list. So let's see So part one is equal to this right here we also print part one No part two Also string to inch And now we're gonna do I am dot elements We don't and we don't want the elements. Yeah I think I know then we don't get the We want the as ox. All right as ox And then let's just filter out filter Not no thought seconds. We don't want those ones in there So zero Rn1 or cm2 Okay, but it starts at one, right? No, okay box aren't Rn1 box zero. Okay So Now let's add the The ones so let's say To the the sum of the map Focus power to where focus power of So this is the box box I And entries was equal to Let's see So for all the entries we are gonna say tip with And then it's one dot and then Okay, so now okay, I can just map second to the entries, right? Hmm So let's see entries just right in there in there, so it lets you sip with So this is gonna be slot index and Don't care about the actual label. Do we know one plus box number or slot number a focal length, okay? slot number slot index and and the number here and This time so I Okay, and then so it's gonna be one and two and two and then Okay, let's just do box I times si times and here, right? Focus power the paint I think this is the what we want Couldn't match Sip with in the equation. Okay into the actual title list of hints Box I is in this is a list of string comma in yes And then this is and I need to do take this So it says 145 here because It's box I plus one one forty five. I Think this is just what we want And now let's see what it says for the Input 286097 All right, we did day 15 Not bad at all didn't take us too long either That's it for today, I won't be streaming tomorrow because I'm traveling, but I'll stream on Sunday and Or hopefully and then just catch up We can do it. We are gonna make it. Anyway, thanks for tuning in today and yeah, see you on Sunday Okay