Advent of Code 2021 - Day 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

If you post a video link, please also take a moment to write here what it is, and why someone might want to watch it.

👍︎︎ 4 👤︎︎ u/AndydeCleyre 📅︎︎ Dec 01 2021 🗫︎ replies
Captions
hello explorers and welcome to another video it's that time again it's the best time of the year it's advent of code every day during december until the 25th we will get two new puzzles every day to solve let's start off with day number one so let's jump over here to my screen and read what today entails sonar sweep you're minding your own business on a ship at sea when overbought alarm goes off and you rush to see if you can help apparently one of the elves tripped and accidentally sentenced slay keys flying all into the ocean before you know it you're inside the submarine and the elves keep ready for situation the elves keep ready for situation like this it's covered in christmas lights because of course it is and it's even an experimental antenna that should be able to track the keys if you can boost its signal strength high enough there's a little meter that indicates the intended signal strength by displaying 0 to 50 stars your instinct tells you that in order to save christmas you'll need to get all 50 stars by december 25th collect stores by solving puzzles two puzzles will be made available each day in the advent calendar and the second puzzle is unlocked when you complete the first each puzzle grants one store so good luck as the submarine drops below the surface of the ocean it automatically performs a sonar sweep of the nearby seafloor a small screen the sauna sweep report your puzzle input appears each line is a measurement of the sea floor depth as a sweep locks further and further away from the submarine for example supposed to have the following report and then they give us a bunch of numbers here this report indicates that scanning outwards uh from the submarine the sonar sweep found the depth of 199 200 208 210 and so on the first order of business is to figure out how quickly the depth increases just so you know what you're dealing with you never know if the keys will get carried into deeper water by the ocean current or fish or something to do this count the number of times a depth measurement increases from the previous measurements there's no measurements before the first measurements in the example above the changes are as follows and then the first no previous measurement then increase increase increase decrease increase increase increase decrease increase which means that in this example there are seven measurements that are larger than the previous measurement how many measurements are larger than the previous measurement and then we have our possum input so this first day is usually much simpler than other days so they start off simple and then there will be more complex of course but let's jump over here i have started up by downloading the inputs and also just check that the actual compiler works i will run jdk 17 for these exercises but it doesn't really um matter what you want to run with if you want to follow along run your own um but i'm gonna try 17 for this maybe use some 17 functions if i get the chance i don't really know so let's see here uh first off i have this puzzle input here if i want i should be able to make this kind of a thing is that a thing now string data could that be a thing do we have that yet or is it just two that we need no i thought it was three so what's the problem here legal text blocks starting and um so that's the thing now we can have text blocks oh much fun yes to write so here i would like to take the data so let's do a string s form data split by newline let's do that and just output that see if that works and print out s let's see what we get and if we get what i expect yes we got all those lines we don't need the first line here that's not important and it seems like we got an extra line down here so the textbook could actually end with um these characters but they could not start with them or it doesn't really matter we got the same result here okay so now we have the numbers so uh if s is blank then uh continue so we will skip any extra lines will we uh miss the last one yeah probably we didn't get one down there anyway and then i want to get each number so integer um i just hint i because we know that they are integer numbers or do we know that we can look at the inputs here for this day it seems like they are all in integer numbers so let's just make it easy for ourselves so let's take integer parseint and this string and then i want to have a int count so how many times uh count decrease and then into last num let's set that minus one so here if last num is equal to minus one then lost num is equal to i else if lost num is larger or let's say if this new number is larger than last num then we will set last num to the new number and take our account decree increase right increase so how many times it increases so we will count that and then we will have a count increase and print that out so if everything is correct here we would get seven but if we get five here so why do we get five so last number is 1 then we will set it to something else so let's look at this did i do anything wrong here so here we have it so the first number is 199 and it's not liquid length so we set it that number and then jump up here we get 200 and it's larger so we will increase next we will get 208 it's larger so we will increase and then we will get uh 208 which is not yeah it's larger yeah 210 we got and then we get 200 which is not larger so we jump over that one 207 which is oh so the last num is something that we set every time okay that changes things uh yeah so if it's then we just need to do this kind of thing and here we continue so in the first case where we don't have any number we just set the number and continue which makes it run one more time otherwise we will see if it's larger than we want to increase it otherwise we will just set the previous number so let's see if we got the right number now yes now we get seven so now we know that our function works with a test data so now we only need to write something that can give us our actual data so let's do a buffered reader br equals to a new buffered reader and it will [Music] have a new file reader which will take the input slash input day one txt and i believe there is newer fancier way to do this uh but i just don't know them so that's something that i need to learn as well so i will get improvement in my coding style here but let's just start with something that i know and if you have any suggestions you know where to put them down in the comment section down below so we learn together here br readline and i usually do this like string line equals to [Music] yeah not set that one and then i do a while loop if line equals to be our line is not equal to null then continue and then i can move these things up like that and then we just replace s will line here so that's pretty much the only thing i need to do and i can run this example and we should not get this kind of issue so we have input day one a yes good thing that we catch the error here so what i did here was i put it into try catch where i print it stack trace down here you could also throw the exception right of the function so 1521. could that be the right one we validated that the function did the right thing with our test data but will it do the correct thing with the actual input data let's try it and yes you get a gold star i get a gold star everybody gets a gold star that was the right solution so we are off to a great start and there's a part two considering every single measurement isn't as useful as you expected there is just too much noise in the data instead consider the sums of three measurements in a sliding window again consider the above example so we have aaa bbb ccc ddd starting by comparing the the two and the first and the second three measurements uh windows the measurements in the first window are marked a 99 200 and 208 their sums are 606 the second window is 200 208 210 its sum is 16 18. the sum of the measurements in the second window is larger than the sum in the first so this comparison increased your goal is now to count the number of times the sum of measurements in the sliding window increases from the previous sum so compare a with b then compare b with c and c with d okay and so on stop there when there aren't any enough measurements left to create a new mesh three measurements some in the above example the sum of each three measurements window is as follows so [Music] we have here that we have an increase no change decrease increase increase increased so there are five sums that are larger than the previous sums so that was the example that we got if we did our wrong calculation consider the sums of a three measurement sliding window how many sums are larger than the previous sum okay so um in this example i think that what we want to do is have a start value so let's say that we want to read two values before we actually do anything so um and after we have done that we can add the third value and that should be our sum and then we need to compare that with the last value um so in start count two so if let's see here uh if start count is larger larger than zero then we take start count and do a minus of that and we will increase lost num with i um will this work because we need to remove the previous value as well so that will not work probably um so last number current equal to we add to current so we will have end current as well and [Music] if start count is less than two then [Music] we add the current [Music] so now we have done the start operation here if we run this again if we comment this one out and then do a for loop of string as of data split over lines like that and we can call this line again so we don't have to swap back and forth um it did that we should be able to test this and see what values we have in our current and our last num so the last num of these two are the addition of two values um no we need to have a third value as well so let's do that [Music] let's run this oh not run this we need to calculate so now we are down here so now we have added three values the last number 606 right um oh that will not work ah that no will not work uh let's do this as small arrays instead um three values and three values new new [Music] let's see here so current zero or will it work no um so current let's do start count less than three start count larger than zero and then start count minus one and last num is stock count so now we should have a race instead here down here so what i'm trying to accomplish here is to save all the values that will be written so i will have the last two values in one array and the last three values in one array so now we see that we have in the last num 99 200 and 208 we will have 200 and 208 in the current um so that's great here we can take current and the second value and put i into that now we can do a calculation of these three um let's do this simple and don't want to make this a hassle another language would probably have something much easier to work with um but i'm just gonna make a little bit of a for loop here last numbers current numbers to make it easy last numbers current numbers there we go and current just [Music] lost and then we do lost equal to last numbers j add that one and we do the same for the current value current numbers now we can have this comparison if the current number is larger than last number we'll print that out and now we need to do a switch row so we will have another one of these for loops what we will want to do here is the last number should be equal to the current number j plus one and we want to do only two steps and the current number of the words we want to be the same current number plus one [Music] and no we want current numbers to be lost numbers right and we want to switch so here we want to do an array erase copy of these so we don't [Music] what new length okay three so that should make a new copy of this with a length of three um and then we just switch this up and hopefully the current numbers will be added up there again so let's try this out we will start over here so let's see we go in here we have the two arrays that we expect we get the next number we go through and calculate these large values so we have two 607 618 i believe those were the right numbers right 200 618 and 607 years and then we will increase the count we go down here we will copy last numbers so you will get all the values of the other one and then we will switch this one out so we will move the numbers around we'll fetch a new number put that into the last place there and we will count these again and if we go down here we should have lost number 618 this number 618 and that's no change so it seems like we have the right algorithm now and if we get 5 as a result looks very promising and if we go here again we will comment out the other lines we go from the test data to the real data and run it here we will get 1543 let's go here and in and enter that value let's hope it's the right value let's see yes you get a gold star i get a gold star everybody gets a gold star we sold day one so let's go back and see what we did we first off wanted to test everything out so we have this test data and now with the fancy new functions that we have in 17 we're able to use a string [Music] multi-line string thing so we have all those values there and then we had a buffered reader to read the other numbers we had something where we counted all the values we wanted we had two windows one window with the last numbers and one with the window with the current numbers and in order to fill them up with the first numbers we had a little start count we got things rolling and we have this little thing to read line by line and here we will read one line until we get null back that means that there we are at the end of the line end of the file we will skip every blank line if there is any we will interpret each line as an integer and then if we have some startup to do we will fill our little account here and if it's not the last value that we want to put in so the values two one and zero um then we or we if it's not the uh let's see if it's not the first value because that value we don't want so if start count is larger than zero so we skip the first value but if it's larger than zero then we will pick that value and put it into the zeroth position or these one position in our array so the first two values will go into the current numbers and then we will just fill up the last numbers so first one and second and third value will go into last numbers and we will increase our start count so this little function will fill up the multiple arrays with the first one the last numbers three numbers and the second current numbers will only have two values the last two values then we will put the last number that we want in the sequence so the fourth number if we just start from the beginning the fourth number will go in position two in the array so zero one two the second position in the current numbers array so now we have all numbers we have the previous three numbers and this will accept the last number and the previous three numbers except the first number in the sequence and then we will go through here and add them together current and lost so we will get this summation of these two arrays and then we will run a little if statement here and increase this count i guess this could have been a small function of let's say a reduce but it's so small numbers i think this was a faster way to do it then i would copy the full current numbers over because we want a new last numbers which is all the three numbers we have seen last and then i want to switch over which numbers are in the current numbers we will get an empty place where we can put the next number into except we will not empty that place it doesn't really matter and when we have done that we will go on to the next value and we'll do that until we don't have any more lines to read and we will print out the count that we increased during this little calculation so this was the first day and i hope that you found this interesting i hope that you learned something today did you solve it in a different language or did you solve it in a different way please leave a comment with some code and i will gladly read it and comment about it i love reading other's code it's the way i learn uh to look at how others solve solutions and if you think that i could improve my code in a significant way then either create a pr for the repository i will push this code or leave a comment and i will try to improve uh until day two i will do this as long as i can manage and release one day one video each day and um if you like this video give it a like share it with your friends and colleagues if you haven't subscribed yet please do that and i really hope to see you in the next video [Music] [Music] [Music] you
Info
Channel: Daniel Persson
Views: 1,895
Rating: undefined out of 5
Keywords: advent of code, advent of code 2021, advent of code in java, java jdk 17, challenge, कोड का आगमन, कोड 2021 का आगमन, Aufkommen des Codes, advento do código
Id: VoZHz59-sEc
Channel Id: undefined
Length: 33min 43sec (2023 seconds)
Published: Wed Dec 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.