Skip to content

Instantly share code, notes, and snippets.

@mattkirwan
Created December 1, 2022 20:08
Show Gist options
  • Save mattkirwan/f104e46ab6efd6c6e545b9c83be89942 to your computer and use it in GitHub Desktop.
Save mattkirwan/f104e46ab6efd6c6e545b9c83be89942 to your computer and use it in GitHub Desktop.
01.Java
File file = new File("./input.txt");
Scanner sc = new Scanner(file);
Integer currentElf = 0;
ArrayList<Integer> elfCalories = new ArrayList<>();
elfCalories.add(0);
while (sc.hasNextInt()) {
String line = sc.nextLine();
if (line.isBlank()) {
// New Elf
currentElf++;
elfCalories.add(0);
} else {
Integer totalCalories = elfCalories.get(currentElf) + Integer.parseInt(line);
elfCalories.set(currentElf, totalCalories);
}
}
// Part 1
System.out.println(elfCalories.stream().sorted().skip(elfCalories.size() - 1).findFirst().orElse(null));
// Part 2
System.out.println(elfCalories.stream().sorted().skip(elfCalories.size() - 3).reduce(0, (a, b) -> a + b));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment