Day 6 part 2 completed.
All checks were successful
Solve / example-action (push) Successful in 2m26s

This commit is contained in:
Logiar 2024-12-06 11:25:52 +01:00
parent 67ce498228
commit fcaa401d60
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
2 changed files with 37 additions and 26 deletions

View File

@ -3,8 +3,7 @@ import java.io.File
import java.lang.RuntimeException
import java.util.Scanner
data class MoveResult(val newLocation: Pair<Int, Int>, val newDirection: Direction)
data class MoveHistory(val location: Pair<Int, Int>, val direction: Direction)
data class LocationState(val location: Pair<Int, Int>, val direction: Direction)
enum class Direction(val offset: Pair<Int, Int>) {
NORTH(Pair(0, -1)),
EAST(Pair(1, 0)),
@ -36,12 +35,12 @@ enum class Direction(val offset: Pair<Int, Int>) {
fun moveByPartOneRules(
currentLocation: Pair<Int, Int>,
blocks: Set<Pair<Int, Int>>
): 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<Int, Int>, xMax: Int, yMax: Int): Boolean {
return player.first in 0..xMax && player.second in 0..yMax
}
fun isLoop(
playerLocation: Pair<Int, Int>,
playerDirection: Direction,
history: List<LocationState>,
blocks: MutableSet<Pair<Int, Int>>,
potentialBlockLocation: Pair<Int, Int>,
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<Pair<Int, Int>>()
val history = mutableListOf<MoveHistory>()
val history = mutableListOf<LocationState>()
var playerLocation = Pair(0, 0)
var playerDirection: Direction = Direction.NORTH
val blocks = mutableSetOf<Pair<Int, Int>>()
@ -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<Int, Int>,
playerDirection: Direction,
history: MutableList<MoveHistory>
history: MutableList<LocationState>
): 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)
}

2
day06.kts.testvalue Normal file
View File

@ -0,0 +1,2 @@
Player location: 4883
Potential blocks: 1655