adventofcode/2022/day01/RankedCalorieCount.java
Fennel Kora d7bae4b118 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
2022-12-02 13:13:51 -05:00

32 lines
No EOL
1.2 KiB
Java

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();
}
}
}