aboutsummaryrefslogtreecommitdiff
path: root/aoc-2021-kotlin/src/Day23.kt
blob: 232434ac9cd31a4d1713f98ff2d518591cf4bcca (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.util.Comparator
import java.util.PriorityQueue
import kotlin.math.absoluteValue
import kotlin.math.max
import kotlin.math.min

object Day23 {
    private enum class Amphipod(val energyPerStep: Int) {
        Amber(1),
        Bronze(10),
        Copper(100),
        Desert(1000);

        companion object {
            fun fromChar(char: Char) = when (char) {
                'A' -> Amber
                'B' -> Bronze
                'C' -> Copper
                'D' -> Desert
                else -> null
            }
        }

        override fun toString(): String {
            return super.toString().first().toString()
        }
    }

    private sealed class Position {
        abstract fun pop(): Position
        abstract fun push(amphipod: Amphipod): Position
        abstract fun peek(): Amphipod

        data class Room(val targetType: Amphipod, val occupants: List<Amphipod>) : Position() {
            override fun pop() = copy(occupants = occupants.dropLast(1))
            override fun push(amphipod: Amphipod) = copy(occupants = occupants + amphipod)
            override fun peek() = occupants.last()
            val isSettled get() = occupants.all { it == targetType }
        }

        data class Hallway(val occupant: Amphipod?) : Position() {
            override fun pop() = copy(occupant = null)
            override fun push(amphipod: Amphipod) = copy(occupant = amphipod)
            override fun peek() = occupant!!
        }
    }

    private data class State(val roomDepth: Int, val positions: List<Position>) {
        companion object {
            fun fromString(input: String) =
                input
                    .split("\n").filter { it.isNotBlank() }.drop(1).dropLast(1)
                    .map { line -> line.drop(1).dropLast(1) }
                    .let { lines ->
                        State(
                            lines.size - 1,
                            (0..10).map { position ->
                                if (lines[1][position] == '#')
                                    Position.Hallway(Amphipod.fromChar(lines.first()[position]))
                                else
                                    Position.Room(
                                        when (position) {
                                            2 -> Amphipod.Amber
                                            4 -> Amphipod.Bronze
                                            6 -> Amphipod.Copper
                                            8 -> Amphipod.Desert
                                            else -> throw IllegalStateException()
                                        },
                                        lines.indices
                                            .drop(1)
                                            .reversed()
                                            .mapNotNull { Amphipod.fromChar(lines[it][position]) }
                                    )
                            }
                        )
                    }
        }

        override fun toString(): String {
            val stringBuilder = StringBuilder()

            stringBuilder.append("#".repeat(positions.size + 2))
            stringBuilder.append('\n')

            stringBuilder.append('#')
            positions.forEach {
                stringBuilder.append(
                    if (it is Position.Hallway) it.occupant?.toString() ?: '.'
                    else '.'
                )
            }
            stringBuilder.append('#')
            stringBuilder.append('\n')

            stringBuilder.append('#')
            positions.forEach {
                stringBuilder.append(
                    if (it is Position.Room) it.occupants.getOrNull(roomDepth - 1)?.toString() ?: '.'
                    else '#'
                )
            }
            stringBuilder.append('#')
            stringBuilder.append('\n')

            val hallwayStart = (positions.indexOfFirst { it is Position.Room } - 1)
            val hallwayEnd = (positions.indexOfLast { it is Position.Room } + 1)
            (roomDepth - 2 downTo 0).forEach { depth ->
                stringBuilder.append(" ".repeat(hallwayStart + 1))
                (hallwayStart..hallwayEnd).map { positions[it] }.forEach {
                    stringBuilder.append(
                        if (it is Position.Room) it.occupants.getOrNull(depth)?.toString() ?: '.'
                        else '#'
                    )
                }
                stringBuilder.append('\n')
            }

            stringBuilder.append(" ".repeat(hallwayStart + 1))
            stringBuilder.append("#".repeat(hallwayEnd - hallwayStart + 1))
            stringBuilder.append('\n')

            return stringBuilder.toString()
        }

        val isFinished
            get() = positions.all {
                when (it) {
                    is Position.Hallway -> hasEmpty(it)
                    is Position.Room -> hasFull(it) && it.occupants.all { a -> a == it.targetType }
                }
            }

        private fun withMove(from: Int, to: Int) = positions[from].peek().let { amphipod ->
            val horizontalDistance = (to - from).absoluteValue
            val outDistance = (positions[from] as? Position.Room)?.let { roomDepth - it.occupants.size + 1 } ?: 0
            val inDistance = (positions[to] as? Position.Room)?.let { roomDepth - it.occupants.size } ?: 0
            val energy = (outDistance + horizontalDistance + inDistance) * amphipod.energyPerStep

            energy to copy(positions = positions.mapIndexed { index, position ->
                when (index) {
                    from -> position.pop()
                    to -> position.push(amphipod)
                    else -> position
                }
            })
        }

        private fun hasHallwayBlocked(from: Int, to: Int) = positions
            .subList(min(from, to) + 1, max(from, to))
            .any { it is Position.Hallway && it.occupant != null }

        private fun hasEmpty(position: Position) = when (position) {
            is Position.Room -> position.occupants.isEmpty()
            is Position.Hallway -> position.occupant == null
        }

        private fun hasFull(position: Position) = when (position) {
            is Position.Room -> position.occupants.size == roomDepth
            is Position.Hallway -> position.occupant != null
        }

        fun allNextStates() = sequence {
            for ((fromPair, toPair) in combinations(positions.withIndex())) {
                val (startIndex, start) = fromPair
                val (endIndex, end) = toPair

                if (
                    startIndex == endIndex ||
                    hasEmpty(start) ||
                    hasFull(end) ||
                    hasHallwayBlocked(startIndex, endIndex)
                )
                    continue

                val afterMove = withMove(startIndex, endIndex)

                when (end) {
                    is Position.Room -> {
                        if (start.peek() == end.targetType && end.isSettled)
                            yield(afterMove)
                    }

                    is Position.Hallway -> {
                        if (start is Position.Room && !start.isSettled)
                            yield(afterMove)
                    }
                }
            }
        }
    }

    private class UniquePriorityQueue<T>(comparator: Comparator<T>) {
        private val internalQueue = PriorityQueue(comparator)
        private val internalSet = mutableSetOf<T>()

        constructor(initialValue: T, comparator: Comparator<T>) : this(comparator) {
            add(initialValue)
        }

        fun add(value: T) {
            if (value !in internalSet) {
                internalQueue.add(value)
                internalSet.add(value)
            }
        }

        fun poll(): T {
            val value = internalQueue.poll()
            internalSet.remove(value)
            return value
        }

        fun isNotEmpty() = internalQueue.isNotEmpty()
    }

    fun part1(input: String): Int {
        val state = State.fromString(input)

        val visited = mutableSetOf<State>()
        val distances = mutableMapOf(state to 0)
        val queue = UniquePriorityQueue(state, compareBy { distances[it]!! })

        queue.add(state)

        while (queue.isNotEmpty()) {
            val current = queue.poll()

            for ((cost, neighbour) in current.allNextStates()) {
                if (neighbour !in visited) {
                    val newCost = distances[current]!! + cost
                    if (newCost < distances.getOrDefault(neighbour, Int.MAX_VALUE)) {
                        distances[neighbour] = newCost
                        queue.add(neighbour)
                    }

                    queue.add(neighbour)
                }
            }

            visited.add(current)
        }

        return distances[visited.find { it.isFinished }]!!
    }

    fun part2(input: String): Int {
        val list = input.split("\n").toMutableList()
        list.addAll(
            3,
            """
            |  #D#C#B#A#
            |  #D#B#A#C#
            """.trimMargin().split("\n")
        )
        return part1(list.joinToString("\n"))
    }
}

fun main() {
    val testInput = """
    #############
    #...........#
    ###B#C#B#D###
      #A#D#C#A#
      #########
    """.trimIndent()
    check(Day23.part1(testInput) == 12521)
    check(Day23.part2(testInput) == 44169)

    val input = """
    #############
    #...........#
    ###D#B#A#C###
      #B#D#A#C#
      #########
    """.trimIndent()
    println(Day23.part1(input))
    println(Day23.part2(input))
}