-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.java
26 lines (24 loc) · 898 Bytes
/
day01.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
import java.util.*;
import static java.lang.Integer.parseInt;
import static java.lang.Math.abs;
import static java.lang.System.in;
import static java.lang.System.out;
public class day01 {
public static void main(String[] args) {
var left = new ArrayList<Integer>();
var right = new ArrayList<Integer>();
new Scanner(in).findAll("(\\d+)\\s+(\\d+)").forEach(pair -> {
left.add(parseInt(pair.group(1)));
right.add(parseInt(pair.group(2)));
});
left.sort(Integer::compareTo);
right.sort(Integer::compareTo);
int total = 0, similarity = 0;
for (int i = 0; i < left.size(); i++) {
int l = left.get(i);
total += abs(l - right.get(i));
similarity += (int) (l * right.stream().filter(n -> n.equals(l)).count());
}
out.println(total + " " + similarity);
}
}