-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.java
105 lines (81 loc) · 3.16 KB
/
solution.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class solution {
private final Map<Integer, List<Connection>> connections;
private solution(Map<Integer, List<Connection>> connections) {
this.connections = connections;
}
private int part1() {
return getMaxScore(0, new HashSet<>());
}
private int getMaxScore(int startingPoint, HashSet<Connection> used) {
int best = 0;
for (Connection c : connections.get(startingPoint)) {
if (used.contains(c)) {
continue;
}
used.add(c);
best = Math.max(best, getMaxScore(c.getOther(startingPoint), used) + c.score());
used.remove(c);
}
return best;
}
private int part2() {
return getMaxLongest(0, new HashSet<>()).getKey();
}
private Map.Entry<Integer, Integer> getMaxLongest(int startingPoint, HashSet<Connection> used) {
int strongest = 0;
int longest = 0;
for (Connection c : connections.get(startingPoint)) {
if (used.contains(c)) {
continue;
}
used.add(c);
Map.Entry<Integer, Integer> res = getMaxLongest(c.getOther(startingPoint), used);
used.remove(c);
if (longest < res.getValue() + 1) {
longest = res.getValue() + 1;
strongest = res.getKey() + c.score();
} else if (longest == res.getValue() + 1) {
strongest = Math.max(res.getKey() + c.score(), strongest);
}
}
return new AbstractMap.SimpleEntry<>(strongest, longest);
}
public static void main(String[] args) throws IOException {
solution s = new solution(readInput());
System.out.println(s.part1());
System.out.println(s.part2());
}
private static Map<Integer, List<Connection>> readInput() throws IOException {
final Map<Integer, List<Connection>> connections = new HashMap<>();
try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
in.lines()
.map(s -> s.trim().split("/"))
.map(Connection::new)
.forEach(c -> {
connections.computeIfAbsent(c.getValue(), a -> new ArrayList<>())
.add(c);
connections.computeIfAbsent(c.getKey(), a -> new ArrayList<>())
.add(c);
});
}
return Collections.unmodifiableMap(connections);
}
private static class Connection extends AbstractMap.SimpleImmutableEntry<Integer, Integer> {
Connection(String[] parts) {
this(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]));
}
Connection(Integer integer, Integer integer2) {
super(Math.min(integer, integer2), Math.max(integer, integer2));
}
int getOther(int first) {
return getKey() == first ? getValue() : getKey();
}
int score() {
return getKey() + getValue();
}
}
}