Part 1 solved, part 2 got issues.
All checks were successful
Solve / example-action (push) Successful in 1m40s
All checks were successful
Solve / example-action (push) Successful in 1m40s
This commit is contained in:
parent
0f75670efb
commit
83c078a4cb
130
day06.kts
Executable file
130
day06.kts
Executable file
@ -0,0 +1,130 @@
|
|||||||
|
#!/usr/bin/env kotlin
|
||||||
|
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)
|
||||||
|
enum class Direction(val offset: Pair<Int, Int>) {
|
||||||
|
NORTH(Pair(0, -1)),
|
||||||
|
EAST(Pair(1, 0)),
|
||||||
|
SOUTH(Pair(0, 1)),
|
||||||
|
WEST(Pair(-1, 0));
|
||||||
|
|
||||||
|
fun move(currentLocation: Pair<Int, Int>): Pair<Int, Int> {
|
||||||
|
return Pair(
|
||||||
|
currentLocation.first + offset.first,
|
||||||
|
currentLocation.second + offset.second
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fun turnLeft(): Direction {
|
||||||
|
return when (this) {
|
||||||
|
NORTH -> WEST
|
||||||
|
WEST -> SOUTH
|
||||||
|
SOUTH -> EAST
|
||||||
|
EAST -> NORTH
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun turnRight(): Direction {
|
||||||
|
return when (this) {
|
||||||
|
NORTH -> EAST
|
||||||
|
EAST -> SOUTH
|
||||||
|
SOUTH -> WEST
|
||||||
|
WEST -> NORTH
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun moveByPartOneRules(
|
||||||
|
currentLocation: Pair<Int, Int>,
|
||||||
|
blocks: Set<Pair<Int, Int>>
|
||||||
|
): MoveResult {
|
||||||
|
var currentDirection = this
|
||||||
|
do {
|
||||||
|
val potentialMove = currentDirection.move(currentLocation)
|
||||||
|
if (potentialMove !in blocks) {
|
||||||
|
return MoveResult(potentialMove, currentDirection)
|
||||||
|
}
|
||||||
|
currentDirection = currentDirection.turnRight()
|
||||||
|
} while (currentDirection != this)
|
||||||
|
throw RuntimeException("Nowhere to go! $currentLocation $this")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun isPositionInTheRoom(player: Pair<Int, Int>, xMax: Int, yMax: Int): Boolean {
|
||||||
|
return player.first in 0..xMax && player.second in 0..yMax
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
val scanner = Scanner(System.`in`)
|
||||||
|
val visited = mutableSetOf<Pair<Int, Int>>()
|
||||||
|
val history = mutableListOf<MoveHistory>()
|
||||||
|
var playerLocation = Pair(0, 0)
|
||||||
|
var playerDirection: Direction = Direction.NORTH
|
||||||
|
val blocks = mutableSetOf<Pair<Int, Int>>()
|
||||||
|
val potentialBlocks = mutableSetOf<Pair<Int, Int>>()
|
||||||
|
var yMax = 0
|
||||||
|
var xMax = 0
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
val line = scanner.nextLine()
|
||||||
|
for (i in line.indices) {
|
||||||
|
if (line[i] == '.') {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (line[i] == '>') {
|
||||||
|
playerDirection = Direction.EAST
|
||||||
|
playerLocation = Pair(i, yMax)
|
||||||
|
} else if (line[i] == '<') {
|
||||||
|
playerDirection = Direction.WEST
|
||||||
|
playerLocation = Pair(i, yMax)
|
||||||
|
} else if (line[i] == '^') {
|
||||||
|
playerDirection = Direction.NORTH
|
||||||
|
playerLocation = Pair(i, yMax)
|
||||||
|
} else if (line[i] == 'v') {
|
||||||
|
playerDirection = Direction.SOUTH
|
||||||
|
playerLocation = Pair(i, yMax)
|
||||||
|
} else if (line[i] == '#') {
|
||||||
|
blocks.add(Pair(i, yMax))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xMax = xMax.coerceAtLeast(line.lastIndex)
|
||||||
|
yMax++
|
||||||
|
}
|
||||||
|
val initialPosition = playerLocation.copy()
|
||||||
|
while (isPositionInTheRoom(playerLocation, xMax, yMax)) {
|
||||||
|
visited.add(playerLocation)
|
||||||
|
if (checkForLoopIfObstacleInFront(playerLocation, playerDirection, history)) {
|
||||||
|
potentialBlocks.add(playerDirection.move(playerLocation))
|
||||||
|
}
|
||||||
|
history.add(MoveHistory(playerLocation, playerDirection))
|
||||||
|
val (newLocation, newDirection) = playerDirection.moveByPartOneRules(playerLocation, blocks)
|
||||||
|
playerLocation = newLocation
|
||||||
|
playerDirection = newDirection
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Player location: ${visited.size}")
|
||||||
|
println("Potential blocks: ${potentialBlocks.size}")
|
||||||
|
|
||||||
|
fun checkForLoopIfObstacleInFront(
|
||||||
|
playerLocation: Pair<Int, Int>,
|
||||||
|
playerDirection: Direction,
|
||||||
|
history: MutableList<MoveHistory>
|
||||||
|
): Boolean {
|
||||||
|
val potentialLocation = playerDirection.move(playerLocation)
|
||||||
|
val nextMove = playerDirection.moveByPartOneRules(playerLocation, blocks)
|
||||||
|
if (potentialLocation in visited || potentialLocation != nextMove.newLocation || !isPositionInTheRoom(potentialLocation, xMax, yMax)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var localLocation = playerLocation
|
||||||
|
var localDirection = playerDirection
|
||||||
|
val localHistory = history.toMutableList()
|
||||||
|
val localBlocks = blocks.toMutableSet()
|
||||||
|
localBlocks.add(potentialLocation)
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user