Minor fix with yMax.
All checks were successful
Solve / example-action (push) Successful in 1m36s

This commit is contained in:
2024-12-06 10:31:20 +01:00
parent 83c078a4cb
commit 67ce498228

View File

@@ -87,6 +87,7 @@ while (scanner.hasNextLine()) {
xMax = xMax.coerceAtLeast(line.lastIndex) xMax = xMax.coerceAtLeast(line.lastIndex)
yMax++ yMax++
} }
yMax-- // Should represent the max index.
val initialPosition = playerLocation.copy() val initialPosition = playerLocation.copy()
while (isPositionInTheRoom(playerLocation, xMax, yMax)) { while (isPositionInTheRoom(playerLocation, xMax, yMax)) {
visited.add(playerLocation) visited.add(playerLocation)
@@ -107,16 +108,19 @@ fun checkForLoopIfObstacleInFront(
playerDirection: Direction, playerDirection: Direction,
history: MutableList<MoveHistory> history: MutableList<MoveHistory>
): Boolean { ): Boolean {
val potentialLocation = playerDirection.move(playerLocation) val potentialBlockLocation = playerDirection.move(playerLocation)
val nextMove = playerDirection.moveByPartOneRules(playerLocation, blocks) 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 return false
} }
var localLocation = playerLocation var localLocation = playerLocation
var localDirection = playerDirection var localDirection = playerDirection
val localHistory = history.toMutableList() val localHistory = history.toMutableList()
val localBlocks = blocks.toMutableSet() val localBlocks = blocks.toMutableSet()
localBlocks.add(potentialLocation) localBlocks.add(potentialBlockLocation)
while (MoveHistory(localLocation, localDirection) !in localHistory) { while (MoveHistory(localLocation, localDirection) !in localHistory) {
localHistory.add(MoveHistory(localLocation, localDirection)) localHistory.add(MoveHistory(localLocation, localDirection))
val (newLocation, newDirection) = localDirection.moveByPartOneRules(localLocation, localBlocks) val (newLocation, newDirection) = localDirection.moveByPartOneRules(localLocation, localBlocks)