-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.java
More file actions
25 lines (20 loc) · 747 Bytes
/
Day1.java
File metadata and controls
25 lines (20 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
class Day1 {
public static void main(String ... args) throws Exception {
var cals = new LinkedList<>(List.of(0));
Files.readAllLines(Path.of("../input/day1.txt")).forEach(line -> {
if (line.isBlank()) {
cals.add(0);
} else {
cals.add(cals.removeLast() + Integer.parseInt(line));
}
});
Collections.sort(cals, (a,b) -> b-a); // reverse sort
System.out.printf("part 1: %d\n", cals.getFirst());
System.out.printf("part 2: %d\n", cals.stream().limit(3).mapToInt(i -> i).sum());
}
}