This commit is contained in:
parent
83c078a4cb
commit
67ce498228
10
day06.kts
10
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<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)
|
||||
|
Loading…
Reference in New Issue
Block a user