 Good afternoon everyone Welcome to today's advent of code Day 18 These have been getting extremely well Very take it taking a long time to do day 17 took seven hours Day 16 we did three hours, but we had to get a hint Now We're doing day 18. I hope it's not seven hours, but we'll see Also shout out to my boy Carlson. Well, not my boy. Anyway, he's competing in the SEC Speed Chest Championship ship this evening Which I want to watch so I hope I'm done. Anyway, let's get started on day 18 the advent of code first of all It's lower than usual. Okay short today. Well, Texas short today What happened yesterday a Is that we got extremely Infused We confused ourselves For the longest time and then What worked as usual Was to just pick back Burn it down start from scratch Module main where main I owe may need for return Let's do the preliminaries Language is you don't want Import you too. Okay. That's how we start now Let's see what we're doing now In the elephant falling elephants finally reach fresh air you merge near the base of a large volcano. Oh, no Locations are my specialty Fortunately the lava seems to be flowing away from you and toward the ocean bits of ours will be injected towards you So you're sheltering in a cavern accident a little longer Outside the gate you can see the lava landing up on then here a lot of easing and solidified Huh obsidian, okay, maybe what you close it might be forming obsidian. Yeah, we call this rap tinda They're like the ravens a flint and The cooling rate should be based on the surface area of the lava drop So you take a quick scan of the droplet as it lies past you you personally put Because of how quickly the lava is moving your can is a very good its resolution is quite low And as a result it approximates the shape of the lava droplet With one x1 cubes in the 3d grid given as it's x y set Positions thing so if you it's okay, so okay Count the number of sides of each cube that are not immediately connected to another cube Dating. Let's get them. I'm gonna cut it slow today Face the input here So if you scan we're two only two, okay, ah, so these are the coordinates of the cubes Kind of our sides of each cube that are not immediately connected to another cube so if you scan we're only two adjacent cubes like One one one and two one two one one Each cube would have a single side covered and five side exposed a total surface area of ten sides Okay And then Okay, let's say Look at this Okay, we have got some collision codes from yesterday, though, that's good Ah, let's see Right input we're not gonna follow with input today. We're just gonna say a File path to ayo And int, int, read input equals fmap, lines, a hard read file, a dating, l2c, let's just write it data, input, int, comma, int, comma, int, okay let me actually int, int, int, now I want to say, c, i2c is equal to pi x equals x, a, i2c, i2, i2c dot reads, instance, read input, where readspreq equals pars, where pars equals a step by a read, so this is going to be read s to p reads, and this is going to be jar comma, that is going to be set by ding ding ding ding do x, y, z, return x, y, z, read s to p, okay we got the chords, read input, okay let's run it, we got the chords, and we can parse the input, good, okay okay now um so 1x1 queues on a grid, okay let me actually type chords, I'm gonna have this as int map, int map int int map int map int net, so this is 3d chords data dot int map import qualified data dot int map import data dot int set import a qualified data dot int set as is, now uh how do we construct so we get a list of coordinates let's construct all the sets a two set two int two int uh cubes a two cubes two set x, y, z equal iam dot singleton x iam dot singleton y yes dot singleton set two cube the cube union equals a union iam dot union width okay so here we have the two and then this one is iam dot union width then is dot union okay now let's uh task one let's take the int comma int comma int and let's review the cube in this way task one equals uh task one equals map two set now i'm gonna do a fold l a cube union and it's gonna start off on the empty queue these are the ones and the twos and then low two and five besides this is gonna be the hard function cube to int now let's say a tiny example equals now let's rent a funny example now i want to say rotate okay and rotate cube to cube so we rotate the cube rotate equals a cube equals okay let's do iam dot sbox okay let's see what happens if we rent we're not rotating anything yet okay let's make this into a list so this is a list of ints and this is a target type currently now i'm gonna go to map x comma y's type repeat x okay so now we have these now i want to say okay and then dot here okay so we go here we go and this is gonna be a x comma okay so why can i not do this maybe because it's not a list yet okay yes and then inside repeat x repeat x okay okay so we covered the ones and twos i'm getting a bit of a deja vu here i feel like we done something like this before but okay a does this work for the example input yeah it seems like it x y z okay we recovered all the now let's rotate okay map a so x y z z x y so let's print a tiny example we print rotate an example and we print rotate rotate tiny example that's so again then then we can see that one of them has two and let's see so there's one two three four and if you rotate it one more time we should get the original bit so here we can see that one of them is joy right pink pink in both sides q equals a count is equal to a im dot i am dot fold l i want to take that current number of sides and for that chord to a v plus im dot size v now i want to do this for the cube use a count and let's say a r cube equal to rotate cube r r cube equal to rotate r cube so we want to do a thumb dot thumb a map let me call a print dollar size tiny example did you give five print a guess what they're saying 10 but for this one there are 25 so it's not just this times two print l t is a cube over read input print cube print rotate cube print rotate rotate but here we can from this we can at least read what is let's just not not do it all of them let's just do there's a 13 in here let's see so one cube has eight sides state q and cubes okay so 13 a cube does not have cube has six faces i am a mathematician don't worry but for the other one that's the gonna be so here we have 10 different sides and 10 different sides and five but there we have only five different sides so what if i do a size of a cube two times a rotate the rc equal to rotate my adjoined i'm adjoined the rc rc but it might not be contiguous right like here we have one two three four six right let's say let's join here let's join okay let's join takes in a set okay because i start from list so it goes whereas so how do we you'll join a group by import a group by compare d two okay x and y are equal if we if x plus x equals y or x plus one s dot a two list s let's see this okay import data i want to say here so okay so one two three four six seven eight eight ten eight twelve thirteen oh okay now group by the import data function okay and then a group i equals on due to correct x y x plus one equals y all right it's just like x x y x plus one equals x plus one equals y i want to merge all those that are continuous okay let's write it in a different way contiguous a so a list of ints to a list list of ints contiguous a empty equals empty contiguous a x y equals okay so i have x plus one equals y if this is the case then a same it's actually we'll do x and what are we doing here okay so here then they here they are contiguous then we'll do um let r r c r s equals a contiguous uh x r c this is not the case contiguous x y r y is equals a x contiguous uh contiguous x equals a ggi day 18 the 1800s okay let me get the l again and then contiguous oh okay so these are the contiguous numbers okay so we're gonna fold it again fold l now here are the insets c a c plus length a contiguous is not from list not is not to list a is a we're getting 32 now we're getting five oh my god nice that's just two times this switch okay let's see if that works for the input task one is int task one equals s hides dot two q print dot task one let's make that tiny example you're also actually new file 3500 does that work all right we got task one and i feel like we did it in a very nice way uh it's very fast 20 30 milliseconds now part two this is where it gets hard the cooling depends on the exterior surface area but your calculation also includes the surface area of air pockets trapped inside the lava droplets instead consider only cube sides that could be reached by the water and stream as the lava droplet trumbles into the pond the steam will expand to reach as much as possible complete displacing any air on the outside of the lava dropout but never expanding diagonally exactly one cube of air is trapped within the lava droplet this is a lot harder let's see oh but i feel like we did task one quite fast let me see the stats you were in the top 15k okay well here i feel like we should compute like the interior cubes and calculate them those areas and then calculate the yeah and deduct that from the total area okay let's uh let's see let's write here a caps caps okay so we're gonna write a i am to a folder um to the current caps and a v a clear current plus plus m okay and then caps prime then and then we will be on the list caps prime then i am to a folder a prime prime but caps prime prime takes in the is caps prime prime takes in int set folder list key c key caps k a k and this is k prime okay so i want to take all these i'm gonna say sip repeat k prime dollar okay and then this is gonna be sip repeat k okay and the cap here is a m s min min is equals int set is dot find min max is equals is dot find max yes and then we're gonna just say filter is dot member max is let's print here a read and put the 18 example caps print dot caps dot cube one two two and five two three is not there and one two four it's not there okay so here we have two so two two two one from list two from list one two three four three two five and three and just differently map and map and so my question is there's something going on here okay a let me just send gaps a x in here x i'm not gonna do this part gaps x equal i am funky y gaps x y gaps x y is x comma y comma one two and then the one two and three and one two and four the one two three is not there four is not there okay and two two three and two four is not so why aren't we getting the two two and the gray show x comma y okay so now we have all the gap all right okay so now i have to somehow filter i have to find a list look so saying like it's completely contained within them not the two two five there for sure now how can i tell that one of these is completely contained in the cube because it might be at like the center right well if it's uh maybe a something is contained let's say neighbors comma int comma int comma int neighbors equal you never expands diagonally so we're gonna do a x y z equal so it's gonna be x plus one a y z x comma y plus one comma z a x comma y comma z plus one then a x minus one y z x comma y minus one comma z x comma y comma z minus one okay now we're gonna write a contains function so we're gonna say contains and we're gonna take a int comma int comma int and we're gonna take a cube and we're gonna say contains x y z is equal to do just y it's it's gonna be c let's flip this cube int int c x y z just y equals a i am c i am dot bang x just just dollar two just z z i am uh wise i am not y and they oh okay i'll do like this just wise just zz a zz a z dot member okay so uh now i'm gonna write this function here okay so task two equals two so this is the like as a task two equals if int equals a where c equals two cube int okay now a g's all g's equal to gaps cube now i'm gonna filter all gs by saying um g okay and now we're gonna say a gs cube equals two cube all gs now filter so g a then i'm gonna say all contains a c all okay and then the g so the gap is within the cube if all of its neighbors are either in the cube or also a gap all and then check a neighbor's key and check here takes in a neighbor and it checks that a contains a cube contains a neighbor or the gaps cube contains gs cube neighbor let's see here a print task two and those are the gaps okay so uh actual gaps equals actual gaps gaps uh we're gonna make a cube out of that actual gap cube equals two cube actual gaps now how many so what is it so we're gonna do the result here is a a sides of c minus sides sides of actual gap cube 58 what i get for the input 2018 uh your answer is too low it's the right answer for someone else okay but we're definitely not far off but it's a bit annoying that we that we get the correct answer for the very annoying actually and the answer was too low which means that we're over counting something i feel like these gaps could be many different cubes let's say i'm gonna check one thing today one second here just gonna move some dress pieces around so just gonna move one around actually i'll do the other one later so we get the gaps okay and let's see here let's do the gaps but only the actual sides a let's see actual gaps we're doing it for the sample okay so these are all the ones that are right next to right next to a in a gap like they're actually right next to one of the cubes this may be it if it's too high yes okay so here we have the gaps so these all contain a neighbor let's see so these are the actual gaps and how many neighbors do they have these are the ones that are entirely contained six times uh maybe it's just like that the length actual gap okay no that was a that was a one I tried that's too high okay between three to eight four is too high and 2018 is too low too low i'm dealing with let's say what is the um gs length of actual gaps so these are any any one of their neighbors is okay now i'm gonna do here and how many other neighbors actually so map g so sum map g a filter any check filter uh check filter check neighbors g you can see here a sides c minus this is even lower right these are not just the ones uh check n equals contain let me see okay so here are the uh these are the neighbors that are completely contained all check see yeah so these are a side c okay we print that out six i'll say a a mono gaps the length and mono gaps let's do a nm nm partition teaching yeah mono gaps okay so there's okay so it's definitely a you're you're definitely contained within the cube on all edges you can like you can never leave without escaping the cube okay so then let's just do let's just brute force this okay a two cube in let's just say a min x equals a minimum x y z x minimum minimum uh oh yeah total map mean i mean said let's just see what these are and we could do this faster max x minimum this is max mom max x what are the ranges here uh one two nineteen zero nineteen zero nineteen oh that's not too bad okay so we have the gaps there's gonna be one of the gaps okay and we're gonna say contained x y z equals um any contains the so and so they have to be and then map any contains c uh we do is for the three and can any contains c and this is gonna be x range is equal to uh x plus i no i'm not gonna do x plus i i'm just gonna do i said so it has to be in both directions okay x range is equal to max x minus min a y range equals a max y minus min y and we'll see range equals max z minus min say we're gonna say here x range y range z range the fully contained where x is a x a x right is equal to a map a i a x plus y i y said zero up to uh no one up to x range x left is equal to map x minus y y comma said one there in a copy and i say x and y oh okay i just do now fully contained is equal to x y z is equal to any contains c contains c right x left z right z left and how to do and of all okay now let's see uh the ones that are fully contained are gonna be a fully fully contained cube equals to cube of filter fully contained all gs side c minus sides fully contained contained fully contained 2024 good to be is it true a okay and x y z x y z range is are correct what did you say when you said 2024 and said something about uh okay 2018 2018 was uh i have to wait for you okay 2024 all points so this is gonna be a x y points i know let's say this is all why said points maybe the gaps function is wrong let's see this should take a while oh okay these are even more points instead we have 17 7600 points we contained a 2299 292 of which are fully contained within the cube okay so these are the ones that are fully contained how many of them have neighbors directly adjacent so uh fully contained points equals filter would be contained x y z okay now for each of them i'm just gonna say a map as some we're gonna take a point we're gonna say um some filter contains c neighbors g so these are uh fully contained points a i can't just sum the links like this because they might have the same neighbor right set dot set set dot from list import data set set qualify to do that set set okay and then set dot union these are fully contained points and now we want to we want to filter we want to filter the ones that are not in the cube as well right uh filter okay and then filter a not contains c okay and then is c minus but we still then we still get 20 24 that was there definitely not the answer okay so wait so these are the full okay these are the fully contained points okay now now let's do the um now let's do the length uh the sum of how many how many sides it's not in each touch g a length filter contains c a neighbors of g 20 29 that seems like a reasonable number let's just let's see what happens 20 29 too low so the exterior sides are let's see what if i do it the other way around okay so i have the fully contained points okay so um um let's write here a fc cube equals two cube well they contain points okay so i'm gonna do this this neighbor filtering what i'm gonna do it on the filter and it's gonna be all x y z but i'm gonna do it on the points that filter g p so this is these are the points which are not contains c e they're not in the cube and they're not contains fc cube b so if i do this right this should also give me 3500 i feel if if my calculations are correct this gives us 3460 maybe i screwed up this okay this gives all the points now i'm gonna remove the ones that are not fully contained it's i do i still get 20 29 which it's pretty weird that um it works for the it works for calculating the total sides also right there's right um this is fully contained now let's just write a the opposite function okay not contained not contained cannot contain not contained x y z the q fully contained points what about the not contained points and then we do a filter not fully contained as a length fully contained points length a imp and length a not contained points and some map length fully contained points imp and not contained so then we have the ones that are fully contained we have the ones that are the length of the input and then we have the ones that are not contained and they seem to sum up to the whole space okay which is good now let's see if we can filter the ones a map so some so not the length a filter any contains c so these are the neighbors that are not contained and let's see that's true i still keep getting this 20 29 number which is not correct we guessed that already okay let me take a quick break i'll be right back okay let me see i got a transition that's where you're knowing that this example doesn't actually so these are the immediate neighbors of the not contained points do i did guess i i did say 20 29 right it came to no i didn't say to the desert okay because in all the cube sides that could be reached by the water and steam as around the drop of stones the steam will expand to reach as much as possible i'm planning to be placing any air on the outside of the lava droplet but never expanding diagonally so i don't get this expanding diagonally in the large example actually one cube of air is trapped within the lava drop so the exterior surface area of the lava drop is 15 so we know that the surface area of the scanned lava droplet is 3500 okay and i guess 20 29 here let me try that again okay i did guess let's just check that ready i don't get it there's some there's something i'm missing here okay so people seem to be doing a search a breath first search i don't get it why so because we just have all the points we just find the ones that are not contained measured x yes i know listen these are the points that are not contained within the ball and these are the fully contained points okay so i know exactly you know i know exactly the area i know exactly the points are not contained right and then i count how many neighbors okay maybe neighbors here should include so how many of those neighbors you know so i i count right so how many of the neighbors of that point and touch the cube right so that should be the face right so if you have one here and then you touch you you you need to count the phase on the outside and not counting anything inside the ball yeah i mean i know that right so i know exactly what is not inside not contained right so then you can go you can go anywhere and it's not within the ball and i know exactly the ones that are fully contained within the ball so this is just checks right that all of them right like if i start anywhere and i go in all directions i hit and those are the ones that are fully contained right yeah the input forms of hollow sphere exactly so a point is fully is in the sphere right it's not like okay anyway you think about it like this right i mean it is a hollow sphere right right just one sphere but it could be like a outcropping right so these are the points that are fully contained within the sphere right so you know you have a point within the sphere that's the definition right of a sphere right like being contained right like if you go in any direction you will hit the sphere and that's what this checks right that a going in all these directions x y x left x right i see right okay you hit the sphere and then i can count the and it even okay yeah right so i'm not contained points that's a i mean that's my problem right so so there's expression here right so that counts the neighbors so how many points a like it touches the touch the sphere from the inside and the outside right and you know as it makes sense if we take if we if you sum these two up right then they result in the total surface area of the sphere okay but then and then these so the first one here should be the exterior and this one should be the interior but i that it's saying it's saying that it's not correct so i don't get the you could technically have a cube that has other cubes in all direction but can still get outside via some curving tunnel then i just okay then we can't do it this way then i need to i need to do a breath first search let's dig that up then did i cleverly put that in util bfs let's put that in util's import data import import data dot int map i am okay need to set import qualify data dot set import data dot set data dot map map import qualify data dot map as map okay it's not just not what we're gonna do like the same let's just copy this let's just move this what is the neighbors function here okay i'm just gonna put this i don't care actually what actually is the graph here uh i don't actually use it's just for the neighbors function so this one to have this be a and start the neighbors function when it takes a is a takes an a and this is something that takes an a and returns on this device so how do i use this bfs need to do do do do do this function not contained okay now i'm not gonna do this for all of them the points i'm gonna consider is a pause neighbors equals a map a and so we're gonna do a filter not contains c the filter not dot contains c of a this is gonna be a p so q points a of neighbors of neighbors okay for the input we're deleting all of the we would like to keep actually we're gonna use the later okay not all that's what i said we're gonna write here a length pause neighbors and let's just do that that from list that that to list can't map pause neighbors and come on in come on in this set but set set that to list there how many possible neighbors are 1631 less than use these cause okay so this works to calculate all of them now i need to filter which of these inside and which of these are outside okay and let's write that function okay so let's say so for this cube 0 0 0 0 is outside right let's just make it or something else so is outside equals and so it's outside if i can get to is just a b okay so and def outside equals definitely outside is minus one common one common one is outside a p is equal to is just bfs nf import data maybe is just is just bsf person and then we start at def outside and we end up at p where nf neighbor function is it takes in a p p see current point curp a we call neighbors of curp p filter not that contains we don't want to don't want to way is outside so this is gonna take a while we're running a lot of worthwhile sectors filter is outside we're outside neighbors equals filter is outside possible neighbors so let's possible neighbors a surface surface is exactly it int comma int comma int surface pass neighbors surface outside neighbors that's a neighbor surface outside neighbors well it's taking long even for the right because it's also like it looks at the it will look at all the points let's have this zero so we'll actually not a def outside 0 0 0 0 now we're gonna find a path it's outside we need to limit our neighbors function here filter in rain we're gonna need that from before the next equals minimum did a lot less than minimum minimum by maximum by in y max y a okay but this is like super dumb right so i want to stop or as soon as i found another neighbor right boss in a set equals a this is just a possible neighbors we're gonna do this the neighbor function is we're just gonna find partition the neighbor set into multiple sets and see if there's a path between them right okay it's quite fast for the example at least all right uh i finally did it so i'll look at these right we were doing like 2019 and then 2024 before 2029 before right so there were just a few tricky cases where there were like some tunnels or weird stuff where it would have like a point and would be okay anyway this is uh yeah this is also based on the hint right breath first search and we could make this faster by making this neighbor's function clear broke more clever anyway uh anyway it wasn't seven hours like yesterday which is i'm actually surprised at how fast it finds that there's no path maybe like for the ones that are stuck but for the ones that are outside it's quite fast and for the ones that are not outside it's probably because the sphere isn't so big right so we hit the wall very soon and then yeah anyway that's it for today thanks for tuning in i would say this was easy i mean the task one was like okay yeah we did it and now yeah it took us two hours to do that we wish we didn't i know i was trying to make my approach work um but it was a wrong approach as it measured x pointed out that was exactly the problem right we might i thought like we would be in the sphere and it would just be a sphere right but not like a tunnel and then there would technically be outside yeah anyway thanks for tuning in see you tomorrow uh day 19 we're almost there we've done quite a lot anyway thanks for tuning in i hope you enjoyed it bye