|
5 | 5 | import java.util.stream.Collectors;
|
6 | 6 |
|
7 | 7 | public class Day24 extends Day2017 {
|
8 |
| - private static class Component { |
9 |
| - final int port1, port2; |
10 |
| - |
11 |
| - Component(int port1, int port2) { |
12 |
| - this.port1 = port1; |
13 |
| - this.port2 = port2; |
14 |
| - } |
15 |
| - |
16 |
| - int getOtherPort(int port) { |
17 |
| - return port == port1 ? port2 : port1; |
18 |
| - } |
19 |
| - |
20 |
| - int getStrength() { |
21 |
| - return port1 + port2; |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - private final List<Component> components = new ArrayList<>(); |
26 |
| - |
27 |
| - public Day24() { |
28 |
| - super(24); |
29 |
| - for (String line : dayStream().toList()) { |
30 |
| - String[] ports = line.split("/"); |
31 |
| - components.add(new Component(Integer.parseInt(ports[0]), Integer.parseInt(ports[1]))); |
32 |
| - } |
33 |
| - } |
34 |
| - |
35 |
| - public static void main(String[] args) { |
36 |
| - new Day24().printParts(); |
37 |
| - } |
38 |
| - |
39 |
| - private int findStrongestBridge(int currentPort, Set<Component> used) { |
40 |
| - int maxStrength = 0; |
41 |
| - |
42 |
| - for (Component c : components) { |
43 |
| - if (!used.contains(c) && (c.port1 == currentPort || c.port2 == currentPort)) { |
44 |
| - used.add(c); |
45 |
| - int strength = c.getStrength() + findStrongestBridge(c.getOtherPort(currentPort), used); |
46 |
| - maxStrength = Math.max(maxStrength, strength); |
47 |
| - used.remove(c); |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - return maxStrength; |
52 |
| - } |
53 |
| - |
54 |
| - @Override |
55 |
| - public Object part1() { |
56 |
| - return findStrongestBridge(0, new HashSet<>()); |
57 |
| - } |
58 |
| - |
59 |
| - @Override |
60 |
| - public Object part2() { |
61 |
| - return 0; |
62 |
| - } |
| 8 | + private record Component(int port1, int port2) { |
| 9 | + boolean hasPort(int port) { |
| 10 | + return port1 == port || port2 == port; |
| 11 | + } |
| 12 | + |
| 13 | + int getOtherPort(int port) { |
| 14 | + return port == port1 ? port2 : port1; |
| 15 | + } |
| 16 | + |
| 17 | + int strength() { |
| 18 | + return port1 + port2; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + private record Bridge(List<Component> components, int lastPort) { |
| 23 | + int strength() { |
| 24 | + return components.stream().mapToInt(Component::strength).sum(); |
| 25 | + } |
| 26 | + |
| 27 | + int length() { |
| 28 | + return components.size(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public Day24() { |
| 33 | + super(24); |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + new Day24().printParts(); |
| 38 | + } |
| 39 | + |
| 40 | + private List<Component> parseComponents() { |
| 41 | + return Arrays.stream(dayStrings()) |
| 42 | + .map(line -> { |
| 43 | + String[] parts = line.split("/"); |
| 44 | + return new Component(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); |
| 45 | + }) |
| 46 | + .collect(Collectors.toList()); |
| 47 | + } |
| 48 | + |
| 49 | + private List<Bridge> buildBridges(Bridge currentBridge, List<Component> availableComponents) { |
| 50 | + List<Bridge> bridges = new ArrayList<>(); |
| 51 | + bridges.add(currentBridge); |
| 52 | + |
| 53 | + for (Component component : availableComponents) { |
| 54 | + if (component.hasPort(currentBridge.lastPort())) { |
| 55 | + List<Component> remainingComponents = new ArrayList<>(availableComponents); |
| 56 | + remainingComponents.remove(component); |
| 57 | + |
| 58 | + List<Component> newBridgeComponents = new ArrayList<>(currentBridge.components()); |
| 59 | + newBridgeComponents.add(component); |
| 60 | + |
| 61 | + Bridge newBridge = new Bridge(newBridgeComponents, component.getOtherPort(currentBridge.lastPort())); |
| 62 | + bridges.addAll(buildBridges(newBridge, remainingComponents)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return bridges; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Object part1() { |
| 71 | + List<Component> components = parseComponents(); |
| 72 | + List<Bridge> bridges = buildBridges(new Bridge(new ArrayList<>(), 0), components); |
| 73 | + return bridges.stream() |
| 74 | + .mapToInt(Bridge::strength) |
| 75 | + .max() |
| 76 | + .orElse(0); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Object part2() { |
| 81 | + List<Component> components = parseComponents(); |
| 82 | + List<Bridge> bridges = buildBridges(new Bridge(new ArrayList<>(), 0), components); |
| 83 | + |
| 84 | + int maxLength = bridges.stream() |
| 85 | + .mapToInt(Bridge::length) |
| 86 | + .max() |
| 87 | + .orElse(0); |
| 88 | + |
| 89 | + return bridges.stream() |
| 90 | + .filter(b -> b.length() == maxLength) |
| 91 | + .mapToInt(Bridge::strength) |
| 92 | + .max() |
| 93 | + .orElse(0); |
| 94 | + } |
63 | 95 | }
|
0 commit comments