mirror of
https://github.com/Zormm/Advent-Of-Code-2022.git
synced 2026-02-26 07:26:51 +01:00
Added the Entrys for the Days 1 - 7
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
ArrayList<Integer> caloriesByElf = new ArrayList<Integer>();
|
||||
int calories = 0;
|
||||
|
||||
try {
|
||||
//Part 1
|
||||
System.out.print("Part 1: ");
|
||||
RandomAccessFile file = new RandomAccessFile("input.txt", "r");
|
||||
String str;
|
||||
|
||||
while ((str = file.readLine()) != null) {
|
||||
if (str.equals("")) {
|
||||
caloriesByElf.add(calories);
|
||||
calories = 0;
|
||||
}
|
||||
else
|
||||
calories += Integer.valueOf(str);
|
||||
}
|
||||
caloriesByElf.add(calories);
|
||||
file.close();
|
||||
|
||||
caloriesByElf.sort(null);
|
||||
|
||||
System.out.println(caloriesByElf.get(caloriesByElf.size()-1));
|
||||
//Part 2
|
||||
System.out.print("Part 2: ");
|
||||
int top3CalorieCountsCombined = 0;
|
||||
|
||||
for (int i = caloriesByElf.size()-1; i > caloriesByElf.size()-4;i--) {
|
||||
top3CalorieCountsCombined += caloriesByElf.get(i);
|
||||
}
|
||||
|
||||
System.out.println(top3CalorieCountsCombined);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user