A new day dawns, a new challenger approaches. It is time for Advent of Code Day 2!
Today's problem is pretty straightforward. The puzzle comes with a set of instructions, and we need to calculate the position of our submarine after we follow these instructions.
The input, as always, is provided in a string file, where each line is formatted as: command units
.
The interpretation of input differs between parts one and two.
Part 1
In Part 1 we are given a set of instructions, and we need to calculate the final position of our submarine if we follow these instructions. There are three instruction "tokens", forward
, up
, down
.
- forward: Increases the horizontal position
- up: Decreases the vertical position
- down: Increases the vertical position
Each token is followed by a number, the units of movement to a given direction.
Let's start building our solution. First, we are going to design an algorithm to solve the puzzle, and then convert it to C# code.
We have a string array Instructions containing all movement instructions. Our goal is to calculate the product of the final X position times the final Y position.
We will iterate through the Instructions sequence and retrieve each instruction. Since the instruction format is command units
, we need to tokenize it to command
and units
.
If the command token is forward
, we will increase the XPosition by units
. If the command token is up
, we will decrease the YPosition by units
. Finally, if the command token is down
, we will increase the YPosition by units
.
The last step is to calculate the product of XPosition times YPosition.
SUBMARINE_MOVEMENT(Instructions)
INPUT: A list of string instructions
OUTPUT: The product of the final horizontal position times the final vertical position
XPosition = 0
YPosition = 0
FOREACH(Instruction in Instructions)
(instruction, units) = TOKENIZE(instruction)
SWITCH instruction
CASE "forward"
XPosition = XPosition + units
CASE "up"
YPosition = YPosition - units
CASE "down"
YPosition = YPosition + units
END SWITCH
END FOREACH
RETURN XPosition * YPosition
With the algorithm figured out, let's begin the C# implementation.
int xPosition = 0;
int yPosition = 0;
foreach(string instruction in data)
{
string[] commands = instruction.Split(" ");
switch(commands[0])
{
case "forward":
xPosition += int.Parse(commands[1]);
break;
case "up":
yPosition -= int.Parse(commands[1]);
break;
case "down":
yPosition += int.Parse(commands[1]);
break;
}
}
return xPosition * yPosition;
How it Works
The solution is pretty clear-cut. First, the instruction is split on whitespace, creating a [command, unit]
array. Then, a switch statement parses the command
argument and performs the appropriate position calculations.
Part 2
Part 2 introduces a twist in the calculations. Instead of changing the vertical position, up
and down
commands change the pitch angle of the submarine. Since only the pitch angle is affected, the forward
command changes the vertical and horizontal positioning. Now forward
adds X units on the horizontal position and pitch_angle
* X units on the vertical.
To address that, we need to perform some minor adjustments to our calculating function:
int xPosition = 0;
int yPosition = 0;
int aim = 0;
foreach (string instruction in data)
{
string[] commands = instruction.Split(" ");
switch (commands[0])
{
case "forward":
xPosition += int.Parse(commands[1]);
yPosition += int.Parse(commands[1]) * aim;
break;
case "up":
aim -= int.Parse(commands[1]);
break;
case "down":
aim += int.Parse(commands[1]);
break;
}
}
return xPosition * yPosition
How it works
Only some minor adjustments are needed to calculate the answer to Part2.
First, I've introduced a new integer variable aim
to store the pitch angle of the submarine.
The up
and down
commands, no longer affect the ship's vertical position, adjusting the pitch angle instead.
Finally, the forward
command, calculates the horizontal movement of the ship by adding X units to the horizontal position, as well as calculating the vertical position. The vertical position is the product of the traversal angle times X units of movement.
That's all folks!
Day02 puzzle, is a nice and clear cut introductory puzzle for AoC, without any difficult part requiring attention to details.
I hope I could provide y'all with a more in-depth explanation of how my solutions work.
You can find this solution and others in my Github Repo.
Hope you had fun and see you around for the next episode of Advent of Code 2021!
Top comments (0)