Skip to content

Commit 7e49eb6

Browse files
committed
Refactor Y2023D08.
1 parent 840ba8d commit 7e49eb6

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

Diff for: solutions/aockt/y2023/Y2023D08.kt

+8-12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ object Y2023D08 : Solution {
1515

1616
/**
1717
* 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.
2020
*/
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) {
2522

2623
/** From the given node, at the given [step], decide which is the next visited node. */
2724
private fun Node.next(step: Int): Node = when (instructions[step % instructions.length]) {
@@ -54,9 +51,8 @@ object Y2023D08 : Solution {
5451
* Returns the answer, or -1 if we cannot determine the correct path.
5552
*/
5653
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
6056
val result = navigate(node) { it.id.endsWith('Z') }
6157

6258
// Check assumption that the step cycle will keep yielding destination nodes.
@@ -65,9 +61,9 @@ object Y2023D08 : Solution {
6561
return if (node.id.endsWith('Z')) result else -1
6662
}
6763

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)
7167
.onEach { if (it == -1) return -1 }
7268
.lcm()
7369
}

0 commit comments

Comments
 (0)