-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.java
More file actions
23 lines (21 loc) · 884 Bytes
/
Day03.java
File metadata and controls
23 lines (21 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void main() {
var jolts = readInput();
IO.println(jolts.stream().mapToLong(jolt -> findLargestJoltage(jolt, 2)).sum());
IO.println(jolts.stream().mapToLong(jolt -> findLargestJoltage(jolt, 12)).sum());
}
long findLargestJoltage(List<Integer> jolt, int digits) {
int from = -1;
long maxJoltage = 0L;
for (int i = 0; i < digits; ++i) {
from = IntStream.range(from + 1, jolt.size() - (digits - i - 1)).boxed().max(Comparator.comparingInt(jolt::get)).orElseThrow();
maxJoltage = maxJoltage * 10 + jolt.get(from);
}
return maxJoltage;
}
List<List<Integer>> readInput() {
try (Stream<String> lines = Files.lines(Path.of("input_03.txt"))) {
return lines.map(s -> s.chars().map(Character::getNumericValue).boxed().collect(Collectors.toList())).toList();
} catch (IOException e) {
throw new RuntimeException(e);
}
}