-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdventOfCode11Golf.java
28 lines (26 loc) · 1.19 KB
/
AdventOfCode11Golf.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
import static java.util.Map.entry;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingLong;
void main() {
var input = "5910927 0 1 47 261223 94788 545 7771";
println(Stream.iterate(Arrays.stream(input.split(" "))
.mapToLong(Long::parseLong).boxed().collect(groupingBy(i -> i, counting())),
m -> m.entrySet().stream()
.flatMap(e -> {
var s = e.getKey();
var count = e.getValue();
if (s == 0) {
return Stream.of(entry(1L, count));
}
var length = 1 + (int) Math.log10(s);
if (length % 2 == 0) {
var divisor = (long) Math.pow(10, length / 2);
return Stream.of(entry(s / divisor, count), entry(s % divisor, count));
}
return Stream.of(entry(Math.multiplyExact(2024L, s), count));
})
.collect(groupingBy(Map.Entry::getKey, summingLong(Map.Entry::getValue))))
.limit(76)
.reduce((_, v) -> v).orElseThrow().values().stream().mapToLong(v -> v).sum());
}