From 6b904bb42c3cfd1af39c6a0da3440d520566727e Mon Sep 17 00:00:00 2001 From: chrisly42 <chrisly@platon42.de> Date: Sat, 23 Dec 2023 12:40:51 +0100 Subject: [PATCH] Minor optimization on speed for day 23. --- src/aoc2023/Day23.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) 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) }) }