-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay2.java
40 lines (35 loc) · 1.08 KB
/
Day2.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
package com.sbaars.adventofcode.year17.days;
import com.sbaars.adventofcode.year17.Day2017;
import java.util.Arrays;
import java.util.stream.IntStream;
public class Day2 extends Day2017 {
public Day2() {
super(2);
}
public static void main(String[] args) {
new Day2().printParts();
}
@Override
public Object part1() {
return dayStream()
.map(line -> Arrays.stream(line.split("\t"))
.mapToInt(Integer::parseInt)
.summaryStatistics())
.mapToInt(stats -> stats.getMax() - stats.getMin())
.sum();
}
@Override
public Object part2() {
return dayStream()
.map(line -> Arrays.stream(line.split("\t"))
.mapToInt(Integer::parseInt)
.toArray())
.mapToInt(row -> IntStream.range(0, row.length)
.flatMap(i -> IntStream.range(i + 1, row.length)
.filter(j -> row[i] % row[j] == 0 || row[j] % row[i] == 0)
.map(j -> row[i] % row[j] == 0 ? row[i] / row[j] : row[j] / row[i]))
.findFirst()
.orElse(0))
.sum();
}
}