Advent Of Code 2021 — Dive! — Puzzle 2

Xavier Jouvenot
4 min readJan 10, 2022

Hello ! I’m Xavier Jouvenot and here is the 2nd part of a long series on Advent Of Code 2021.

For this new post, we are going to solve the problem from the 2nd December 2021, named “Dive!”. The solution I will propose in C++, but the reasoning can be applied to other languages.

Self promotion: You can find other articles on computer science and programming on my website 😉

Part 1

The Problem

The full version of this problem can be found directly on the Advent of Code website, I will only describe the essence of the problem here:

After using the sonar to identify our surroundings, during the Day 1, we now want to move with our submarine. In order to do so, the submarine can understand 3 kind of instructions:

  • forward X increases the horizontal position by X units.
  • down X increases the depth by X units.
  • up X decreases the depth by X units.

Since the submarine already has a sets of instruction programmed, we need to know where we are going, and to figure out at which depth and horizontal position we are going to end up. To have only one answer to give to the problem (and to the website), we have to multiply the 2 values we are going to found and the result will be our answer.

Solution

--

--