Skip to content

Commit

Permalink
2024 Day 15, part 2 🤯
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongXiLu committed Dec 15, 2024
1 parent d30ed24 commit b19d566
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
89 changes: 82 additions & 7 deletions src/main/kotlin/aoc/years/year2024/Day15.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import aoc.Day
class Day15 : Day() {

override fun solvePart1(input: List<String>): Any {
// 8,10,50
return Warehouse.of(input.take(50))
.move(getMoves(input.subList(50, input.size)))
.getSumOfGpsCoordinates()
}

override fun solvePart2(input: List<String>): Any {

return 0
return Warehouse.ofWithExpansion(input.take(50))
.move(getMoves(input.subList(50, input.size)))
.getSumOfGpsCoordinates()
}

private fun getMoves(input: List<String>) = input.map { it.split("") }.flatten().filter(String::isNotBlank)
Expand All @@ -24,15 +24,29 @@ class Day15 : Day() {
class Warehouse(private val warehouse: MutableList<MutableList<String>>) {
companion object {
fun of(input: List<String>): Warehouse {
val warehouse = input.take(50).map { it.split("").filter(String::isNotBlank).toMutableList() }.toMutableList()
val warehouse =
input.take(50).map { it.split("").filter(String::isNotBlank).toMutableList() }.toMutableList()
return Warehouse(warehouse)
}

fun ofWithExpansion(input: List<String>): Warehouse {
val warehouse = input.take(50).map {
it.split("").filter(String::isNotBlank).map { char ->
when (char) {
"#" -> listOf("#", "#")
"O" -> listOf("[", "]")
"." -> listOf(".", ".")
"@" -> listOf("@", ".")
else -> listOf()
}
}.flatten().toMutableList()
}.toMutableList()
return Warehouse(warehouse)
}
}

fun move(moves: List<String>): Warehouse {
moves.forEach { move ->
println(warehouse.joinToString("\n") { it.joinToString("") })

val (x, y) = getRobotPosition()
val (newX, newY) = getMove(move, x, y)

Expand All @@ -46,10 +60,19 @@ class Warehouse(private val warehouse: MutableList<MutableList<String>>) {
warehouse[newX][newY] = "@"
}

} else if (warehouse[newX][newY] == "[" || warehouse[newX][newY] == "]") {
if (moveExpandedBox(move, newX, newY)) {
warehouse[x][y] = "."
warehouse[newX][newY] = "@"
}

} else {
warehouse[x][y] = "."
warehouse[newX][newY] = "@"
}

// println(move)
// println(warehouse.joinToString("\n") { it.joinToString("") } + "\n")
}
return this
}
Expand Down Expand Up @@ -124,10 +147,62 @@ class Warehouse(private val warehouse: MutableList<MutableList<String>>) {
return false
}

private fun moveExpandedBox(move: String, x: Int, y: Int): Boolean {
val connectedBoxes = getConnectedBoxes(move, x, y)

if (connectedBoxes.isNotEmpty()) {
if (canMoveBoxes(move, connectedBoxes)) {
moveBoxes(move, connectedBoxes)
return true
}
}
return false
}

private fun getConnectedBoxes(move: String, x: Int, y: Int): List<Pair<Int, Int>> {
if (warehouse[x][y] == "]" || warehouse[x][y] == "[") {
val box = listOf(Pair(x, y), Pair(x, y.plus(if (warehouse[x][y] == "]") -1 else 1)))
val boxesToCheck = when (move) {
">" -> listOf(Pair(x, y + 1))
"<" -> listOf(Pair(x, y - 1))
else -> box
}

val connectedBoxes = boxesToCheck.map {
val newMove = getMove(move, it.first, it.second)
return@map getConnectedBoxes(move, newMove.first, newMove.second)
}.flatten()
return box.plus(connectedBoxes).distinct()
}

return listOf()
}

private fun canMoveBoxes(move: String, boxes: List<Pair<Int, Int>>): Boolean {
return boxes.all { box ->
val newMove = getMove(move, box.first, box.second)
return@all warehouse[newMove.first][newMove.second] != "#"
}
}

private fun moveBoxes(move: String, boxes: List<Pair<Int, Int>>) {
boxes.map { box ->
val newMove = getMove(move, box.first, box.second)
val boxSymbol = warehouse[box.first][box.second]
warehouse[box.first][box.second] = "."
return@map Pair(newMove, boxSymbol)
}.forEach { (newMove, boxSymbol) ->
warehouse[newMove.first][newMove.second] = boxSymbol
}
}

fun getSumOfGpsCoordinates(): Int {
return warehouse.withIndex().sumOf { (x, row) ->
row.withIndex().sumOf { (y, cell) ->
if (cell == "O") 100 * x + y else 0
when (cell) {
"O", "[" -> 100 * x + y
else -> 0
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/kotlin/aoc/years/year2024/Day15Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import kotlin.test.assertEquals
internal class Day15Test {

@Test

fun testPart1_1() {
fun testPart1() {
val day = Day15()
assertEquals(
2028, day.solvePart1(
Expand All @@ -30,10 +29,10 @@ internal class Day15Test {
}

@Test
fun testPart1() {
fun testPart2() {
val day = Day15()
assertEquals(
2028, day.solvePart1(
9021, day.solvePart2(
listOf(
"##########",
"#..O..O.O#",
Expand All @@ -47,18 +46,19 @@ internal class Day15Test {
"##########",
"",
"<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^",
"vv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v",
"vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v",
"><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<",
"<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^",
"^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><",
"^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^",
">^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^",
"<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>",
"^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>",
"^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^",
"v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^",
)
)
)
}


}

0 comments on commit b19d566

Please sign in to comment.