-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle1.java
68 lines (62 loc) · 2.16 KB
/
Puzzle1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package advent2024;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Math.absExact;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Éamonn McManus
*/
public class Puzzle1 {
private static final String SAMPLE =
"""
3 4
4 3
2 5
1 3
3 9
3 3
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample", () -> new StringReader(SAMPLE),
"problem", () -> new InputStreamReader(Puzzle1.class.getResourceAsStream("puzzle1.txt")));
private static final Pattern LINE_PATTERN = Pattern.compile("(\\d+)\\s+(\\d+)");
public static void main(String[] args) throws Exception {
for (var entry : INPUT_PRODUCERS.entrySet()) {
String name = entry.getKey();
try (Reader r = entry.getValue().call()) {
List<String> lines = CharStreams.readLines(r);
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (String line : lines) {
Matcher matcher = LINE_PATTERN.matcher(line);
checkArgument(matcher.matches());
left.add(Integer.valueOf(matcher.group(1)));
right.add(Integer.valueOf(matcher.group(2)));
}
Collections.sort(left);
Collections.sort(right);
var rightCounts = ImmutableMultiset.copyOf(right);
int total = 0;
long similarity = 0;
for (int i = 0; i < left.size(); i++) {
total += absExact(left.get(i) - right.get(i));
similarity += left.get(i) * rightCounts.count(left.get(i));
}
System.out.println("Total for " + name + " is " + total);
System.out.println("Similarity for " + name + " is " + similarity);
}
}
}
}