 all right good morning early end of the code today day 16 we've done all day so far we've done it all in half skull now we're gonna do day 16 we're almost I want to say almost done but we have like nine days left not bad anyway let's get started with today's sensors so we found a sensor right so we fell into the river we fall in a mountain we went to the cave system sand was falling and then we're beacons now we're found where the district the stress signal is coming okay however you don't see any else around said the device is about elephants what mm-hmm what kind of it reports oh no this isn't a cave you just it's a volcano you need to get the elephants out of here quickly the device is meant to get you have 30 minutes so no getting time pressure here and before the volcano rocks so you don't have time to go back out the way you came in you scan the cave for other options carbonate for pipes and pressure release valves you're not sure how such a system got into a volcano you don't have to play your device which is a report your puzzle input of each was flow rate if it were opened in pressure per minute and the tunnels you could use to move between the valves there's even a valve in the room you nail has currently standing in labeled a a yes male will take you one minute to open a single valve and one minute to follow any tunnel from one valve to another what is the most pressure you could release mm-hmm okay let's get cracking how's everybody doing today it's Friday and Christmas season that's how it is a data input okay and now our read input file read input is file path input let's add this to utils okay a line input we don't know okay we define input every time and then let's do it like this read a file path a IO a line I put input line input equals map no F map map read dot lines dot read file not many people chatting today well you're lost anyway a import you tell I need to export it as well import you till read input equals line input equals a V a valve okay and then we have the key a a string let's start at the actual input just to see what we're all capital letters okay but so it's two letters okay B string and a full all right let's see here key all right uh tunnels do we want the yeah let's just have this be other be other inputs okay so valve so it goes to be okay okay now this is what should be string listen strings okay so the keys driving show instant rates preq equals what did I have first preq is equal to do so string also first it's the string valve skip spaces then we parse two characters yet can I repeat okay let me do count a key count to get then skip spaces string has flow rate equal then we get an it a FR read read S to P rates string comm semicolon tunnels lead to valves then a set by one count to jar jar comma return parts V dot dot which is 2021 and record wild card record dot and it doesn't like this count to get ours now this should be ours okay now let's print okay now we do look into trace show and debug our parser I have an empty line at the start okay okay skip spaces has slow rate uh okay now mix it past this tunnels lead to valves skip spaces set by P for instance one or more occurrences of sap separated by sap they're actually separated by the string ffh it's all when it has a one ddi ii baby okay it's not the single value one seems about gg ffh that works then it seems to lose okay so it's just it was that but it was because tunnels okay a string let's see here tunnel optional great thank you for that s okay we managed to parse the example that's always that one right now let's get the input and let's just make sure we can't parse the input we managed to read everything good these are the tunnels okay um no valves open you move to valve dv valves open valve dv okay so i can have 30 minutes okay um for the example okay so let's compute the distance right let's map out the um but i can also move and open and then be closer to somewhere else right this one is a bit hard to take so i have to pick a series of 30 actions and try to maximize the flow rate first of all let's say filter so we're never going to okay so if the flow rate is zero we're never gonna go there okay so let's actually like we're actually we're not gonna open that valve right that would be not good okay um so let's simplify our network uh like by kind of making distance computing the distance between tunnels okay uh how am i gonna do that uh well i'm gonna stick this and i'm gonna sip we're gonna compute the closure always okay and there might be tunnels that aren't even connected okay so uh let's create the map of um as map so we have the map of uh string to string so tunnel okay it was just gonna be the list of uh but string and a distance okay so initial map takes in the list of input and gives us tunnel map initial map material map and the key and then if you don't care about the flow rate here uh let me equal to map string fr and then t's okay initial map uh compare v is the key and then the flow rate and the t's equals and there's gonna be a map dot from list okay let's actually not yeah let's have some map map map map from list a sip t okay that's the first one a map dot singleton k and we have a map dot single single i think it'll be okay we don't have to like care about oh we could like on the path there we could do something but maybe initial map let's say tms frs equals unsip map to pair is gonna be in and then this is gonna be map dot union unions tms map dot unions frs oh example okay and now we have the list of like map and distances and then we have the flow rates of all of them now let's do the update map tunnel map tunnel map update map where okay so we're gonna do lc equals map dot lms there's gonna be a list of map string um no it's gonna be map i don't want the ls i want the i want both i socks okay so initial map initial map now i want to update the map okay so i want to do i don't i don't want to do map with a map dot map map with key a map of option okay okay a okay so what is the update here update a update key and then okay so um first of all i'm gonna take and so a here so a here it's gonna be uh it's gonna be update string to uh to map string okay so uh we have a key okay i don't actually need the key i need the key okay um how are we gonna do this so for all the associations in we're basically gonna update the associations oh my god maybe i'm doing this all along i think i'll just um i'll simplify it a bit tunnels okay so i'm gonna do the tunnels and i'm gonna say that we have distance and then i'll just remove all the ones that don't have uh so well we could like do like you know a and back so i'm trying to find like the path does the this one is hard to do i want to compute a kind of a path um that hits the hits the highest uh amounts of paths of lengths less than 30 that hit the most um that have the highest value right okay but we should compute the distance here update map okay and also map dot so it's gonna be stream there's gonna be the uh tunnels reading from there now so for each of these i'm gonna do here map map with key uh no i actually want to do the dot as out of ts these equals so for each value map dot we're gonna reconcat map f a yes where so f takes in a key and a distance okay we do and we know that this key exists so you just do map dot bang please map dot k so k values um so this is gonna be the values for the key the map for the key there's gonna be k comma d and then k prime d prime d prime comma d prime plus that's actually doing more like this doesn't like this because maps by the react by the type this is one update of the map print a map m map and print dollar map dot as ox okay so this is the initial tunnel map and this is the update map and one round make sure this doesn't filter you don't want the self-reference there yeah this one certainly actually map with key okay so uh initially a leads to b d and i now b leads to c then c now we always want to get the minimum distance right from any tunnel okay if it's already there then filter yes so we don't want to take the ones we've seen before two times okay then we find the fix point can i just uh couldn't be found my internet okay this has called that hack it down i'm just hitting it down a bit like a gti a import data dot function i think we need to define ourselves because this one just takes i mean it doesn't take the initial value okay x map a tunnel map a two tunnel map x map initial map equals a x map now we do where um equals update the map initial just say let's just say a curve map update map curve map case a um equals curve map versus equal to curve map otherwise okay if it's not equal to curve map then we just do a fix map um fix map a can we just fix the map here okay so this will fix the map and this is now it's like the distance from a to everywhere else okay now let's simplify this map by essentially removing everywhere the flow rate is zero okay um except we still start at a okay um okay let's simplify so flow simplify and now we always want to start at a uh wait okay uh we also want to start at the initial one initial look equals um package still offline oh package dot map we're not this is an in-map but it's gonna be the same and okay let's just do this as simple okay just just i don't want to do too much map dot as ox tm as ox as ox equals a okay then a so here we have all the as ox now um let's say interesting non zero equals a filter a not is zero not first of as ox let's actually just do that is zero k equals k not is let's just say it's okay so it's okay key equals initial look mutual look so equals true is okay k k equals so we're gonna do f r f r map dot bang k uh largely equal to zero and there's gonna be a that's all the as ox okay then we have to map over the as ox okay uh map dot from list e comma so this is the like the else of the else of the map is gonna be key comma filter now these are the f s t is okay dot f s t so we have to play it to both kind of here filter is okay f s t map dot as ox okay map from list and this is on as ox print the simplify so start with a a here that's the initial key is actually gonna be a v dot k uh so it's gonna be a simplified key f r m f x this should be star larger than zero okay so we have the initial one and then we only have the ones that actually have flow rates okay and then we have the distance is okay prime key only checks the f r map dot bang key so we get the initial key and then we just remove the initial key from the rest as we're in america now have to go back to the origin um okay now uh we have to create um let's see task one input task one uh v so imps uh to v equals uh t m comma f r m equals initial maps equals fix map and simple equals uh simplify the dot key f r m fixed and this is gonna be then as simple let's do then you're gonna print the as ox of okay uh what does it say for the input here okay but we can see for the input that there's actually very few that have uh that have um anything now we start at uh r t use revolve a okay i still feel like there should be someone go oh okay a a is there a a always starts there and i just have to select the next one what if we do uh not just the list here okay so now let's say here okay so let's see um so let's go okay let's just start here okay so let's just do a calc order okay now there's gonna be the ones we've had very very busy so far current time current time okay because we want to know how many how much we have left curl oak okay and let's start okay where neighbors nay nay ends equals a simple map dot bang okay these are the uh but these will always be all of them these will be all of the ones that have okay but that doesn't matter these will be the ones that have something okay a then let's a but we're gonna filter filter uh so we're gonna filter it so that uh so we have some minutes left right so we can't make it to the end but it's not always best to select okay we filter um okay so so we have to consider like the value gained so first we're in this list okay key so we're gonna take the key there's gonna be a map there's an extra parenthesis somewhere okay so now we have to multiply it by let's say let's see here the frs do multiply by the current time minus no so minus the how far it takes us where ns equals okay so we're gonna take the neighbors and we're gonna take the top bang so e frs map dot bang curl oak okay we are going to filter a we're gonna filter the ones that we're gonna filter the keys that are in the visit key list dot number okay a let's just say here visit it empty 30 okay let's actually let's just maintain the list let's just make it into a set every time reset it's not gonna be that big we just have to go set that from the list so first we map and print as a okay so these are the possible options now the gain so the gain by for each is a map dot map and the gain so it's gonna be a distance comma low rate we're gonna turn this into gain by saying distance comma low rate times current time minus one to turn it on minus a d so this is the gain from each so i think maybe we can't be too greedy let's see okay so the greedy one doesn't work here because they would so just in the example i'm sorry consider this approach this approach was yours it's a much pressure okay so at a flow rate of yeah so i'll calculate the flow rate correctly then you could spend your third minute moving to val cc and your fourth minute opening it providing an additional 26 minutes of actual pressure release so essentially but here there's just a lot of combinations so we can't just like try different combinations it would be so good to separate the two oh wow this is already taking quite a while it seems let me give you see here um like this current time x is k is a max time equals comma keys equals so then we do we have to look up and vf for us and and this is let's see clclkks equals equals and then k kp equals a do we look up the key value and we look up the of the key d comma fr is going to be fr times per time minus one minus d calc total the current level would be k now let's not count the order here let me do total what is the path i go d d d cc bb this is a third time is 30 current look is a a and then the order they go in is d d d cc d d cc ah no jj doesn't do anything bb jj jj hh so dd bb jj ah with cc was there right oh wow we don't even open valve cc it's uh let's just see what this one does 16 39 so 1654 ah cc is in there the number of combinations of keys this thing is so if i look at the input right like if i select something that's super far away like 15 right it's a it's going to limit the number of choices i can make yeah it's a hard one we might have to because i have a i have an hour left we might have to pause and continue tomorrow and because i won't have time tonight okay anyway a pass call all permutations of list okay let me just see how many number of permutations are all right okay um so b frs gonna be calcorder s list dot permutation snap dot keys v frs map dot bang e how many permutations are there there's only 720 permutations for the example for the input however takes a while tell a number of objects i'm taking those unobjects one two three four five thirteen fourteen fifteen there's a lot of permutations okay so i cannot brute forces okay let me actually take a quick break i'm just gonna go to the bottom and then i'll be right back i'm gonna run some ads i can click this button here okay sorry about that i'm just gonna head out and i'll be right back right bye enemy i have to turn this off it's okay ad does not work we can't even like we're not even doing the computation we're just checking how many permutations there are um but for calc total we calculated it correctly i don't understand why why is jj is it because dd why is it not better to go to jj first is it because the difference is let's see there's gonna be ns a okay so let's just curl up a a ds ox okay and so we have the most amount of gain if we go to jj from this point i'm just gonna see what happens maybe it turns out to be the same okay then let's go a set dot map dot map dot as ox for this we'll just do a greedy equals and max maximum um list dot maximum by compare on compare on a key we don't care about the key we don't care about the distance we only care about the game or the data not function on compare on variable odds and scope but i just imported it uh but i imported it into you too okay uh compare on to the greedy okay the greedy is going to be the key d and fr we don't care about the fr uh so then we're gonna be saying calc order okay uh visit it so we're gonna add the key there visit it curl time is gonna be curl time minus d minus one curl look is k and s is supposed to be a list of string comma and combined okay so we're gonna say okay visit it and max has the type what's wrong here oh my god in a question for task calc order this is string okay and the integer and string and returns a list of string okay calc order visited so there's time to come to a just come to conditions here okay so case a ns empty which returned reverse visited that dot from okay um okay uh that's one way to end but also curl time about the look i was playing about a v set uh maybe i can do a map instead of map dot with keys without the total of this it's not as much right so the greedy algorithm doesn't work interesting it's always trying to find the best one right away because i want to contribute more you know somehow computed in reverse uh because like we have to go there and then we have to oh we don't have to go back didi bbjj didi jj bb jj easy so i think that's one of what i'm so that one i'm saying like go there and then come back that's uh inefficient so because it doesn't work as like a breath first search right because it's like a didi bbjj hey jj ec this will not work okay this is like a scheduling problem maximum so go to ii in the jj right so there you have to quote back and forth right how could i how can i print this graph in a nice way in that so you see right so you go from a a then you have to so to get to jj you have to go from a a to ii to jj and then you have to go all the way back right and so i feel like we can kind of could kind of like wrap you know kind of compute backwards right so we kind of have the we could like make like a tree right and then we can kind of compute the value of each node in the tree somehow because it won't be because it will be like you know a a jj and this is a so a didi so this can be collapsed into just the then bb goes to that's actually not do it like this a i i wonder if it's always a tree i don't think so bb uh bb and then bb goes to bc like this this is this okay okay and then go didi goes ff oh no didi goes and e which goes to ff and ff goes to gg and gg goes to hh affects the sign is my passion so i would like to collapse this right so because bb and cc they both have a value right so i don't need to so i can write from here from the limitations hh ee and cc didi jj okay so this one is telling us to go you know from a to didi okay and then from didi to jj and then for jj to bb and then for bb to hh yeah so this doesn't make sense right if we just can i i'm gonna turn on the plus one always so i would like to identify in this graph like so instead of having jj i would just like to have ii but with distance two that's to say that visiting ii with distance two and then the other one is to go but it's not even like that because you see like they will the solution they turn on hh so go first different on hh so they do go didi and then bb and then jj i make a choice and then i have a list of all the possible choices and i make a choice from those now how can i kind of estimate before i compute what i have essentially done is adding a distance everywhere right so ii doesn't have anything in it yes and i've collapsed fng which is a distance three um it doesn't have anything fng there's still a bunch of notes right maybe the tones are all like this it's probably not in the order yeah so the greedy choice is not because what did i guess 1732 let me just guess that again what did it say maybe we'll have to look on the you know okay uh two days the first day i'm completely bankrupt because i had the idea to get ready to uh is it at each valve turn what valve should we turn next yeah more than zero fluid there's not a proper presence on problem fortunately most of these permutations would take more than 30 minutes to walk if you find a way to only look at permutation shorter than that you have a reasonable size set of possible strategies to calculate okay that's the hint that we're not gonna take all the permutations we're gonna select the sets that some okay so possible paths let's just call it that possible paths per look okay possible paths visited per time per look okay we already have a lot of it this year okay and this is going to be the possible paths map a map without keys we remove the visited let me do map dot filter width so let's just we filter a d and we don't care about the flow rate we filter the ones that current time minus d is less than zero less than or equal to zero map dot a map dot keys okay so these are the possible keys this is going to be just strings now no selection here uh maybe i uh sort by actually sort by a take the keys here we take the associations and then we take this list out sort by okay now this is going to be list of strings okay so these are the map okay let's see no this is not work like this okay so we have the current look and we have the next ones okay so we just do a map current look because our next year is going to be ns so next the next a given that we're visiting the next key key prime and d prime and whatever is equal to calc order reverse visited and a calc order uh and we do k prime here we add that the current time will be current time minus d minus one minus d prime minus one and curl okay is k prime next takes a string in comma int okay and then okay so this one will return a list of strings and map next this is going to be mx next mx wait so this is that takes a list of uh i don't get this couldn't match type chart we actually saw a string expect okay conkat map a curl look we'll conkat map next okay and then next is good mapping okay and for the example what do we do then it doesn't return anything because eventually we'll just do nothing so let's see case mx a mx of so if mx is empty we return reverse visited uh this is supposed to be these are all the paths okay there are a lot fewer permutations for nice ones okay maximum by list of maximum by compare on let's see how long this takes one second all right okay great so we had almost all of them and we were already filtering but like it wasn't enough to do greedy selection we had to do and we had to do it a bit harder okay now let's hope task two is not too bad okay what if you got one of the elements to help you it would take you four minutes so you teach an elephant to open the right 12 and right already on 26 minutes actually depend would having two of you working together be better even if it means having less time okay um no let's first of all contrast but yeah i have to go uh i'm gonna give myself another 15 minutes i have no idea how to do this in 15 minutes but we're gonna i just want to see the uh length here okay so linda are there are so there's 26 minutes this is even harder okay okay the simplifications are still valid but now we have to compute the path any different okay and i mean that the time steps are not the same right so okay let's just start i don't use max time here uh so the visited set would be okay but now we're gonna have a set of strengths but now we're gonna have the set okay now this is gonna be me comma elephant this is gonna be a me comma elephant me comma elephant okay so the visitor set so we it's not gonna help if we are both visiting the same spot so that's okay so we're gonna say a me comma l me l okay we're gonna have the my time l time and we're gonna have my look l look um okay now this is gonna be so this starts off with that dot empty this is the current time so the set of the set dot empty it starts off with two empty strings it starts up with both of us having 26 minutes and it starts off with a pair starting positions now okay so if i have no options left then i will return okay so first let's choose um let's choose for whoever has more time left right okay okay let's see bigger time equals max might well let's have this actually not be let's have this be something we can index okay me comma l l time so ind okay this is a if my time bigger than equal to l time else one okay so this is so now we're gonna select the okay now then we're gonna select ones where times of ind bloodier than zero and we remove the verse v sets and we do select the end and then we don't need the v set we just keep that always there now i've selected someone so how i selected the one to pick then we are going to do it so the next one is gonna be calc order so it's gonna be v set set insert we're gonna insert to the key prime into the v set that's gonna be the next one for this one um what do we do before here tindu tindu tindu tindu celebratory mean is the prime minus one okay so you see a curve so that's gonna be the times end minus the prime minus one set the right it's like a zero list dot list insert okay Okay, import, data, import, qualified, I am, data.int, I am, so we're not, we don't have it like this, and then we have the ads, we have a time, so we have looks, okay, and then a look at data.intmap, the maximum key is also the maximum, look up min, find min, this is all over the key, from list, same here, another int, okay, we just do here, my time, because timemap.assox, this is not map, this is I am.assox, okay, and then int is equal to if my time larger than l, time, then 0, l is 1, I am.bang, .bang, okay, and then next, so we insert the calcorder into here, the time prime, so we do a I am.update, modify, adjust, okay, adjust a of the int, we modify the time this way, the current look, right, so this is a, okay, and we have to modify the path also, path, okay, this is gonna be paths, and then I am.modify, I don't care what it was, we're gonna put that to k prime, supposed to be adjust, a key, and then this is gonna be the int times, now I just need to figure out how to update the paths, I am.adjust, so what the path was before, I just want to k prime, that's how I adjust the path, okay, so this is how we adjust, we select it for one of them, and then we select all the selections, now after all of them have been selected, and it's damn, I'm almost there, okay, so this is just gonna be a list, string, comma, string, and this is gonna be, and it doesn't matter which one it is, a map, reverse, dollar, I am, dot, lm, so it's gonna be I am, dot, so paths, I am, dot, bang, zero, reverse, and I am, dot, bang, I should all tight check now, no, 159, parse error, there's a problem here, this seems to work, can't get it, is it somewhere in task 2, okay, is it somewhere here, good, oh, this one should be, okay, right, which one was it, was this one, I had that, so it's only one here, the, so paths is a map of, okay, this one should be also, so paths is supposed to be, because I'm just adjusting, so it's going to be, expected list of strings, okay, prime here is a string, so this should actually be an input of list of strings, and then this is all correct, but now it's actually a list of, yes, and then this is not the string, it will be a list of, what's confusing everything, undefined, wait, it's gonna be a lot of paths, I think, oh shit, it's not even, I don't think it's even running for the input, it's running for the, okay, let me just calculate the, the total, okay, so count total, we're not gonna do the length here, okay, and then we're gonna say, paths is equal to maximum, list.maximum by compare on pcomp, okay, and the pcomp is of p1, p2 is equal to calc total, 26 aap1 plus calc total 26 aap2, okay, list maximum by compare on paths, and then we're gonna see pcomp of maxpac, so 1700 and 7 is the result for the example, okay, let's see what it is for the input, this will take a while, all right, but I really should get going, let's see what it is, it would be really nice if we could avoid doing part 2 today, but it has been an extra extra long session, yeah, how long did it run before I cancelled it before, oh, we need 40 seconds, all right, I'm gonna go for a bit, just pop off again to the bathroom, and I'll leave this running, and then hopefully it finishes within two, three minutes, we'll see, I'm gonna go do a bit of work, I'll let it run here, I'm gonna do my laptop, hopefully it works out, but it's been a couple of minutes already, right, so I feel like this mic will not work out, so what can we do here to speed it up, let me see, we're not doing too bad on memory use at least, that's something for five minutes so far, okay, let's let it, I'll leave for like 10-15 minutes and keep it running, let's see what happens, all right, we have our assault, 2556, let's see if it works, all right, we got it, okay, this is an A version, slow, but you know, I think it's also like when you're at it for three hours, it just becomes too much, but yeah, sorry about taking a long time, I'll probably shorten this section of the video on YouTube, anyway, thanks for tuning in, and I'll be back tomorrow, maybe at 10, maybe at 2, European time, and so probably at like yeah, like 2, so just check the check the Twitter, maybe early, it seems depends on how hard I go for Christmas party, so follow my Twitter, stay informed, okay, probably solved it, now you've left, but we did it, all right, thanks again, bye-bye.