From 67ce49822894fc26a30910ff014dedd522e7587b Mon Sep 17 00:00:00 2001 From: Logiar Date: Fri, 6 Dec 2024 10:31:20 +0100 Subject: [PATCH] Minor fix with yMax. --- day06.kts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/day06.kts b/day06.kts index 5efc3a6..5456c7f 100755 --- a/day06.kts +++ b/day06.kts @@ -87,6 +87,7 @@ while (scanner.hasNextLine()) { xMax = xMax.coerceAtLeast(line.lastIndex) yMax++ } +yMax-- // Should represent the max index. val initialPosition = playerLocation.copy() while (isPositionInTheRoom(playerLocation, xMax, yMax)) { visited.add(playerLocation) @@ -107,16 +108,19 @@ fun checkForLoopIfObstacleInFront( playerDirection: Direction, history: MutableList ): Boolean { - val potentialLocation = playerDirection.move(playerLocation) + val potentialBlockLocation = playerDirection.move(playerLocation) val nextMove = playerDirection.moveByPartOneRules(playerLocation, blocks) - if (potentialLocation in visited || potentialLocation != nextMove.newLocation || !isPositionInTheRoom(potentialLocation, xMax, yMax)) { + val alreadyVisited = potentialBlockLocation in visited + val pathAlreadyBlocked = potentialBlockLocation != nextMove.newLocation + val positionNotInTheRoom = !isPositionInTheRoom(potentialBlockLocation, xMax, yMax) + if (alreadyVisited || pathAlreadyBlocked || positionNotInTheRoom) { return false } var localLocation = playerLocation var localDirection = playerDirection val localHistory = history.toMutableList() val localBlocks = blocks.toMutableSet() - localBlocks.add(potentialLocation) + localBlocks.add(potentialBlockLocation) while (MoveHistory(localLocation, localDirection) !in localHistory) { localHistory.add(MoveHistory(localLocation, localDirection)) val (newLocation, newDirection) = localDirection.moveByPartOneRules(localLocation, localBlocks)