-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.kt
209 lines (184 loc) Β· 6.77 KB
/
Day15.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package aoc.years.year2024
import aoc.Day
@Year2024
class Day15 : Day() {
override fun solvePart1(input: List<String>): Any {
return Warehouse.of(input.take(50))
.move(getMoves(input.subList(50, input.size)))
.getSumOfGpsCoordinates()
}
override fun solvePart2(input: List<String>): Any {
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)
}
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()
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 ->
val (x, y) = getRobotPosition()
val (newX, newY) = getMove(move, x, y)
if (newX !in warehouse.indices || newY !in warehouse[0].indices || warehouse[newX][newY] == "#") {
return@forEach
}
if (warehouse[newX][newY] == "O") {
if (moveBox(move, newX, newY)) {
warehouse[x][y] = "."
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
}
private fun getRobotPosition(): Pair<Int, Int> {
warehouse.forEachIndexed { x, row ->
row.forEachIndexed { y, cell ->
if (cell == "@") {
return Pair(x, y)
}
}
}
return Pair(-1, -1)
}
private fun getMove(move: String, x: Int, y: Int): Pair<Int, Int> {
return when (move) {
"^" -> Pair(x - 1, y)
"v" -> Pair(x + 1, y)
"<" -> Pair(x, y - 1)
">" -> Pair(x, y + 1)
else -> Pair(x, y)
}
}
private fun moveBox(move: String, x: Int, y: Int): Boolean {
if (move == ">") {
(y..<warehouse[x].size).forEach { newY ->
if (warehouse[x][newY] == "#") {
return false
}
if (warehouse[x][newY] == ".") {
warehouse[x][y] = "."
warehouse[x][newY] = "O"
return true
}
}
} else if (move == "<") {
(y downTo 0).forEach { newY ->
if (warehouse[x][newY] == "#") {
return false
}
if (warehouse[x][newY] == ".") {
warehouse[x][y] = "."
warehouse[x][newY] = "O"
return true
}
}
} else if (move == "^") {
(x downTo 0).forEach { newX ->
if (warehouse[newX][y] == "#") {
return false
}
if (warehouse[newX][y] == ".") {
warehouse[x][y] = "."
warehouse[newX][y] = "O"
return true
}
}
} else if (move == "v") {
(x..<warehouse.size).forEach { newX ->
if (warehouse[newX][y] == "#") {
return false
}
if (warehouse[newX][y] == ".") {
warehouse[x][y] = "."
warehouse[newX][y] = "O"
return true
}
}
}
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) ->
when (cell) {
"O", "[" -> 100 * x + y
else -> 0
}
}
}
}
}