|
| 1 | +import static java.util.stream.Collectors.toMap; |
| 2 | +import static java.util.stream.Collectors.toSet; |
| 3 | + |
| 4 | +import com.github.pareronia.aoc.itertools.IterTools; |
| 5 | +import com.github.pareronia.aoc.solution.Sample; |
| 6 | +import com.github.pareronia.aoc.solution.Samples; |
| 7 | +import com.github.pareronia.aoc.solution.SolutionBase; |
| 8 | + |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +@SuppressWarnings({"PMD.ClassNamingConventions", "PMD.NoPackage"}) |
| 17 | +public final class AoC2025_11 extends SolutionBase<AoC2025_11.Graph, Long, Long> { |
| 18 | + |
| 19 | + private static final String YOU = "you"; |
| 20 | + private static final String SVR = "svr"; |
| 21 | + private static final String FFT = "fft"; |
| 22 | + private static final String DAC = "dac"; |
| 23 | + private static final String OUT = "out"; |
| 24 | + |
| 25 | + private AoC2025_11(final boolean debug) { |
| 26 | + super(debug); |
| 27 | + } |
| 28 | + |
| 29 | + public static AoC2025_11 create() { |
| 30 | + return new AoC2025_11(false); |
| 31 | + } |
| 32 | + |
| 33 | + public static AoC2025_11 createDebug() { |
| 34 | + return new AoC2025_11(true); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Graph parseInput(final List<String> inputs) { |
| 39 | + return Graph.fromInput(inputs); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Long solvePart1(final Graph graph) { |
| 44 | + return graph.countAllPathsAlong(List.of(YOU, OUT)); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Long solvePart2(final Graph graph) { |
| 49 | + return Stream.of(List.of(SVR, DAC, FFT, OUT), List.of(SVR, FFT, DAC, OUT)) |
| 50 | + .mapToLong(graph::countAllPathsAlong) |
| 51 | + .sum(); |
| 52 | + } |
| 53 | + |
| 54 | + @Samples({ |
| 55 | + @Sample(method = "part1", input = TEST1, expected = "5"), |
| 56 | + @Sample(method = "part2", input = TEST2, expected = "2"), |
| 57 | + }) |
| 58 | + public static void main(final String[] args) throws Exception { |
| 59 | + create().run(); |
| 60 | + } |
| 61 | + |
| 62 | + private static final String TEST1 = |
| 63 | + """ |
| 64 | + aaa: you hhh |
| 65 | + you: bbb ccc |
| 66 | + bbb: ddd eee |
| 67 | + ccc: ddd eee fff |
| 68 | + ddd: ggg |
| 69 | + eee: out |
| 70 | + fff: out |
| 71 | + ggg: out |
| 72 | + hhh: ccc fff iii |
| 73 | + iii: out |
| 74 | + """; |
| 75 | + private static final String TEST2 = |
| 76 | + """ |
| 77 | + svr: aaa bbb |
| 78 | + aaa: fft |
| 79 | + fft: ccc |
| 80 | + bbb: tty |
| 81 | + tty: ccc |
| 82 | + ccc: ddd eee |
| 83 | + ddd: hub |
| 84 | + hub: fff |
| 85 | + eee: dac |
| 86 | + dac: fff |
| 87 | + fff: ggg hhh |
| 88 | + ggg: out |
| 89 | + hhh: out |
| 90 | + """; |
| 91 | + |
| 92 | + record Graph(Map<String, Set<String>> edges) { |
| 93 | + |
| 94 | + public static Graph fromInput(final List<String> inputs) { |
| 95 | + final Map<String, Set<String>> edges = |
| 96 | + inputs.stream() |
| 97 | + .collect( |
| 98 | + toMap( |
| 99 | + line -> line.split(": ")[0], |
| 100 | + line -> |
| 101 | + Arrays.stream(line.split(": ")[1].split(" ")) |
| 102 | + .collect(toSet()))); |
| 103 | + return new Graph(edges); |
| 104 | + } |
| 105 | + |
| 106 | + private long countAllPaths(final String start, final String end) { |
| 107 | + class DFS { |
| 108 | + private final Map<String, Long> cache; |
| 109 | + |
| 110 | + protected DFS() { |
| 111 | + this.cache = new HashMap<>(); |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressWarnings("PMD.OnlyOneReturn") |
| 115 | + public long dfs(final String node) { |
| 116 | + if (this.cache.containsKey(node)) { |
| 117 | + return this.cache.get(node); |
| 118 | + } |
| 119 | + if (end.equals(node)) { |
| 120 | + return 1; |
| 121 | + } |
| 122 | + final long ans = |
| 123 | + Graph.this.edges.getOrDefault(node, Set.of()).stream() |
| 124 | + .mapToLong(this::dfs) |
| 125 | + .sum(); |
| 126 | + this.cache.put(node, ans); |
| 127 | + return ans; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return new DFS().dfs(start); |
| 132 | + } |
| 133 | + |
| 134 | + public long countAllPathsAlong(final List<String> along) { |
| 135 | + return IterTools.pairwise(along).stream() |
| 136 | + .mapToLong(pair -> this.countAllPaths(pair.first(), pair.second())) |
| 137 | + .reduce((acc, cnt) -> acc * cnt) |
| 138 | + .getAsLong(); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments