-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoC2023_08.java
More file actions
123 lines (105 loc) Β· 3.67 KB
/
AoC2023_08.java
File metadata and controls
123 lines (105 loc) Β· 3.67 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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.StringOps;
import com.github.pareronia.aoc.Utils;
import com.github.pareronia.aoc.itertools.IterTools;
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<AoC2023_08.Map, Long, Long> {
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<String> 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<Character> instructions, java.util.Map<String, Fork> forks) {
private record Fork(String left, String right) {}
public static Map fromInput(final List<String> lines) {
final List<List<String>> blocks = StringOps.toBlocks(lines);
final List<Character> instructions
= Utils.asCharacterStream(blocks.get(0).get(0)).toList();
final java.util.Map<String, Fork> 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<Character> 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)
""";
}