Minor optimization on speed for day 23.

This commit is contained in:
Chris Hodges 2023-12-23 12:40:51 +01:00
parent 12d2e88da1
commit 6b904bb42c

View File

@ -209,6 +209,7 @@ fun main() {
pos = pos.translate(nextDirs.single())
if (pos == endPos) {
node.edges.add(relLen + 1 to endNode)
endNode.edges.add(relLen + 1 to node)
break
}
} else {
@ -222,6 +223,16 @@ fun main() {
}
} while (nextDirs.size == 1)
}
// optimize endnode path
var cNode: GNode = endNode
var pNode = cNode
while (cNode.edges.size == 1) {
pNode = cNode
cNode = cNode.edges.single().second
}
cNode.edges.removeIf { it.second != pNode }
return rec(endNode, startNode, 0, Array(grid.height) { BooleanArray(grid.width) })
}