Created
December 1, 2022 20:08
-
-
Save mattkirwan/f104e46ab6efd6c6e545b9c83be89942 to your computer and use it in GitHub Desktop.
01.Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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