-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.java
50 lines (39 loc) · 1.28 KB
/
Day1.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class Day1 {
public static void main(String[] args) {
try {
String fileName = "files/input.txt";
Path path = Paths.get(fileName);
Files.readAllBytes(path);
List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
List<Long> sums = new ArrayList<>();
long currentSum = 0;
for(String line : allLines) {
if(line.isEmpty() || line.isBlank()) {
sums.add(currentSum);
currentSum = 0;
} else {
currentSum += Long.parseLong(line);
}
}
Optional<Long> max = sums.stream().max(Comparator.comparingLong(x -> x));
if(max.isPresent()) {
System.out.println(max);
}
// if speed was not important can be done in O(1)
sums.sort(Comparator.comparingLong(x -> x));
// unsafe if trere are lt 3 elements
System.out.println(sums.get(sums.size() - 1) + sums.get(sums.size() - 2) + sums.get(sums.size() - 3));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}