Day 2

Now we're onto the second day! Let's do some setup.

  1. Copy and paste the folder "0" we created last time, and start the live server.
  2. Rename the folder to "2", indicating it has the problems of day 2.
  3. Download the input file from https://adventofcode.com/2024/day/2 and place it in the "2" folder, naming it "input2.txt".

Let's get started with the first task!

  1. I forgot to mention it, but do read the instructions this time too.
  2. We need to analyze the rows and tell if they are safe or not.
  3. Similarly to last time, let's read the file using the fetch function using an async function as the wrapper, and split the lines into an array.
  4. async function main(){
      let file_response = await fetch("./input2.txt");
      let file_string = await file_response.text();
      let file_lines = file_string.split("\n");
    }
    main();
  5. Next, we need to create a variable to track the unsafe lines, iterate through the lines with another for of loop and split the datapoints by whitespaces.
  6.   let unsafe_num = 0;
      for (let i of file_lines) {
        let data_list = i.split(" ");
      }
  7. Now still inside this for loop, we need to convert the array of strings into an array of numbers.
  8.     data_list = data_list.map(text => Number(text));
  9. The ".map()" function takes one argument for our purposes, a function...

    This is called a "lambda" function for... well... reasons. The syntax is "(arguments) => { return some_value; }. This, however, can be simplified to "argument => some_value".

    ...which it calls for every element in the array, and returns an array built from the values that function returns.
  10. Now we need to do the checking:
  11.     let ascending = (data_list[0] - data_list[1]) < 0;
        let last;
        for (let j of data_list) {
          if (last === undefined){
            last = j;
            continue;
          }
          if (Math.abs(last - j) > 3 || Math.abs(last - j) === 0 || (((last - j) < 0)) !== ascending) { // last-j is negative if j is larger, so it's ascending
            unsafe_num++;
            break;
          }
          last = j;
        }
  12. We create two variables here, one checking for the direction of the change and one to store the last number in the array.
  13. The first if statement is there to handle the start of the loop and initialize the "last" variable.
  14. The second if statement marks the line unsafe if one of the three things occurs.
    1. The difference is larger than 3
    2. The difference is zero
    3. The direction of change doesn't match the initially determined one.
  15. If the difference is unsafe, we add one to the counter and break out of the loop.
  16. The next and final step is printing out the number of safe lines. This is done outside the two for loops, at the end of the main() function.
  17.   document.getElementById("output").innerHTML += file_lines.length - unsafe_num;

    Now go submit this on the website and continue to the second part of the challenge!


If you want a response, please include a reply address (email, Discord, etc.).




Output div