Skip to main content
View on YouTube to comment and like!

Syntax Video

Advent of Code | Day 1 and Day 2 Explained | JavaScript / TypeScript

Chapter 1 0:00

intro

It's that time of year again. Advent of Code is upon us. If you're not familiar with it, this is a series of challenges that get released throughout the month of December. Each one getting released at midnight Eastern time on that given day. So, uh, when I'm recording this, both the challenges for day one and day two have been released. And in this video, I'm going to show you how to solve both day 1 and day two using TypeScript. Now, if you're not familiar with Advent of Code, it's been around for a very long time. And you can even look at the previous year's challenges.

So, if you go to adventofcode.com/certain year, you can actually dial in and see what challenges existed for previous years. This is a great way to practice solving algorithmic problems. Uh, a great way to test your chops and, uh, also a great way to practice not using AI cuz I'm not going to be using AI uh, to solve these problems. Now, I already solve these problems live. If you want to see me solve them live, I'll have a link in the description. In this video, I'm just going to get straight to the solutions and show you how to solve them using TypeScript. So, if that sounds good, let's dive in. My name is CJ.

Welcome to Syntax. Now, since we're just getting started, I'm going to show you how I set up my computer to write TypeScript code. I have a directory here called Advent of Code, and I like to create a directory with the current year. And then we'll go into that year, and I set up the root directory here as an npm or PNPM project. and then create a directory for each day. So I'll do a pnpm in it and then I'm going to install a couple of dependencies. We're going to add at typesnode and typescript. Now with that we're going to create a tsconfig. So I'm going to do a pnpm tsc--init.

Chapter 2 1:06

project setup

That will create our tsconfig.json and then I'm going to open up this directory with vscode. In our ts config, we want to tell it about those node.js types. So I'm going to comment out this line. So now we can use node.js JS specific stuffs and in our package.json I'm going to add type module so we can use import statements and so with that we can use Node.js to run our TypeScript code. I'm going to create a directory called 01. So this is going to be for our day one challenge and inside of there I'm going to create a file called index.ts. This is where all of our codes are going to go. So now let's jump over to the challenge. Now there's a lot of words here and one of the hardest parts of solving a lot of these problems is just figuring out what they're asking for. And so I'm just going to dive right into my understanding of the problem instead of reading all of this. And essentially we are dealing with a lock that has numbers from zero to 99. So it goes 0 1 2 3 4 5 6 7 8 9 all the way up to 99 and it goes back to zero. So it's

Chapter 3 2:10

Day 1, Part 1

a it's a circle of numbers. And the idea is we are starting on the number 50. So with 100 numbers that would be right right around six o'clock like at the bottom. So we're starting on 50. That's our starting point. And then we're given a series of directions in terms of which way we should go around that circle. So in the example input here, we're starting at 50 and then it says go left 68. So if we're going left, that's actually counterclockwise. And we'll go from 50 past zero to 82. So now we're pointing to 82. And then our next instruction is to go left 30. So from 82, we can go down to 52. And then we go right 48. So from 52 up to 48, we're now back at the top, which is zero, etc. So we're basically going around this wheel of numbers, left, right, left, right.

And we need to calculate how many times we hit the number zero based on all of these instructions. So for this sample input, we hit the number zero three times if we follow those instructions exactly. So that's what we're trying to solve here. Let's get it set up. What I like to do is take this example input and throw it into a file called sample input.txt. That way I can write my code which will read in this file and solve the problem. And then once I have it working with my sample input, I can point it at my actual input. So I'm going to create another file called input.txt. And then if you head to the problem description down here, you can click on get your puzzle input. So your puzzle input is going to be different from mine. We all get different puzzle inputs. So that way we can't cheat and share answers. We can share solutions, but we can't share answers. So, I'm going to grab all of this and throw it into input.txt. I'm also going to make sure that there is not a blank new line so that way we don't run into an issue with an empty line. And so, now I need to write some code that can read in this file. So, for that, because we're using Noode.js, I'm going to import fs. We'll

import fs from fs/promises. And I'm going to say lines is going to be fs read file. Then I can point it at our sample input.txt. And I need to tell it that we are using UTF8 encoding. So essentially when I run this file with Node.js, it's going to read that file in and now I have a variable which is a string that has the contents of that file. What I also like to do is have a function for part one because after this there's going to be a part two. So this will be part one. And this should take in a list of rotations. So I'm going to have rotations which is a string array.

Now I'm going to call part one with those lines. But lines is just a single string. I want to split it on the new line. So each rotation is on its own line in this array. We pass that into part one. And now we should get access to that list of rotations. So I'm going to log this out and then run this file to make sure that everything is working correct. So from the command line, I'm going to cd into directory01 for day one. And we'll do node index.ts.

And if everything's working, we should see an array that has the list of all of the rotations that happen on the wheel. Great. Now we can get to parsing and coding. So we need a couple variables for setup. First I'm going to have a starting variable that starts at 50 because the dial always starts at 50. So I'm going to have a const start. Set that equal to 50. And then we know the values are from 0 to 99. So that's 100 total values. So I'm going to have another variable called total and set that equal to 100. Now the thing that we're calculating is the password. And the password is the number of times the dial hits zero. So inside of our function here, we have a password and that starts off as zero. And essentially, we're going to increase it by one every time we land on zero. Now, we need to iterate over those rotations.

So for this, I'm just going to use a for each. This is going to give us each individual rotation. And that rotation is just going to be a string with a direction and a number. So I want to parse this into either a positive or negative value. So first I need to grab the direction. And this is just going to be that rotation string at zero. So every time no matter what at zero is either going to give us L or R. So that's the direction. And then the two numbers after that are the actual amount of rotation. So here I'm going to have a value and for this I'm actually going to use slice. So slice is a function built into strings and we can say at which point in the string do we want to start slicing from. So if I start slicing from one that means skip the first value in the string and give me everything after it. So slice of one is going to remove that direction and now everything after it will be the actual value. So that'll give us back a string. I want to use it as a number because we're going to be doing some math. So I'll pass it into the number constructor, which gives me a number that we can actually work with.

Now, I mentioned we want either a positive or negative number. Essentially, what I'm going to do is if we are rotating right, then it'll be positive. But if we're rotating left, I'm going to make it a negative number. So to get this working, I'm going to make it so that we can change value. And I'll just have an if statement afterwards. So if the direction is equal to left, then value times equal negative 1. So turn it into a negative number if we're going left. If it's right, it stays positive. Now from here, we need to keep track of where the dial is. And it always starts at 50, but we need a variable that can hold on to that. So we're going to have a current variable that starts at the start. And then each time we rotate, we're going to override what current is. Now, for each rotation, I want to keep track of how much the current value is going to change. So I'm going to create a variable called change. And that's going to be equal to whatever the dial is currently on plus whatever the current value is. And so if that value is negative, meaning we're turning left, we could end up with a negative number. Like in the case of starting on 50 and going left 68, we're

actually going to be at -8. And so I want that to point to 82. So if the change is less than zero, then we actually want to take the total amount and add that onto the amount of change. So if we take for instance starting at 50, if we're going left by 68, that would give us a value of -8, we want to end up with 82. So we're going to say 100 plus a8, which will give us 82. From there, we can overwrite the current value. So we're going to say current is equal to the change. But we need to account for overflow, right? So if it goes past 100 in either direction and for this we'll use the modulus operator.

So we'll say change modulus total. Now we want to keep track of every time it hits zero. So we will say if the current value is equal to zero then increase the password by one. And then when all is said and done we can log out what the current password is. So if we run our codes this gives us a value of three. Now the main crux of this problem is working with modulus and understanding the overflow in either direction. So if you really want to test out what this modulus operator is doing in the terminal, you type in node. This will give us a ripple. We can try things out.

So let's say our dial starts at 50 and we want to increase it by 68 to the right. That's going to give us 118, but we want a value between 0 and 99. So if we do 118 mod 100, we get the remainder of the division and we actually end up at the value 18 on the dial. And so if we take into account like what's actually happening. So let's say um the dial starts at 95 and then we're rotating to the right by 60. That gives us a value of 155. But if we do 155, remainder of the division by 100 is 55.

So our dial is pointing to 55. Now to see the negative in effect, let's take a look at this one. So let's say the dial starts at zero and we need to go left by one. So we we would be subtracting one. That gives us a negative one. So by our logic, if that change, which is a negative 1, is less than zero, we need to turn that into a value that's represented on the wheel, not a negative number. So in this case, if we do our total, which is 100 minus the change, that gives us 99. And then 99 mod 100 is 99 because that's the remainder of the division. So that works perfectly fine for our sample input. Now, we want to run this against our real input to get the answer for this problem. So I'll change this to read from the input file itself. But I want to do some debugging here. So I want to make sure that we're we're getting the right values. So let's actually log out on each iteration. I want to see what is that current value, what is the change, and what is the value. So now I'll run this. And we really just want to keep an eye on

current. We're looking for any anomalies. And do you spot them? Our current value is going negative. And the reason this happens is our logic is a little bit too naive. Right? So this is happening right here. So if the amount of change is less than zero, we're just adding it on to the total. But let's consider the scenario. So to test this out, I'm going to pull up node again. So we have a ripple. And so let's say we're at 50 and we want to go left by 220. So if we subtract 220, that's going to give us -170. And if we take the total and subtract -170 from it, so we'll do 100 + 170, that now gives us a -70. We're still not wrapped back around to a positive value on the wheel. But if we take -70 and then subtract that from our maximum value again, so we'll do 100 plus70, that gives us a value of 30. So if we started at 50 and we went left by

220, we actually end up at 30. So the main change that I need to make in my code here is instead of just an if statement, we can actually make this a while statement. So we can say while the change is less than zero, keep adding it onto the total and overwriting it until we get a positive number. And so essentially it would run each iteration until it got that positive number 30.

And so now with this change, if we run our codes again, we don't see any negative values in the current value. Now, it's interesting to note that we still get the same value 192. So, if I change this back to an if statement, even though we got negative values, still get the same value, but it is definitely more correct to have to have a while loop there. So, let's give it a try. With that working, we can take this number, plug it in, see if we got it right, and we did. Great. So, we can move on to part two. Now, part two is a little bit trickier. Instead of only counting when the dial hits zero, we want to keep track of every time it goes past zero. So if it's rotated left or right past zero. So for this, I like to copy my function, rename it part two, and then do the modifications there. So now we're going to call part two, and we'll run it on our sample input again.

Chapter 4 12:30

Day 1, Part 2

Now, for this, we actually want to keep track of the number of times that it goes around. And we can do this by finding the number of times it could be divided by whole. So here's what we're going to do. We get rid of all this extra logic. And if we are rotating left, that means it's a negative value.

I want to determine how many times does this value, the amount that we're turning, fit into our maximum value as a negative value. So I'll call this the divisions and this will be the value itself multiplied by a negative 1 because we're we're left this time divided by our total value which is of course 100. Now, this will potentially have a decimal place and we want to find the whole number. So, the whole number of times that it can go in cuz that's the total number of rotations. So, we're just going to do a math.floor. So, this will drop the decimal place and this will tell us the whole number of times that we can go into uh this value. And we do want this to be a positive number.

So, I'm going to do total times negative 1. So, we make sure that it's negative. And so this will give us the total number of divisions that this value basically wraps around into the total amount. And so now that we know that we can increase the password by that number of divisions. And so this essentially tells us how many times we would have gone past zero for that given amount of rotation. Now from here we want to calculate the modulus again. Now we could replicate the logic like we had before. But I want to do something a little bit more interesting. I want to create a function that will give us the modulus regardless of whether it's negative. Basically, it'll it'll wrap around automatically for us. And this will be nice and useful because we won't have all this logic embedded within the function here. So, I'm going to create a function called mod that takes in the value, which is a number. And then we also have the divisor. So, the thing that we're finding the remainder of division by. And so, this is also a number. Now, from here, we just need to do some maths. So, we're going to return the value itself minus the divisor times math.f floor of value divided by the

divisor. So let's say in the case of 50 modulus 100, we have 50 minus our divisor which is 100 times math.f floor of 50 / 100. 50 divided by 100 is 0.5. It's 1/2. Math.floor of that is just 0.

So this entire right side is 0. So 50 minus 0 is 50. 50 mod 100 is 50. Easy. Now let's take for example uh a number greater than 100. So let's say 220. So if we do 220 that would be 220 minus 100 * 220 divided by 100 is 2.2 math.flo is 2. So our divisor 100 multiplied by the whole number of divisions 2 gives us 200. And then 220 minus 200 gives us 20.

So 220 mod 100 is 20. And what's nice about this is it also automatically calculates the wraparound values. If for instance we take the scenario where it goes negative. So if we're going 50 left by 68 that gives us a8 divided by 100 is0.18 and math.f floor of0.18 is -1 which I had to look up but -1 * 100 gives us100 and then a8 minus a100 is8 + 100 which gives us the value of 82. So this nifty little function automatically wraps around for negative values for us. So we don't have to do any weird things like a like a while loop. So with that we can use it in our function. Um I'm going to rename this to modulus just to be clear. And in this case when we're rotating left we still want to turn value into the negative. So we'll multiply it by a negative 1 and then we'll use that in both spots. So

here we'll pass in value. That gives us the whole number of divisions. Now we can also calculate the remainder of the division using our fancy modulus function with that value which can be negative now and our negative total. So divisions gives us the total number of times we're going to wrap around. But there still is a possibility that we'll wrap around one more time given this remainder from where we're currently at.

So we need to take that into account. So if we currently were not at zero because if we were at zero any remainder is either just going to be before or after it. It's not going to wrap back around. So we'll say if current is not equal to zero, we don't care about that scenario. And we take the current value, add the remainder onto it. If that is less than or equal to zero, meaning we went past zero on the remainder, then that counts as one more turn past zero. So we're going to increase password by one. Okay, so it's a bit of a doozy, but now we're going to basically do the exact same thing in the right direction, but with just positive values. So, I'm going to paste all this code in, but now we can just do total instead. Same thing, we'll do total here. And then in the case of the remainder here, we just want to see if we're going to go past 100 in the positive because we're rotating in the positive direction. Let's say we're at like 99. If that final remainder was five, that's going to put us past 100.

So, we're here. We're going to say if this is greater than or equal to 100, then we did one more pass uh the zero. So we'll increase our password by one. Okay. So we've determined the amount of rotations. Now we just need to overwrite current uh based on the current turn. So here we're going to do current. And in this case we can just do modulus because that's going to account for the overflow. And we just want to do modulus of the current value plus value which is the amount we're turning. And we're finding the mod of the total. Okay. So from here we've done it. It's a bit of a doozy. You might have to wrap your mind around what that modulus is actually doing, but it definitely simplifies things for us. And with that, we should get the right answer of six. And we do.

So now if we pass in the full input, we get our second answer and we can check if it's right. Let's give it a try. We go. That's right. Nice. So we can move on to day two. So let's head into day two. And I'm going to go ahead and get all of my codes set up. So I'll make a directory called O2. Copied our parsing code. We'll create this index.ts in here and we'll import fs from fs promises.

Chapter 5 18:59

Day 2, Part 1

We'll also make sure we're only reading in the sample input first and then we'll create both of those files. So we'll have our sample input. This will have all of these and it needs to be all on a single line. Great. And then we'll also load in our main input. So we'll grab this. Create a new file input.txt. Throw it in there. Good to go. Okay. So now we need a function for part one. This is going to take in our ranges. So each of these is a commaepparated list of a range of values, a start value and an end value. So we're going to have our ranges. We'll just have this be an array of strings which we're going to further parse once we get them in. And now we can call part one with lines split on a comma. If we split on commas, that's going to give us each group with the dash separating the start and the end.

And so now as we iterate over each range, we can actually parse out the start and the end. So here I'm going to say ranges for each. This gives us a range. And if we want to parse it out, we can do a split. So this is going to give us the start and the end. And we can say range.split on a dash. Now this is going to give us strings, but we want to work with them as numbers. So we can actually map that to the number constructor, and this will give us back numbers that we can work with instead. So the problem says that for each one of these ranges, we want to find all of the invalid IDs within that range. And an invalid ID is essentially a number that is repeated twice within itself. So 11 22 999 1010 11851185 2222 446 446 3859 3859 etc. So if a number is has a repeating number inside of it, that is an invalid ID. So for each of these ranges, we want to look at all the numbers from start to end within that

range and check to see does it have a repeating value inside of it. So for this, I'm just going to do a for loop. It's going to start at that starting value. And because we're in TypeScript, I'm going to use the notnull operator because split technically could return undefined. So this is saying start could be a number or undefined. I want this to be a number. So I'm going to use the notnull operator. And we want to go while I is less than or equal to end.

again the notnull operator and then increase i for each iteration. So this is going to loop from start to end for every single one of those ranges. And then for each one we want to check to see is that given number an invalid ID. So we want to keep track of all the invalid ids. So I'm going to have a variable called invalid ids. We'll start off as empty. We want this to be a array of numbers. And right here we'll say if is invalid ID we're given i. Then we'll push it into that list. So we'll say invalid ids.push I. Cool. Now we need to actually write this function. So how do we determine if the number is repeating?

There's a couple of ways to do it. I'm going to do it the manual way first because this might be the way that you reach for if you're not going to reach for a regular expression. So this is going to take in a value which is a number. And one of the easiest things we can do is just split it in half and compare the left half to the right half. So if the left half is equal to the right half, it's an invalid ID. And that works for every one of these because you'll notice that each one of these invalid IDs are all a uh even length so we can evenly split it in half. So because of that I'm going to grab the left and the right side. So first I'm going to turn that value into a string and I'll just say value.2ring. And now that it's a string I can use slice to pull off the left or the right side. So here we'll say the left is going to be that value string slice. We want to go from zero up to the halfway point. So I'll say that value string.length divided by two. And you'll notice that slice is non-inclusive. So it goes from

start to end but not including end. Which means if we want to find the right half, we can do the same thing but start at value string.length divided by two and then it automatically goes to the end. So this left half will not include the index passed as the second value. And this right half will start at the index which is the length divided by two. So this gives us the left half and the right half. And here we're just going to say if they're equal. So return left is equal to right.

If that's the case then we have found an invalid ID. So now let's go ahead and just log these out. Make sure we're getting the right values and we'll give it a try. So we'll cd into day two and then run our file and we'll double check. Did we get all of the right numbers? So, we're looking for 11, 22, 99, 10, 1010, 11,885, 11,885. Yes. 22.

Yes. Yes. Yes. Yes. So, we found them all. Fantastic. The last part is we need to sum them all together. So, adding up all the invalid IDs gives us this value. So, now we need to sum them up. And for this, I'm just going to use a reduce. So, we get the total sum, each individual value, and we'll just do sum plus value. That should sum them all together. and we get 1227775554 which is the number we're looking for.

Fantastic. If we run this against our actual input now we get this value. So let's give it a try. Awesome. We did it. So that worked. But I I do want to show you how we can do this with a regular expression. So after turning it into a string we can create our regular expression and then do test against that string to see if it matches. And the regular expression that we want to do is we want to find a group of one or more numbers. And so slashd says find a digit. The plus sign says one or more.

So this matches any group with one or more digit. And then from there we can say match against that group. So find a group of numbers and then match that group of numbers. And we can be more explicit to say that this needs to match any number that starts with that group and then ends with that group as well.

And so if we do this, we should get the exact same value. We do, but this was with a regular expression. So now for part two, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So it doesn't just have to be the left half and the right half. We're also taking into account oddlength numbers um in scenarios where it might be even length but we're matching like two numbers repeated. So like 56 56 56 that's technically an even length but the number 56 is repeated. So the easiest way to solve this is just by modifying our regular expression because this regular expression says find one group and then match it exactly after that. So that's only going to work for the case where you have the first half and the second half being equal. But we can also modify this regular expression to say find the group match against that group and find one or more of those groups. So just by adding the plus sign there, that will now match even smaller groups of like one number or two numbers

Chapter 6 25:10

Day 2, Part 2

that are repeated. So if we run this again with our sample input, we get 4174379265. And that's the number that we're looking for. So this video is getting much longer than I wanted it to be. I'm going to leave you with just the regular expression solution. If you want to see me solve this without a regular expression, essentially I am looking at any number of repeated values and then repeating that value and comparing it to the original string. Check the live stream where I where I solved it that way. But for this video, we're just going to use the regular expression. So let's give it a try. We're going to run it against our overall input. This is the value we get and we can only hope that it is the answer. It is awesome. So we completed day two. Now that's it for this video. Tune in on Thursday. I'm going to release a video where I solve challenges number three and four. Let me know down in the comments if you were able to solve it. Let me know down in the comments if you were able to solve it in a different way maybe or if you have any tips for anybody that's trying to solve these as well. If you have questions, leave them in the comments.

All that stuff. Thank you so much for watching. I'll see you in the next one.

Quote it with credit and a link. Quoting Syntax