diff --git a/day06.kts b/day06.kts index 5456c7f..dc90137 100755 --- a/day06.kts +++ b/day06.kts @@ -3,8 +3,7 @@ import java.io.File import java.lang.RuntimeException import java.util.Scanner -data class MoveResult(val newLocation: Pair, val newDirection: Direction) -data class MoveHistory(val location: Pair, val direction: Direction) +data class LocationState(val location: Pair, val direction: Direction) enum class Direction(val offset: Pair) { NORTH(Pair(0, -1)), EAST(Pair(1, 0)), @@ -36,12 +35,12 @@ enum class Direction(val offset: Pair) { fun moveByPartOneRules( currentLocation: Pair, blocks: Set> - ): MoveResult { + ): LocationState { var currentDirection = this do { val potentialMove = currentDirection.move(currentLocation) if (potentialMove !in blocks) { - return MoveResult(potentialMove, currentDirection) + return LocationState(potentialMove, currentDirection) } currentDirection = currentDirection.turnRight() } while (currentDirection != this) @@ -52,10 +51,36 @@ fun isPositionInTheRoom(player: Pair, xMax: Int, yMax: Int): Boolean { return player.first in 0..xMax && player.second in 0..yMax } +fun isLoop( + playerLocation: Pair, + playerDirection: Direction, + history: List, + blocks: MutableSet>, + potentialBlockLocation: Pair, + xMax: Int, + yMax: Int +): Boolean { + var localLocation = playerLocation + var localDirection = playerDirection + val localHistory = history.toMutableList() + val localBlocks = blocks.toMutableSet() + localBlocks.add(potentialBlockLocation) + while (LocationState(localLocation, localDirection) !in localHistory) { + localHistory.add(LocationState(localLocation, localDirection)) + val (newLocation, newDirection) = localDirection.moveByPartOneRules(localLocation, localBlocks) + localLocation = newLocation + localDirection = newDirection + if (!isPositionInTheRoom(localLocation, xMax, yMax)) { + return false + } + } + return true +} + val scanner = Scanner(System.`in`) val visited = mutableSetOf>() -val history = mutableListOf() +val history = mutableListOf() var playerLocation = Pair(0, 0) var playerDirection: Direction = Direction.NORTH val blocks = mutableSetOf>() @@ -94,7 +119,7 @@ while (isPositionInTheRoom(playerLocation, xMax, yMax)) { if (checkForLoopIfObstacleInFront(playerLocation, playerDirection, history)) { potentialBlocks.add(playerDirection.move(playerLocation)) } - history.add(MoveHistory(playerLocation, playerDirection)) + history.add(LocationState(playerLocation, playerDirection)) val (newLocation, newDirection) = playerDirection.moveByPartOneRules(playerLocation, blocks) playerLocation = newLocation playerDirection = newDirection @@ -106,29 +131,13 @@ println("Potential blocks: ${potentialBlocks.size}") fun checkForLoopIfObstacleInFront( playerLocation: Pair, playerDirection: Direction, - history: MutableList + history: MutableList ): Boolean { - val potentialBlockLocation = playerDirection.move(playerLocation) - val nextMove = playerDirection.moveByPartOneRules(playerLocation, blocks) + val potentialBlockLocation = playerDirection.moveByPartOneRules(playerLocation, blocks).location val alreadyVisited = potentialBlockLocation in visited - val pathAlreadyBlocked = potentialBlockLocation != nextMove.newLocation val positionNotInTheRoom = !isPositionInTheRoom(potentialBlockLocation, xMax, yMax) - if (alreadyVisited || pathAlreadyBlocked || positionNotInTheRoom) { + if (alreadyVisited || positionNotInTheRoom) { return false } - var localLocation = playerLocation - var localDirection = playerDirection - val localHistory = history.toMutableList() - val localBlocks = blocks.toMutableSet() - localBlocks.add(potentialBlockLocation) - while (MoveHistory(localLocation, localDirection) !in localHistory) { - localHistory.add(MoveHistory(localLocation, localDirection)) - val (newLocation, newDirection) = localDirection.moveByPartOneRules(localLocation, localBlocks) - localLocation = newLocation - localDirection = newDirection - if (!isPositionInTheRoom(localLocation, xMax, yMax)) { - return false - } - } - return true; + return isLoop(playerLocation, playerDirection, history, blocks, potentialBlockLocation, xMax, yMax) } diff --git a/day06.kts.testvalue b/day06.kts.testvalue new file mode 100644 index 0000000..387568b --- /dev/null +++ b/day06.kts.testvalue @@ -0,0 +1,2 @@ +Player location: 4883 +Potential blocks: 1655