Syntax Video
Advent of Code | Day 3 and Day 4 Explained | JavaScript / TypeScript
Intro
What's up? CJ here. Today we're going to be solving days three and four of the 2025 Advent of Code Challenge using JavaScript and TypeScript. Now, I solved these problems live on stream. So, if you want to see me struggle and solve them live, check the link in the description for that. But in this video, I'm going to show you how I solved them and I get right to the solutions that I figured out live, which took me a few hours, but this should be much shorter than that. So, if you want to see how I set up my code for working with Node and TypeScript to do these challenges, check out the video that covers day one and two. In this video, we're going to get right into solving. And I have all of the files ready to go and and ready to run in my folder. So, let's dive in.
Day 3, Part 1
All right. For day three, part one, we are essentially given a list of numbers. Each of these numbers represents a battery bank. Each number being a battery labeled with its voltage. And the short way of going about this is we essentially need to find the two largest numbers in the list from left to right.
So on the first pass we find nine and then on the second pass we find 8. So the largest possible number is 98. For this number on the first pass we find 8 and then we find nine. So this is one edge case where basically we don't want to look at the last value when we're finding the maximum value because uh we're only finding two values. So that last value could potentially be the maximum value. And then in this example, very similar. On our first pass, we want to find seven. And then on the second pass, we'll find eight. And then in this example, 9 is the largest. So that's easy. But after that, we need to find the smallest number to the right of that number. So this last example is really the crux of the solution here, which is find the largest number excluding the last value, and then find the largest number after the largest number that you just found. So that's what we're going to implement. Now, like I mentioned, everything is set up to go. I have my sample input here, and then I am splitting it on new lines. So we get
each battery bank as its own individual string. Now, we're attempting to find the total output jolt, which is going to be those max numbers from each bank summed together. So we'll start off with the total equal to zero. And then we're going to iterate over each battery bank to find its max. Now bank is a string, but I want to work with an array of numbers. So we're going to go ahead and parse it. So here I'm going to say the values we bank.split split it on the empty string and then map it to the number constructor. So this will essentially give us back a list of numbers that we can work. Now from here I want to find the largest value in the array from left to right except for that last index in the array. So for this I'm going to do a simple for loop. We'll start i at zero while i is less than values.length minus one because we don't want to look at that last value and then increase i on each iteration. Now, we need a variable to keep track of the max index. And so, I'm going to start that off at zero. So, from here, we just want to see is the value at the given index greater than the value at the max index.
And if it is, we're going to overwrite that max index. So, if values at i is greater than values at max index, max index equals i. Simple as I said it and then I then I wrote it. But, um, we are using TypeScript. So, TypeScript is complaining that technically when you're indexing into an array that could be undefined. We know that our index here is going to be within the range of values because we wrote the for loop ourself. So in this case it actually is okay to use the notnull assertion here in Typescript because we know that I will always be within the range of the length of the values because we know that I will always be within the length of values because we wrote the for loop.
Okay, so that allows us to find our max index. From there we want to find the next index starting at the max index. So we're going to have another loop, but this one is actually going to start at max index + one because we want to find the maximum value after the max value that we just found. So this will be max index + one. So this one pass finds the max index in all scenarios, right? So it's going to find nine in this case. In this case, it's going to find eight because we're not looking at the last value. In this case, it's going to find seven. And in this case, it's going to find nine because we're looking all the way up to here. Great. Now the second pass needs to start after the one that we just found. So in the last case, we search all of those numbers. In this case, we're just searching that last value. Uh in this case, we're searching every value after the first one. And in this case, we're searching every value after the first one. So now we need a loop that starts one after the index that we just found to find the next max index. So let's create a variable called the next max index. That's going to
start off at max index plus one. So we immediately assign it to the value after the max index that we just found. And then we need a for loop that starts at that next max index and goes all the way to the end. So in this case, we actually do want to look at that last value because it could be that last value. Now we do need to replace our variables. So we're going to look at values at the next max index and then we're going to overwrite our next max index. Awesome.
So at this point, we should have found those two indices and now we will log them out. So we'll log out the values at the max index and the values at the next max index. So now let's run it to see what we get. Let's go into day three and then run this file with node. And if we compare our output with the problem description. So we get 98, 89, 78, and 92. Fantastic. So at this point, we need to actually turn those into two-digit numbers and add them on to the total. So for this we'll have our joltage and you could technically turn it into a string and then turn it back into a number but we can just take that first max index value multiply it by 10 and then add the second max index value and that will give us a two-digit number that's the combination of the two. And again we can assume that they're not null because we actually found those indices. From here we can increase our total by that joltage. And then when we're done we will log out the final total of all the joltage. So, if we run our codes, we get
357. And 357 was the value that we're looking for. So, if we've done everything right, we can run our code against the actual input. And again, I've already set this up. So, I have the puzzle input ready to go. We'll run our codes. We get this value. Let's try it out. There we go. So, day three, part one done. Let's move on to day three, part two. Now, day three, part two is much trickier, and this one took me over an hour to figure out, but I'll walk you through the solution that I came up. So for part two, instead of just finding the two largest numbers, we need to find the largest 12 digits from left to right. So the first two fairly simple because this first one is just decreasing in values, right? So if we found the max from left to right, we'd have nine. And then the max from left to right, we'd have eight. And then the max from left to right, we'd have seven. And we just repeat this until we have 12 values. But things get tricky when it's not just in descending order. So in this example, the first number that we want is four because we're collecting 12
Day 3, Part 2
digits in total. So we have to grab the largest one on this side and then just grab the rest of the list because that would actually put our total number of digits at 12. So there's a lot we need to think about here. And in this case, because we need 12 digits and there are only 11 digits left that we're searching for, this would actually just be the rest of the number that we're looking for. And then on this last one again, we'll find the max, but then we need to find the next max and make sure that there are at least 12 more digits for us to choose on the end. So this took me so long to figure out. I tried like four different ways. But at the end of the day, that logic that I'm talking about here where find the value, but take into account the fact that we're looking for 12 digits. So we can't look any further than a certain amount to make sure that we have digits left over for the rest of the number. So, if you're still not following me, I'm going to write this out as pseudo code so you can you can get a better idea of what we're going to do here. So, I'm going to copy my part one function. We'll call this part two.
And I actually am going to get rid of all of my existing codes. We're going to write it from scratch. So, the problem is asking us to find 12 digits. Okay. So, on our first iteration, we need to find the largest number for all the values excluding the last 11 values, right? because we could get to the point where the last 11 need to be included in the number. So we're not going to search across those. So here we'll say find the max value up to n minus1. And so this will be index n minus 11. And to see this in action, let's use this number here. So if we're trying to find the 12 digits here, we're going to find the max value up to n minus 11. So we're going to be searching this except for the last 11 values. So 1 2 3 4 5 6 7 8 9 10 11.
We're actually only going to search those four values to find the max. So in this case, our max value is four. Great. From there, we need to find the max value after the previous max value up to n minus 10. So at this point, we need at least the last 10 values in the array. So we're going to be searching this after the one that we found. So we just found the four. So we're going to get rid of those. And then we're going to get rid of the last 10 values. So 1 2 3 4 5 6 7 8 9 10. So in this case we're just searching 2 and 3. In this case the max value here is three. And if we look at our solution we are looking for 4 3 4. So good so far. And then we have our next iteration. So it's going to be very similar. Find the max value after the previous max value up to n minus 9. And n I'm saying is the the length of our our input string here if that wasn't clear. So in this case we take a look at what we're searching. We just found this value three. So we're not going to be searching that. And then we need to
remove the last nine values or or we're not going to be looking at the last nine values. So 1 2 3 4 5 6 7 8 9. And when you look at that, we're only searching one number at this point. So the max is four. And then we repeat. But in this case, we're only going to search up to n minus 8. And so we're looking at everything after the four, but not the last eight value. So 1 2 3 4 5 6 7 8.
And when you look at that, the max value is two. So when I was solving this live, writing out this pseudo code is really what got me to come up with the solution because I was trying so many things like reversing the list and and all this stuff. But this should be a very straightforward solution, right? Because our pseudo code basically says we need a loop that starts at 11 and goes down to zero and then each time we're only going to be looking at a certain subset of the values. So let's do it. And essentially what we're going to need is a loop that goes from 11 down to zero. Right? So that's going to be our outer loop. So we'll have a loop. And I'm going to call this variable needed because it's how many digits do we currently still need?
And that starts off as 11 cuz we're going to go down to zero. So we need 12 total digits. And so we'll say loop while needed is greater than or equal to zero. And here we'll decrement needed on each iteration. So this is a simple little loop. It's going to go 11 10 9 8 7 6 5 4 3 2 1 zero because it's inclusive, but that's going to allow us to know how much we need to iterate up to when we're looking at values. Now, we want to keep track of what the max index is. So we're going to start that off at zero. And then inside this loop, we'll loop over values up to the total length of the values minus the amount that is currently needed. So we'll have a for loop. We'll start off I at the max index and we want to go while I is less than the total number of values. So values.length minus the number of values that are needed and then on each iteration we'll increase i. So essentially this loop will start at the last max index and then go all the way
up to in minus the needed amount. So in this case it would have only gone up to the value two because we didn't want to include those last 11 values. So this inner loop here is going to start where we want and then end where we want without exceeding the number of values that are needed to to put into our overall 12digit number. Now inside of here, our logic is going to be exactly the same. So if values at i is greater than values at that max index, we're going to overwrite it. So we'll say max index equals i. And then we can use the notnull assertion because we know we're within the range of the length of values. And then from here we want to keep track of every digit that we find.
So we need a list. I'm going to call this digits. And this will be an array of number. And we're going to push into this list of numbers for each digit that we find. So here we're going to push in values at that max index. And again, not null assertion. And then we're going to overwrite the max index to be max index + one. So that's our next step here, which is find the max value after the previous max value. So wherever that max value was, set the index to be the next one. Repeat. push the digits in. At the end of the day, we should have 12 digits. So here, I'm going to log out those digits, join it together on the empty string, and we won't add it to the total yet. I just want to make sure that we're actually calculating the right number. So let's run this for our sample input and see what we got. So this gives us 357 uh and we'll call part two. So let's run this for our sample input and also uh call part two instead. So now if we run our codes, this gives us these numbers.
Let's double check these against what we have in the problem description. So we get 98765432111. Great. We get 81119. Awesome. We get 434234234278. Wonderful. And we get 8891112111. Amazing. So we've found the right numbers. Now we need to add them up. So I'm going to get rid of my pseudo codes here. And here we can just say total plus equal those digits. But we are going to parse it as a number after joining all those digits together. And that should give us our final total of 31219 01778619 which is the same number that they got.
Amazing. So now if we run our codes against our puzzle input, we get this number. Let's give it a try. And it's right. Amazing. So I'll give you one last look at our solution here. Uh just in case I went a little bit too fast, but uh again I I tried many different ways. I finally ended on this solution which I think is pretty great because it is bigo of 11 in. Right? So this this is a nested loop but it's not in squared.
We're not looping over the numbers and then again over the numbers. We're not looping over the length of the array and then the length of the array again. We're literally just looping up to 11, but each iteration is just one pass of the array. There's probably more performance solutions and if you know of those or solutions that require less iterations, let me know down in the comments. But this actually runs fairly fast on our input because this isn't a nested loop based on the length of the input. It's just iterating over the values 12 times for each value in the input list. So that's part two. Let's move on to day four. Now for day four, part one, we are given this input here.
Day 4, Part 1
And essentially what we need to do is we need to find every cell in this grid. So if you haven't thought about this problem in terms of of a grid yet that that's basically what it is. Each of these values here you could think of as like an x ycoordinate or a row column coordinate. And so what we need to do is for any given at sign if fewer than four of its neighbors are an at sign then it can be accessed. And what do we mean by neighbors? Well for any given row and column we can look at all of the neighbors on top. So you have the previous row up and to the left, right above it, and right above it to the right. If we're looking at this value, then we also have the value to the left, the value to the right, and then we also have the values below it, below it, to the left, and below it to the right. So for this particular cell, all of its neighbors are the ones that are surrounding it. So to solve this problem, we basically need to look at every single cell. If the cell is an at sign, we then look at all of its neighbors and count them up to see which
one of those neighbors are at signs. And then if fewer than four of those neighbors are at signs, then this roll of paper can be accessed. So that's the problem. That's how we're going to go about it. Let's get into it. And the idea here is when we read in that sample input, we take a look at it. This if we split it on new lines, we're going to get multiple strings. But if I have an array of strings, we can treat that like a matrix or a grid because I can say array at zero which gives us this first row and then that's a string. So we can index into it and say array at zero at uh three and that would access that particular element right there. So that's how we're going to treat it. So we're going to take those lines that we just read in and split it on the new line. So that's going to give us each individual row. We're going to call this our grid. And our grid is just an array of string. But again, to really show you what I'm talking about here, let's say I want to access this at sign right there.
I would say, let's just log it out. Grid at zero. So in the first row and then in the third column, which I can't remember. Yeah, 0 1 2 in in the second column. So this is going to give us that very first at sign. But if we log the one before it, that should give us a period, right? So on the sample input the zeroth row. So that needs to be index 2. But when we log this, we should get that very first at sign. And for instance, if we wanted to access the period here, if we said let's log the grid at 01, that's going to be a period.
If we logged the grid at 0 0, that should also be a period. If we log the grid at 03, that actually should be an at sign as well. So this is how we're going to be accessing the cells in our grid, and it should give us back those exact values. So if we run day four, we get those values that we were expecting. But with this in mind, now we can just treat this array of strings as a grid with rows and columns. So the overall width of the grid is going to be the length of any one of the strings that's in the array. So I'm going to create a variable called width. And we're just going to grab the first string in the array and grab its length. And we are going to use optional chaining here because TypeScript isn't sure if it actually has a value there, but we know it does because the sample input has data at least in the in the first row.
So that's going to be the width of our grid. And then I'll create two loops. We'll have one loop that will go over all of the columns and then another loop that will go over all of the rows. So we'll have one loop that goes over all of the rows. And then another loop that goes over all of the columns. So we'll have our row starts off as zero. Well, the row is less than the grid.length because our overall length is going to be how many rows do we have. So that's the maximum row. And then we'll increase the row on each iteration. And then inside of here, we can iterate over all the columns. So we can have column start off as zero while column is less than the overall width and we'll increment the column on each iteration. Now instead of optional chaining, we're actually going to use the not null assertion because we know that it exists. So we want width to actually be a number. So with these two loops, we can now look at every single cell in this grid to check what its value is.
And that's exactly what we need to do. So I'm going to create a variable called cell. And this will be grid at that row, at that column. And again we can use the notnull assertion to say hey I know that that exists but that's going to give us that particular cell and then we can check to see is that cell an at sign. If it is we need to count up its neighbors.
So I'll say neighbors is count neighbors with this grid with this row at this row and at this column. And if the total number of neighbors is less than four then we'll increase the total number of items that we can collect. So let's have a total that starts off as zero. If the neighbors is less than four, we're going to increment that total by one. And then at the end of it all, we're going to log out that total. Okay, seems simple enough, right? All we need to do now is implement count neighbors. And this is going to be the code that looks at all of the neighbors around that given cell in that row and column. So, I'm going to create a separate function, count neighbors. It takes in a grid, which is a string array. We get the row, which is a number, and the column, which is a number. And now we can take a look at each neighbor. Now, again, the way to think about this is let's say we take this cell into account. We want to look above it, to the left and the right and directly above it. We want to look to the left. We want to look to the right.
We want to look below it. To the left, to the right and directly below it. So, we want the uh top left, we want the top, we want the top right, we want the left, we want the right, we want the bottom left, we want the bottom, and we want the bottom right. So, this should give us all of our neighbors. We have 1 2 3 4 5 6 7 8 total neighbors that we need to check to see are they an at sign as well. Now there's a few different ways to go about this. We could actually have some loops that are allow us to calculate like the row before, the column before, the column after, the current row, the row after. I just like to hardcode this because I can know exactly what each one is. So what we're going to do is have an array of neighbors that we directly put inside of this array and then we'll iterate over it to check those given neighbors. So the top left is going to be the grid at the row minus one. Right? So if we're looking above us, we want to go row minus one. And then if we want top left, we're going to also say column minus
one. And again, we'll use the notnull assertion. So that finds the neighbor to the top and to the left. If we want to find the neighbor that's on top of us, we just do row minus one on the current column. If we want to find the neighbor to the right, it's going to be row minus one, column + one. So I'm going to write this out for each one of these. So to the left will be current row and column minus one. To the right is current row and column + one. And then bottom left will be row + one. So we want the row after this with column minus one. and then bottom will be row + one in the current column and then bottom right will be row plus one column plus one. So I wrote these out manually. Um and honestly I encourage you to do this if you've never really worked with grids in this way. But as a challenge to you try to implement this as a nested loop instead of writing these out manually to to calculate all of these neighbors. So this will give us the value of each neighbor whether it's an at sign or a period. And we want to return the total number that are at signs. So I'm
actually going to use a reduce for this. So we're going to reduce over this array. We're going to get the total sum and the current neighbor. And then for each neighbor if it's equal to the at sign, we're going to return sum plus one. Otherwise, we'll return the sum. And we need to start this whole thing off at zero. So this is going to look at each value. If that value is an at sign, increase the sum. Otherwise, keep the sum the same. And so now for every given cell, we're calculating which neighbors are at signs. If the total number of neighbors is less than four, we increase the total. This should do it. So let's run it for our sample input. And we do get an error. And you might have caught this. And that's because we actually can't assume that these are not null. In this case, we actually need to use optional chaining because if we assume that they're not null, then uh this would be accessing undefined. So if this if row minus one doesn't exist, this is undefined. And then we're trying to index into undefined. So for each one of these, we actually do want to use optional chaining, which says if the
thing on the left hand side exists, then do the access on the right hand side, but if not, just return undefined. And for our use case, that's totally fine because if it's undefined, then it's not the at sign, so we're not going to take it into account. So that was a bug. Instead of using the notnull assertion, we need to use optional chaining. Okay, let's give it a go. All right, we get the value 13. That matches up with what we need. Let's run it on the actual input and see what we get. We get 1320.
Submit that. Fantastic. Let's move on to part two. Okay, for part two, this is actually going to be iterative because what we need to do is on the first pass, it works exactly the same as part one. Count the total number of cells who have less than four neighbors. Great. But act as if those stacks of paper had actually been picked up by the forklift. So, we would replace them with a period or an X. so they're no longer in the way. And then do the exact same thing again. So which of the cells have less than four neighbors? And then do it again. And then do it again. And then do it again until we reach a point where no more cells have less than four neighbors. So this should be fairly straightforward.
Day 4, Part 2
It's really just a matter of reworking our current function to replace the cells that had neighbors and then run it again and run it again. So let's get it going. I'm going to copy our part one function, throw it into part two. Now, this calculation of the total really needs to be a function that we can call over and over again. And so, I'm going to call this grid pass because we're going to be passing over the grid. And we're going to move all of this code in there. And instead of logging the total, we're just going to return the total.
And now, essentially what we want to do is we want to keep passing over the grid until we don't find any more neighbors. And so, what we would do here is calculate the neighbors and then overwrite any found cells with a dot. So basically remove that stack of paper. So we'll need to write that code. But first let's think about how we're going to call this iteratively. So essentially what I want is to get the total number of removed stacks. So we'll call this removed. And this will be a grid pass like so. And then I want to keep passing over the grid until I remove no more. So if you look in their example, once we reach this state, there are no more that can be removed. So if our grid pass function returns zero, then we're basically done. So what I'm going to say is while the number of removed is greater than zero, do it again. And with that, we'll also need to keep track of like our grand total. So we'll have our grand total that starts off as zero. And then we'll need to increment our grand total each time by the number that were removed. When all is said and done, we
should have that grand total. So we're going to log it out. Awesome. Now becomes the tricky part of overwriting those found cells with any periods. So essentially what I want to do is if we found a stack of paper that can be removed. I want to keep track of that row and column. So after this iteration I can do one more iteration that overwrites those particular values. So for this I'm going to have an array of an array of booleans. So essentially we're going to have an array where any one that we decide to remove we're going to put the value true there. So in the second pass I can instantly just know if a particular row and column needs to be removed. So here we're going to have our found and this is going to be a boolean array of arrays and starts off as the empty array. For any given row we are going to push in a brand new array for that given row to keep track of. And for any cell that we find we want to put the value true into our our found grid. So here I'm going to say found at row at column equals true. And essentially what we're doing here is what's called like a
sparse array. So this array starts off as empty and then when we find a column which could be like column 3 or four then we set the index at three for that row to be true but all the indices before it will be undefined and all the indices after it will be undefined unless we overwrite them and that's totally fine because when we do our second iteration we just need to know which values have true in them and if they have undefined we're going to ignore them. So in this case, we can use the notnull assertion because we know that we just pushed an array into found for this given row. And then we're going to set column to true. So with that, all we have to do is one more iteration for rows and columns. And then just build up a new row that has those given cells replaced with a period like so. So we're iterating over the whole grid.
Essentially, for each row, I'm going to be creating a new string. So we're going to call this the updated row. And this will start off as the empty string because we we can't mutate strings in uh JavaScript. It would be great if we could just literally override grid at row at column. But it's a string. So we need to build up a new string. So our updated row is going to be updated row plus that current cell value. But if that cell value was found in the previous loop, we're actually going to overwrite it. So here we'll say if found at that row at that column, then cell equals a dot. So instead of writing the value that was there, we're going to write a dot instead. From there, we've updated that given row, we need to overwrite it in the original grid. So here, we can say grid at that row equals this updated row. So the first pass is going to work exactly like it did before, except we're keeping track of all of the ones that can be removed.
Then after that, we need to update each row to remove that cell. So replace the at sign that was there with a period and then repeat the process. And so this while loop is basically just going to keep running, overwriting, overwriting until we find no more cells that have less than four neighbors. So with that, let's try this out on our sample input.
We're going to run this for part two. We'll give it a go. We get the answer 43, which is the answer that we're looking for. So let's try it with the overall input. And we get 8354. Let's hope we did it right. And we did. Awesome. So, that's it for days three and four. If you enjoyed this, let me know down in the comments. Also, if you solved it a different way, let me know as well. Also, if you didn't like my solutions, let me know. I don't know if I'm going to be posting days five and six or beyond yet. If you want to see that, let me know down in the comments, but it might not happen. Uh, but that's all I got for you in this one. I'll see you in the next one.
Quote it with credit and a link. Quoting Syntax