MENU

December 2, 2021

2021 Advent of Code Day 2 Solution

Introduction

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language in numerous different ways. In this post I will be outlining the solutions I wrote for the Day 2 problems, called "Dive!", written in C++ so that others may learn from this post based on my code as well as find help in creating their own solutions to the issue and grasping the concepts of what the code is doing to reach the end goal.

Part 1

The problem states:

forward 5
down 5
forward 8
up 3
down 8
forward 2

Your horizontal position and depth both start at 0. The steps above would then modify them as follows:

  • forward 5 adds 5 to your horizontal position, a total of 5.
  • down 5 adds 5 to your depth, resulting in a value of 5.
  • forward 8 adds 8 to your horizontal position, a total of 13.
  • up 3 decreases your depth by 3, resulting in a value of 2.
  • down 8 adds 8 to your depth, resulting in a value of 10.
  • forward 2 adds 2 to your horizontal position, a total of 15.

After following these instructions, you would have a horizontal position of 15 and a depth of 10. (Multiplying these together produces 150.)

Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

The input provided by Advent of Code is different for each participant in order to keep the solution unique for each person. It is roughly 1000 lines of text mimicking the above example with each line having a command and an integer effecting a variable. For this solution fstream was utilized after putting the text into a .txt file that can be reached by the program.

Here is the code:


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream fin("text.txt"); //opening the file

int main() {
  int amount, depth = 0, position = 0;
  string command;
    while (fin >> command >> amount){ //reading each line into variables
      if(command=="forward"){
	position += amount;
      }else if(command=="up"){
	depth -= amount;
      }else if(command=="down"){
	depth += amount;
      }else{
	cout << "error?" << endl;
      }
    }
  cout << "position: " << position << endl; //outputs
  cout << "depth: " << depth << endl;
  cout << "position * depth: " << (position * depth) << endl; //desired result
} 

Part 2

Part 2 of the problem states:

In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:

  • down X increases your aim by X units.
  • up X decreases your aim by X units.
  • forward X does two things:
    • It increases your horizontal position by X units.
    • It increases your depth by your aim multiplied by X.

Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.

Now, the above example does something different:

  • forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
  • down 5 adds 5 to your aim, resulting in a value of 5.
  • forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
  • up 3 decreases your aim by 3, resulting in a value of 2.
  • down 8 adds 8 to your aim, resulting in a value of 10.
  • forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.

After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)

Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

The second part of the problem utilizes essentially the same program with a minor difference of adding a third variable of aim to be multiplied by the amount entered and added into depth with the desired result being the same as the previous question of position multiplied by depth.

Here is the code:


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream fin("text.txt");

int main() {
  int amount, depth = 0, position = 0, aim = 0;
  string command;
    while (fin >> command >> amount){ //reading each line into variables
      if(command=="forward"){
	position += amount;
	depth = depth + (aim * amount);
      }else if(command=="up"){
	aim -= amount;
      }else if(command=="down"){
	aim += amount;
      }else{
	cout << "error?" << endl;
      }
    }
  cout << "position: " << position << endl; //outputs
  cout << "depth: " << depth << endl;
  cout << "position * depth: " << (position * depth) << endl; //desired result
} 

Conclusion

Advent of Code is a very fun event that only happens once a year similar to typical advent calendars. Having an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language is a fun little challenge that brings a lot of the coding community together in various different ways. Users can have private leaderboards to compare their scores with their friends as well as possibly making their way onto the global leaderboards. The Day 2 problems "Dive!" were rather simple and can be solved in a variety of methods and this was simply mine utilizing a simple program of reading a file and manipulating variables. I hope that you learned from this post of my code and can help in creating your own solution to the issue and grasp the concepts of what the code is doing to reach the end goal.

I will likely be continuing this series for the remainder of the 2021 Advent of Code event.

Loading

Zach
Author
About Me
Privacy Policy
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram