-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay5.java
86 lines (71 loc) · 2.86 KB
/
Day5.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.sbaars.adventofcode.year24.days;
import com.sbaars.adventofcode.year24.Day2024;
import com.sbaars.adventofcode.common.Graph;
import com.sbaars.adventofcode.common.map.ListMap;
import com.sbaars.adventofcode.common.Pair;
import static com.sbaars.adventofcode.common.map.ListMap.toListMap;
import java.util.*;
import java.util.stream.Collectors;
public class Day5 extends Day2024 {
public Day5() {
super(5);
}
public static void main(String[] args) {
new Day5().printParts();
}
@Override
public Object part1() {
return calculateMiddlePages(true);
}
@Override
public Object part2() {
return calculateMiddlePages(false);
}
private Object calculateMiddlePages(boolean onlyValid) {
String[] sections = day().split("\n\n");
List<Pair<Integer, Integer>> orderPairs = Arrays.stream(sections[0].split("\n"))
.filter(rule -> !rule.isEmpty())
.map(rule -> {
String[] parts = rule.split("\\|");
return new Pair<>(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
})
.toList();
return Arrays.stream(sections[1].split("\n"))
.filter(u -> !u.isEmpty())
.map(update -> Arrays.stream(update.split(",")).map(Integer::parseInt).toList())
.filter(pages -> isValidUpdate(pages, orderPairs) == onlyValid)
.mapToInt(pages -> onlyValid ? pages.get(pages.size() / 2) : calculateMiddlePage(orderPairs, pages))
.sum();
}
private int calculateMiddlePage(List<Pair<Integer, Integer>> orderPairs, List<Integer> pages) {
Set<Integer> nodes = new HashSet<>(pages);
var edges = orderPairs.stream()
.filter(pair -> nodes.contains(pair.a()) && nodes.contains(pair.b()))
.collect(toListMap(Pair::a, Pair::b));
return topologicalSort(nodes, edges).get(nodes.size() / 2);
}
private boolean isValidUpdate(List<Integer> pages, List<Pair<Integer, Integer>> orderPairs) {
Map<Integer, Integer> pageToIndex = new HashMap<>();
for (int i = 0; i < pages.size(); i++) {
pageToIndex.put(pages.get(i), i);
}
return orderPairs.stream().noneMatch(pair ->
pageToIndex.containsKey(pair.a()) && pageToIndex.containsKey(pair.b()) &&
pageToIndex.get(pair.a()) >= pageToIndex.get(pair.b())
);
}
private List<Integer> topologicalSort(Set<Integer> nodes, ListMap<Integer, Integer> edges) {
Graph<Integer> graph = new Graph<>(edges);
List<Integer> ordering = new ArrayList<>();
Queue<Graph.Node<Integer>> queue = graph.stream().filter(node -> node.parents().isEmpty()).collect(Collectors.toCollection(LinkedList::new));
while (!queue.isEmpty()) {
Graph.Node<Integer> node = queue.poll();
ordering.add(node.data());
node.children().forEach(neighbor -> {
neighbor.parents().remove(node);
if (neighbor.parents().isEmpty()) queue.add(neighbor);
});
}
return ordering;
}
}