Skip to content

Commit

Permalink
2024 Day 16, part 2, WIP :(
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongXiLu committed Dec 16, 2024
1 parent 1a0a014 commit b0d7436
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/main/kotlin/aoc/years/year2024/Day16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Day16 : Day() {
}

override fun solvePart2(input: List<String>): Any {
return 1
return Maze.of(input).findShortestPathLength()
}

}
Expand All @@ -29,6 +29,26 @@ class Maze(private val maze: List<List<Char>>) {
return visited[x][y].second
}

fun findShortestPathLength(): Int {
val visited = traverse()

val pathLength = visited.getPathLength(getEnd())
// print the maze
maze.forEachIndexed { x, row ->
row.forEachIndexed { y, c ->
// print(visited[x][y].toString() + "\t")
if (Pair(x, y) in pathLength) {
print("O")
} else {
print(c)
}
}
println()
}

return pathLength.size
}

private fun traverse(): List<MutableList<Triple<Boolean, Int, Char>>> {
val visited = maze.indices
.map {
Expand Down Expand Up @@ -95,4 +115,37 @@ class Maze(private val maze: List<List<Char>>) {
throw IllegalStateException("Not found: $char")
}

private fun List<MutableList<Triple<Boolean, Int, Char>>>.getPathLength(position: Pair<Int, Int>): List<Pair<Int, Int>> {
if (maze[position.first][position.second] == 'S') {
return listOf(position)
}

val (_, distance, direction) = this[position.first][position.second]
val paths = Triple(position.first, position.second, direction).getNeighbours()
.filter {
var distanceOfNeighbour = distance
if (this[it.first][it.second].third == it.third.flip() && this[it.first][it.second].third == direction) {
distanceOfNeighbour -= 1
} else if (this[it.first][it.second].third == it.third.flip()) {
distanceOfNeighbour += 999
} else {
distanceOfNeighbour -= 1001
}
return@filter this[it.first][it.second].second == distanceOfNeighbour
}
.map { getPathLength(Pair(it.first, it.second)) }
.flatten()

return listOf(position).plus(paths).distinct()
}

private fun Char.flip(): Char {
return when (this) {
'^' -> 'v'
'v' -> '^'
'<' -> '>'
'>' -> '<'
else -> throw IllegalStateException("Not a valid direction: $this")
}
}
}
Loading

0 comments on commit b0d7436

Please sign in to comment.