Advent of Code 2021: Day 2

Search for a command to run...

No comments yet. Be the first to comment.
In this series we solve programming challenges from the Advent of Code 2021 using JavaScript.
This blog post is third in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 3. All solutions are in this GitHub repository. Solving Part One The input file consists of binary numbers on separate lin...
This blog post is fourteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 14. This challenge was about optimizing an algorithm that generates exponentially larger and larger strings. I found ...

This blog post is thirteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 13. All solutions are in this GitHub repository. Solving Part One Given a list of x, y coordinates representing dots ...

This blog post is twelfth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described on Day 12. All solutions are in this GitHub repository. Solving Part One Given a set of connections between underground caves, ...

This blog post is eleventh in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 11. This day had many similarities to Day 9, and I solved the challenge using similar methods. All solutions are in thi...

This blog post is the tenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 10. All solutions are in this GitHub repository. Solving Part One Given a set of lines with opening and closing bracke...

On this page
This blog post is second in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 2: Dive!.
All solutions are in this GitHub repository.
The goal is to find the horizontal position and depth of a moving submarine based on a set of given instructions.
According to the problem the initial depth and horizontal positions are 0.
let depth = 0
let horizontalPosition = 0
We can read the set of instructions using the readInput utility created on Day 1. Then we can split the data line by line.
const commands = data.split('\n')
Each line contains a command type and a number of steps. To use these, we can split the line by the space character, then convert the steps to a number.
const [type, stepsAsString] = command.split(' ')
const steps = Number(stepsAsString)
Finally, we can calculate the depth and horizontal positions based on the command type and steps to move according to the rules described in the challenge.
switch (type) {
case 'forward':
horizontalPosition += steps
break
case 'down':
depth += steps
break
case 'up':
depth -= steps
break
default:
break
}
The final solution:
import { URL, fileURLToPath } from 'url'
import { readInput } from '../utils/readInput.js'
const solve = (data) => {
const commands = data.split('\n')
let depth = 0
let horizontalPosition = 0
commands.forEach((command) => {
const [type, stepsAsString] = command.split(' ')
const steps = Number(stepsAsString)
switch (type) {
case 'forward':
horizontalPosition += steps
break
case 'down':
depth += steps
break
case 'up':
depth -= steps
break
default:
break
}
})
return depth * horizontalPosition
}
const inputPath = fileURLToPath(new URL('./input.txt', import.meta.url))
const data = await readInput(inputPath)
console.log(solve(data))
The second part of the problem requires calculating depth and horizontal position in a slightly different way using new rules and an aim value.
let aim = 0
The new rules can be written in JavaScript as follows:
switch (type) {
case 'forward':
horizontalPosition += steps
depth += aim * steps
break
case 'down':
aim += steps
break
case 'up':
aim -= steps
break
default:
break
}
The final solution for part two is on my GitHub repository.
Day 2 was about calculating values such as horizontal position and depth of a moving submarine based on a set of given commands. I had fun solving the challenge and the utility functions added on Day 1 helped solve the second challenge somewhat faster.