-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartTwo.java
51 lines (44 loc) · 1.55 KB
/
PartTwo.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
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* --- Day 1: Historian Hysteria --- --- Part Two ---
*
*
* https://adventofcode.com/2024/day/1
**/
public class PartTwo {
public static void main(String[] args) throws Exception {
Path path = Path.of("2024/days/01/src/input.txt");
List<Integer> leftList = new ArrayList<>();
List<Integer> rightList = new ArrayList<>();
int similarityScore = 0;
try (var lines = Files.lines(path)) {
lines.forEach(line -> {
String[] parts = line.split("\\s+");
int leftNumber = Integer.parseInt(parts[0]);
int rightNumber = Integer.parseInt(parts[1]);
leftList.add(leftNumber);
rightList.add(rightNumber);
});
}
leftList.sort(Integer::compareTo);
rightList.sort(Integer::compareTo);
Map<Integer, Integer> countMap = new HashMap<>();
for (Integer item : rightList) {
countMap.put(item,
countMap.getOrDefault(item, 0) + 1);
}
int listSize = leftList.size();
for (int index = 0; index < listSize; index++) {
int leftNumber = leftList.get(index);
int amountOfOccurences = countMap.getOrDefault(
leftNumber, 0);
similarityScore += Math.abs(leftNumber * amountOfOccurences);
}
System.out.println(similarityScore);
}
}