diff --git a/README.md b/README.md index adcdcb2e..352d27d6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ | | 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 | | ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | python3 | [✓](src/main/python/AoC2023_01.py) | [✓](src/main/python/AoC2023_02.py) | [✓](src/main/python/AoC2023_03.py) | [✓](src/main/python/AoC2023_04.py) | [✓](src/main/python/AoC2023_05.py) | [✓](src/main/python/AoC2023_06.py) | [✓](src/main/python/AoC2023_07.py) | [✓](src/main/python/AoC2023_08.py) | | | | | | | | | | | | | | | | | | -| java | [✓](src/main/java/AoC2023_01.java) | [✓](src/main/java/AoC2023_02.java) | [✓](src/main/java/AoC2023_03.java) | [✓](src/main/java/AoC2023_04.java) | | [✓](src/main/java/AoC2023_06.java) | | | | | | | | | | | | | | | | | | | | +| java | [✓](src/main/java/AoC2023_01.java) | [✓](src/main/java/AoC2023_02.java) | [✓](src/main/java/AoC2023_03.java) | [✓](src/main/java/AoC2023_04.java) | | [✓](src/main/java/AoC2023_06.java) | | [✓](src/main/java/AoC2023_08.java) | | | | | | | | | | | | | | | | | | | bash | | | | | | | | | | | | | | | | | | | | | | | | | | | c++ | | | | | | | | | | | | | | | | | | | | | | | | | | | julia | | | | | | | | | | | | | | | | | | | | | | | | | | diff --git a/src/main/java/AoC2023_08.java b/src/main/java/AoC2023_08.java new file mode 100644 index 00000000..3b0e2cde --- /dev/null +++ b/src/main/java/AoC2023_08.java @@ -0,0 +1,123 @@ +import static com.github.pareronia.aoc.Utils.last; + +import java.math.BigInteger; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import com.github.pareronia.aoc.IterTools; +import com.github.pareronia.aoc.StringOps; +import com.github.pareronia.aoc.Utils; +import com.github.pareronia.aoc.solution.Sample; +import com.github.pareronia.aoc.solution.Samples; +import com.github.pareronia.aoc.solution.SolutionBase; + +public final class AoC2023_08 + extends SolutionBase { + + private AoC2023_08(final boolean debug) { + super(debug); + } + + public static AoC2023_08 create() { + return new AoC2023_08(false); + } + + public static AoC2023_08 createDebug() { + return new AoC2023_08(true); + } + + @Override + protected Map parseInput(final List inputs) { + return Map.fromInput(inputs); + } + + @Override + public Long solvePart1(final Map map) { + return map.steps("AAA"); + } + + @Override + public Long solvePart2(final Map map) { + return map.forks().keySet().stream() + .filter(key -> last(key) == 'A') + .map(map::steps) + .map(BigInteger::valueOf) + .reduce((a, b) -> a.multiply(b).divide(a.gcd(b))) + .get().longValue(); + } + + @Override + @Samples({ + @Sample(method = "part1", input = TEST1, expected = "2"), + @Sample(method = "part1", input = TEST2, expected = "6"), + @Sample(method = "part2", input = TEST3, expected = "6"), + }) + public void samples() { + } + + public static void main(final String[] args) throws Exception { + AoC2023_08.create().run(); + } + + record Map(List instructions, java.util.Map forks) { + private record Fork(String left, String right) {} + + public static Map fromInput(final List lines) { + final List> blocks = StringOps.toBlocks(lines); + final List instructions + = Utils.asCharacterStream(blocks.get(0).get(0)).toList(); + final java.util.Map forks = new HashMap<>(); + for (final String line : blocks.get(1)) { + final String[] splits = line.split(" = "); + forks.put( + splits[0], + new Fork(splits[1].substring(1, 4), splits[1].substring(6, 9))); + } + return new Map(instructions, forks); + } + + public long steps(String key) { + final Iterator inss = IterTools.cycle(this.instructions); + Fork fork = this.forks.get(key); + long ans = 0; + while (last(key) != 'Z') { + key = inss.next() == 'L' ? fork.left : fork.right; + fork = this.forks.get(key); + ans++; + } + return ans; + } + } + + private static final String TEST1 = """ + RL + + AAA = (BBB, CCC) + BBB = (DDD, EEE) + CCC = (ZZZ, GGG) + DDD = (DDD, DDD) + EEE = (EEE, EEE) + GGG = (GGG, GGG) + ZZZ = (ZZZ, ZZZ) + """; + private static final String TEST2 = """ + LLR + + AAA = (BBB, BBB) + BBB = (AAA, ZZZ) + ZZZ = (ZZZ, ZZZ) + """; + private static final String TEST3 = """ + LR + + 11A = (11B, XXX) + 11B = (XXX, 11Z) + 11Z = (11B, XXX) + 22A = (22B, XXX) + 22B = (22C, 22C) + 22C = (22Z, 22Z) + 22Z = (22B, 22B) + XXX = (XXX, XXX) + """; +}