@@ -15,13 +15,10 @@ object Y2023D08 : Solution {
15
15
16
16
/* *
17
17
* A ghost's map for navigating the Desert Island and avoid sandstorms.
18
- * @property nodes The nodes on the map, indexed by their [Node.id].
19
- * @property instructions
18
+ * @property nodes The nodes on the map, indexed by their [Node.id].
19
+ * @property instructions The directions given on the map, deciding how to navigate.
20
20
*/
21
- private class DesertGhostMap (
22
- private val nodes : Map <String , Node >,
23
- private val instructions : String ,
24
- ) {
21
+ private class DesertGhostMap (private val nodes : Map <String , Node >, private val instructions : String ) {
25
22
26
23
/* * From the given node, at the given [step], decide which is the next visited node. */
27
24
private fun Node.next (step : Int ): Node = when (instructions[step % instructions.length]) {
@@ -54,9 +51,8 @@ object Y2023D08 : Solution {
54
51
* Returns the answer, or -1 if we cannot determine the correct path.
55
52
*/
56
53
fun navigateAsGhost (): Long {
57
- fun navigateAsSuperpositionComponent (from : String ): Int {
58
- var node = nodes[from] ? : return - 1
59
-
54
+ fun navigateAsSuperpositionComponent (start : Node ): Int {
55
+ var node = start
60
56
val result = navigate(node) { it.id.endsWith(' Z' ) }
61
57
62
58
// Check assumption that the step cycle will keep yielding destination nodes.
@@ -65,9 +61,9 @@ object Y2023D08 : Solution {
65
61
return if (node.id.endsWith(' Z' )) result else - 1
66
62
}
67
63
68
- return nodes.keys
69
- .filter { it.endsWith(' A' ) }
70
- .map { navigateAsSuperpositionComponent(it) }
64
+ return nodes.values
65
+ .filter { it.id. endsWith(' A' ) }
66
+ .map(::navigateAsSuperpositionComponent)
71
67
.onEach { if (it == - 1 ) return - 1 }
72
68
.lcm()
73
69
}
0 commit comments