Theme
Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!
Before you know it, you're inside a submarine the Elves keep ready for situations like this.
Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th.
Problem: https://adventofcode.com/2021/day/2
Problem Text:
Now, you need to figure out how to pilot this thing.
It seems like the submarine can take a series of commands like forward 1, down 2, or up 3:
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.
Note that since you're on a submarine, down and up affect your depth, and so they have the opposite result of what you might expect.
Annotations
At this point, I notice that I will likely need to keep track of two variables for position and depth.
The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example:
forward 5
down 5
forward 8
up 3
down 8
forward 2
Here we can see what our input is going to look like.
One thing to notice is that each line contains
A direction
A value
We'll need to parse this information in order to work with it.
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?
Solving Part 1
Before we start solving this problem, we need to set up our environment. I'll be using an online IDE that you can use as well. But feel free to use your own IDE of choice.
This starter code is provided to assist in the "setting up" portion of the problem. This involves creating files to hold the input (given on the advent of code website), and how to read it into an array list. This is how most problems will start.
If you'd like to work along, use the link below. There will be more links provided with various stages of completion for you to check your work or catch-up if needed.
It's important to periodically test your solutions as you work to make sure your code is working as intended.
It's also important to have a plan for how you're going to tackle the problem. There are two major lifts in the problem:
Separate the input into direction and number
Create a variable for depth and position, using the direction and number from the input.
Separating the Input
Right now, we have values like "forward 5" in our list. What we'd like is a way to convert that into separate values "forward" and "5".
We never know what "part 2" is going to hold for the challenge, so the more reusable and modular you can make your code, the easier it is to adapt to the challenges.
Let's add these two methods to our code.
Note that the Integer.parseInt method converts a numeric value stored as a string into an integer.
We can test our methods in main to see if they're working correctly.
This would check the parsing for the first and second items in our input list.
Solving the Problem
Now that we're able to work with our input data, it's time to solve the problem.
We'll be using a loop to go through our list and process each input value. Then, we'll update a position and depth variable accordingly.
The for-each loop shown traverses the list of commands and breaks them down into their direction and value.
It is recommended you start with code checkpoint 3 and work on the problem on your own before reading on.
In order to get credit on Advent of Code, you must use your individual input data for your solution.
After entering your number, you will be told whether it is correct or incorrect.
Part 2
You've now unlocked part 2. In this example, we shouldn't have to change to much about our approach just some minor tweaks. In some cases, your approach may need to change drastically. It's up to you to decide what's best. Good Luck!