DEV Community

Rachel
Rachel

Posted on

Advent of Code 2021 in Javascript: My beginner solutions (days 1-2)

Hi dev.to!

Since this is my first time doing Advent of Code, I thought it would be fun to document the experience as I go. So far it's been super fun solving the problems, especially since I have no classes to keep my brain busy over the summer... I'd been missing that feeling of satisfaction when your code works, and Advent of Code gives me just that, but in bite-sized pieces! 😁

So, here are my solutions to the first two days of Advent of Code 2021. Just to note, I haven't looked at anyone else's solutions yet, so here you have my raw unfiltered thought process!! ✌️ Since I have limited maths and data manipulation skills (and I'm still learning Javascript), these solutions may be a bit simple/beginner level, but they do work and I think they're quite easy to read at least. Here we go!

Day One: Sonar Sweep

(link)

Part One

In this problem, we are given a whole heap of numbers separated by new lines, that represent the depth of a submarine. We need to count the number of times the depth measurement increases from the previous measurement.

The first thing to do is to convert the input we're given into a manageable format. First I copied the text from the input page into my Javascript file as a string literal and assigned it to a variable "input".

const input = `159 ...
// etc!
6568`
Enter fullscreen mode Exit fullscreen mode

Then I collapsed the first 2000 lines in VS Code since that's how long it was. XD
Next:

const arr = input.split("\n").map(Number);
Enter fullscreen mode Exit fullscreen mode

This converts the input into an array of strings using the separator "\n" (new line), then converts each string into a number. Now we just need to initialise a counter and iterate through the array, checking whether each value is larger than the previous one.

let count = 0;

for (let i = 1; i < arr.length; i++) {
  if (arr[i] > arr[i - 1]) count++;
}
Enter fullscreen mode Exit fullscreen mode

We start with index 1 because index 0 doesn't have any previous value to measure against.

console.log(count);
Enter fullscreen mode Exit fullscreen mode

Now we can check the answer, which should be the value of 'count' :)

Part Two

In this problem, we need to add each value to its previous and next values in a sliding window. Then we again need to give the number of times the resulting values increase.

We already have the array ready to go, so now we just need to iterate through the list again starting with index 1.

let count = 0;

for (let i = 1; i < arr.length - 2; i++) {
  let a = arr[i] + arr[i + 1] + arr[i + 2];
  let b = arr[i - 1] + arr[i] + arr[i + 1];
  if (a > b) count++;
}

console.log(count);
Enter fullscreen mode Exit fullscreen mode

Here we need to stop two indexes before the last index (i < arr.length - 2) because after that there won't be enough measurements to create a three-measurement sum.

Ok, on to the next day :)

Day Two: Dive!

(link)

Part One

We're still in a submarine! But this time we need to learn how to steer it?? In this puzzle, we're again given input separated by new lines, but with added complexity. I won't get into detail as the puzzle instructions explain it very well.

Basically, we need two counters: depth and horizontal position. These will be increased (or increased or decreased in the case of depth) as we iterate through the input. First off, we get organised by initialising our array and counters:

const arr = input.split("\n");

let depth = 0;
let horizontal = 0;
Enter fullscreen mode Exit fullscreen mode

Now we have an array of strings looking like ["forward 5", "down 5", "up 3"] etc.. We need to split up the word and the number, so that we can work with each separately. I put each instruction into its own nested array like this:

const newArr = arr.map((instruction) => {
  return instruction.split(" ");
});
Enter fullscreen mode Exit fullscreen mode

Now newArr will look something like: [["forward", "5"], ["down", "5"], ["up", "3"]]. All we have to do next is iterate through the array, checking the instruction and adjusting the associated counter accordingly. We can parse the number part of the instruction (which is currently a string at position [1] in each sub array) with parseInt(i[1]).

for (let i of newArr) {
  const num = parseInt(i[1]);

  if (i[0] === "forward") {
    horizontal += num;
  }
  if (i[0] === "up") {
    depth -= num;
  }
  if (i[0] === "down") {
    depth += num;
  }
}
Enter fullscreen mode Exit fullscreen mode

Now all that remains is to provide the depth multiplied by horizontal position.

console.log(horizontal * depth);
Enter fullscreen mode Exit fullscreen mode

Onwards!

Part Two

Part two is pretty straightforward; we just need to adjust the for loop a tiny bit. First we reset the depth and horizontal values and add in a new value: aim.

depth = 0;
horizontal = 0;
let aim = 0;
Enter fullscreen mode Exit fullscreen mode

Now "down" and "up" will modify aim rather than depth, and "forward" will both increase the horizontal position and increase depth by the given value multiplied by the current value of aim. Luckily our array is already set up :) So:

for (let i of newArr) {
  const num = parseInt(i[1]);

  if (i[0] == "forward") {
    horizontal += num;
    depth += aim * num;
  }
  if (i[0] == "up") {
    aim -= num;
  }
  if (i[0] == "down") {
    aim += num;
  }
}

console.log(horizontal * depth);
Enter fullscreen mode Exit fullscreen mode

And done!

That was fun to write up, now I'm going to have a look at other people's cool answers and feel like a noob!

Let me know if I should continue into days 3 & 4 👋

Discussion (0)