Theme
Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows.
To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th. Although the Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way, just in case.
Problem: https://adventofcode.com/2022/day/1
Text
The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line.
Annotations
At this point, I notice I'm going to have to have special consideration for the blank line in our puzzle input.
I also realize that I'm going to want to hold a value to represent the calories each elf is carrying.
For example, suppose the Elves finish writing their items' Calories and end up with the following list:
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
Since each lift has a list of items, I want to create an elf class that as a list of calories as an instance variable.
I can also create a method to find the total calories the elf is holding.
This will make it easier to find the Elf holding the most calories.
This list represents the Calories of the food carried by five Elves:
The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.
The second Elf is carrying one food item with 4000 Calories.
The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.
The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.
The fifth Elf is carrying one food item with 10000 Calories.
In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
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.
We'll start this problem similar to Example 1 with one notable exception - instead of storing an array list of strings, we're going to create a data type called an Elf.
An Elf is defined by a list of calories with methods to display and find the total calories an elf is carrying.
In the Main class, we can test to make sure that our elf class is working properly.
If you'd like to code along, this is a good place to start.
Creating an Array List of Elves
It's now time to work with our testInput file. We're going to need to have special handling for blank lines in our file. In general our algorithm can be summarized as followed:
Create a new Elf
Read each line in the file.
If the line is not blank:
add the value to the current elf's mealList
Else
Create a new elf
The code can be represented below.
Printing our List
For debugging purposes, it's always a good idea to have a way to print your list. Let's write a method to do so.
This method labels each elf with a number and then calls the printMeals method to visualize each elf.
Cleaning Up
We've written a lot of code so far and haven't really even addressed the problem. Let's do some tidying up.
We can create a method for filling our list passing in all the necessary components through the parameter
You can see the cleaned up version on
Solving Part 1
Our objective is to find the elf carrying the maximum number of calories. At its core, this can be solved with the find-max algorithm with special consideration given to the elf's location.
The algorithm we will be using can be summarized as follows:
Assume the maximum is the first elf in the list
Go through each elf in the list and calculate their total calories
If their total calories exceed the current maximum
Change maximum to that elf's total calories and change current max elf to the current elf
It's recommended you start with Code Check Point 3 and complete this exercise on your own before reading on.
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!