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)
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<MoveHistory>
): 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)