Adds RankedCalorieCount for Day 1 Part 2 question, getting sum of top three calorie-bearing elves
Changes to be committed: new file: 2022/day01/RankedCalorieCount.java modified: 2022/day01/day01_algo.docx
This commit is contained in:
parent
c381113042
commit
d7bae4b118
2 changed files with 32 additions and 0 deletions
32
2022/day01/RankedCalorieCount.java
Normal file
32
2022/day01/RankedCalorieCount.java
Normal file
|
@ -0,0 +1,32 @@
|
|||
import java.nio.file.Paths;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
public class RankedCalorieCount {
|
||||
public static void main (String[] args) {
|
||||
try (Scanner inputReader = new Scanner(Paths.get("input.txt"))) {
|
||||
ArrayList<Integer> calsList = new ArrayList<Integer>();
|
||||
while (inputReader.hasNext()) {
|
||||
int currentCals = 0;
|
||||
String inLine = new String();
|
||||
do {
|
||||
inLine = inputReader.nextLine();
|
||||
if (!inLine.isEmpty()) {
|
||||
currentCals += Integer.parseInt(inLine);
|
||||
}
|
||||
} while (!inLine.isEmpty() && inputReader.hasNext());
|
||||
calsList.add(currentCals);
|
||||
}
|
||||
|
||||
Collections.sort(calsList);
|
||||
Collections.reverse(calsList);
|
||||
int topThreeSum = calsList.get(0)+calsList.get(1)+calsList.get(2);
|
||||
System.out.printf("The sum of calories by the top three is: %d%n", topThreeSum);
|
||||
} catch (IOException e) {
|
||||
System.out.println("IOException");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in a new issue