diff --git a/src/aoc2023/Day23.kt b/src/aoc2023/Day23.kt index c806f9e..59cc9ba 100644 --- a/src/aoc2023/Day23.kt +++ b/src/aoc2023/Day23.kt @@ -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) }) }