Advent of code 2021 in Python - Day 14 part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the end event of code 2021 day 14. today we are giving a starting polymer which is a sequence of uppercase letters of size 4 in this case but there is no specific sides and a list of rules every rule starts with two letters and ends with another one so associate a couple of letters to a single one what we have to do is essentially to expand the polymer according to these rules so for example here there is an n we find the rule that associates with n n this one and then goes to c so the polymer will become n c n in this point then we do the same for n and c we find the rule and c where is it here nc becomes b so instead of having nc we'll have n bc essentially every rule is putting something inside the polymer between two letters after we do we end up with a longer polymer with more letters inside every two letters there is an accelerator added by this step and we can iterate over the next step so we again compare the polymer in which we add the letter with the rules and we add new letters and every step we have we had more and more letters we insert every time into a pair here you can see an example and the problem is asking us to expand the polymer for 10 steps and calculate what is the most and least common element and the difference between the two counts is the solution for the part one so i start with the with the copy of the sample problem so that you can try my solution on a known input see the result and then try it on the real input so i already copied on the day 14. text the the sample input the one that we have in this information solution and now we have to implement so first let's parse the file so i iterate over the lines and i have two cases i have one line that contains the polymer and other lines that contain the rule so let's start with the polymer so create a variable called polymer initially empty i don't i can also create it in the form but i prefer to have it a single time and here i say that if the line sorry if the line doesn't contain this symbol which is the one that defines the the expansion rules and not now let's start with the with the reactions so the reactions will be uh dictionary because it goes from a couple of vectors to the one to insert inside so i say that if this thing is in the line so if the line contains this arrow sign which means is a reaction then i say i will call it couple then nothing then the result the insertion let's call it is equal to line dot strip because i want to ignore the new line at the end and every space dot partition and what time what am i partitioning on on the arrow sign plus the two spaces that are on the right and left notice that the two spaces are not removed by three by strip because we're not at the end of the string so they're kept only the ones at the end of the beginning string and now that they have the couple which is the first part of the string and the insertion which is the last part after this arrow sign that this underscore is just to ignore the the second value returned by partition which is the separator itself we don't care about it i add it to the reactions so reactions of couple is equal to insertion then i say that okay if this line doesn't contain this arrow sign could be an empty line like line two that i ignore or as something so i say that the length of line is greater than one so it's greater than just the new line character then the polymer is equal to line dot strip again i ate white spaces so just remove them every time i can so that i have a proper input now to be sure let's print the polymer and also print the reaction so they can see that my problem is actually working so at least the parsing is working okay so the polymer is n and cb which is correct is this one and reactions also seems fine okay then analysis because it's trying to invoke part one and part two that are of course not defined yet i still have to solve the problem okay so let's start by creating a function that calculates a single step so i pass to part one the polymer and the reactions and part one we go for 10 steps because the problem says that we want to simulate 10 steps so i say that four in range 11 because you know that range actually we don't care uh one eleven i want to count like a human so i i start from one so one range one eleven will give me the number between one and ten i want to um extend the polymer so i say that part one receiving polymer reactions we calculate a new polymer so polymer is equal to [Music] reaction step let's call it we it's a function we're going to create that will receive the original polymer and the reactions and then we print this step so let's call it idx i will print this step and the polymer so i simply want to print what is happening at every step now i have to define reaction step which is where the actual reaction happens so reaction step will receive the polymer and the reactions hey what a what it has to do is to go through all the couples so all the letters that are one after the other in the original template how do we do that well it's simple we let's call it imam b we iterate over polymer sorry we iterate over the zip of polymer and polymer ignoring the first element so this is a simple and very common way in python to iterate over consecutive elements another thing you can do is just to have a four on a range that goes from zero to the length of polymer and every time take the element at that position and the position plus one this simply takes the polymer shifts shifts it's by one and with zip it gets gives me a tuple that contains an element and the next one in the polymer so now i have a and b that are two consecutive letters in a polymer and i want to get the reaction between them so i get the insertion which is equal to what so what we have in reactions corresponding to these two letters so reactions in which position at the position a plus b a and b are strings a plus b means just a concatenation so i am getting for example n here and c here and concatenate them to get nc even in this dictionary i will find the key and c for example here and c will give me b so in session it's the new letter that i have to insert between a and b what i do with this i create a list of insertions initially empty actually this is a string so i i can treat it as a list of a string it doesn't matter insertions dot append in session can we simplify this code a bit yes because first of all this variable is created only to be used here so we can just copy paste this thing into here and every single line and then you can notice that this is just a list compression insertion is completely fine by this so also this is a string and here i'm doing append so it makes no sense let's say the insertion is just a list that contains this thing for amb in polymer blubber so in simply transforming this stuff into uh this compression because there is no there is no reason to to have an explicit four it's very simple i do it only because it's a very simple operation if it was more complex i would prefer four just because it's more readable and why do stuff in a complex way when you do it in a similar way okay so we found the insertions which is essentially the list of all the letters that we have to put inside this um beside this polymer now we have to interpolate so essentially to get one letter from the original call it from the insertion wallet from the original letter from the ascension until we find the complete list of letters how we do again the same trick in fact we can just copy paste brutally swing here we we interpret the polymer and insertions so let's call it o for original and i for insertion just for clarity and let's call it result the result will be a string so i simply say okay result plus equal o plus i notice that polymerization will have a different length why because insertions are put in the middle of the polymer so if the polymers has four letter in session will be three i mean look at the fingers of your hand if you have five fingers there are four spaces between the fingers so there is always one letter less in the insertion than polymer so this dip will actually stop and leave out a single um letter from the polymer because the polymer is one longer for example ncb will have three letters that are between n and n between and c between c and b so three letters but there are four letters in ncb and that's just a result of the problem so we have to add the last letter the b in this case like this result plus equal the last letter of polymer and we return the result we can also just return the result plus this thing remove this line it's the same okay so now we can in theory run part one and see what happens if we run the problem we can see how the polymer evolves okay so it becomes very large very soon which is expected because at every step we double the size of the polymer so in 10 steps it will reach 120 1024 plus multiplied by the size original side and also i have to print the original polymer well we know it because it's already improved so step one we will be ncnbchb and cnbchb step 2 will be this thing i already don't want to compare but it seems correct and b blah blah c b and b blcb okay it seems correct what is the problem asking it says that okay step one it seems correct so we are matching correctly the steps and the length what is the problem asking is asking us what do you get if you take the quantity to the most common element and separate the quantity of the least common element this is trivial because um here after the whole thing is processed we can just create a counter so i use my favorite python uh module which is from which is collections from collections import counter i wrote for form okay counter is a very nice module because it gives me title account so let's say c equal counter of polymer this will count how many others are there in fact i can also print it just to be sure so i run again see what happens the counter is telling me okay b appears this many times and appears many times and here it says okay b appears one seven four nine times which is what we get and h appears uh one six one times which is correct the problem is asking us to get the most common and the least common the most common actually is a function already in the [Music] in the counter most common which gives me a list [Music] okay so most common as a parameter which most common essentially you can see from the documentation gives me at least a sorted list of the elements that are in order of frequency so i can ask it for some of the three most common elements but if i pass none i get all the elements which is exactly what i want so essentially i get a ranking of the frequency so let's call it the frequency ranking equal this thing i don't like to have this c variable this basically in every user so i simply create here the counter just for the sake of this so create a counter and get a com account the most common and and now i can just take the last the first and last element because they will be the most and less least common let's also print just to give an idea i can ignore this print freak ranking my printful and here it is so i get this list that is telling me okay b appears this many times h appears many times the problem is asking us to get the difference between this and this value which is just the difference between the last um the first element of freak ranking which is this tuple b1749 but i get also the number i don't care about the tuple but only about the value minus the same thing but at the last position so minus one this is the solution one five eight eight which is here the actual solution so i can remove i can comment out this part i can also comment out what was it there was another print somewhere yes i don't care anymore i know that the parsing works it is a solution so now i try it with my real input if it opens no no it's not working okay it's working it's quite longer which is expected i ran it two seven four five so this will be the solution to seven four five yes see you for part two thank you for watching bye
Info
Channel: codiamo
Views: 16
Rating: undefined out of 5
Keywords:
Id: toSblzXLQew
Channel Id: undefined
Length: 17min 0sec (1020 seconds)
Published: Mon Dec 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.