-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23.kt
146 lines (118 loc) · 4.39 KB
/
Day23.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
package aoc2021.day23
import kotlin.math.abs
data class Burrow(
val roomSize: Int,
val hallway: Map<Int, Char>,
val rooms: Map<Int, List<Char>>,
)
val roomAssignments = mapOf(2 to 'A', 4 to 'B', 6 to 'C', 8 to 'D')
fun getPossibleMoves(burrow: Burrow): List<Pair<Int, Int>> = buildList {
fun pathExists(i1: Int, i2: Int): Boolean {
val min = minOf(i1, i2)
val max = maxOf(i1, i2)
return burrow.hallway.all { (index, value) ->
index !in (min + 1) until max || value == '.'
}
}
burrow.hallway.forEach { (hindex, hvalue) ->
burrow.rooms.forEach inner@{ (rindex, rvalue) ->
// Both empty
if (hvalue == '.' && rvalue.isEmpty()) {
return@inner
}
// Hallway to room (room compatible)
if (hvalue != '.') {
if (hvalue == roomAssignments[rindex]
&& rvalue.all { it == hvalue }
&& pathExists(hindex, rindex)
) {
add(hindex to rindex)
}
return@inner
}
// Room to hallway (hallway empty)
if (pathExists(rindex, hindex)
&& rvalue.any { it != roomAssignments[rindex] }
) {
add(rindex to hindex)
}
}
}
}
fun execute(burrow: Burrow, move: Pair<Int, Int>): Pair<Burrow, Int> {
fun costFor(amphipod: Char) = when (amphipod) {
'A' -> 1
'B' -> 10
'C' -> 100
'D' -> 1000
else -> error("Invalid amphipod provided")
}
fun roomToHallway(burrow: Burrow, ri: Int, hi: Int): Pair<Burrow, Int> {
val room = burrow.rooms.getValue(ri)
val amphipod = room.first()
val upwards = burrow.roomSize - room.size + 1
val sideways = abs(ri - hi)
val cost = (upwards + sideways) * costFor(amphipod)
val modifiedBurrow = burrow.copy(
rooms = burrow.rooms.plus(ri to room.drop(1)),
hallway = burrow.hallway.plus(hi to amphipod),
)
return Pair(modifiedBurrow, cost)
}
fun hallwayToRoom(burrow: Burrow, hi: Int, ri: Int): Pair<Burrow, Int> {
val room = burrow.rooms.getValue(ri)
val amphipod = burrow.hallway.getValue(hi)
val sideways = abs(ri - hi)
val downwards = burrow.roomSize - room.size
val cost = (sideways + downwards) * costFor(amphipod)
val modifiedBurrow = burrow.copy(
rooms = burrow.rooms.plus(ri to listOf(amphipod) + room),
hallway = burrow.hallway.plus(hi to '.'),
)
return Pair(modifiedBurrow, cost)
}
return if (move.first in roomAssignments.keys) {
roomToHallway(burrow, move.first, move.second)
} else {
hallwayToRoom(burrow, move.first, move.second)
}
}
fun organizeAmphipods(input: List<String>): Int {
val rooms = input.drop(2).dropLast(1).map { line ->
line.filter { char -> char in 'A'..'D' }
}
val a = rooms.map { it[0] }
val b = rooms.map { it[1] }
val c = rooms.map { it[2] }
val d = rooms.map { it[3] }
val initialBurrow = Burrow(
roomSize = a.size,
hallway = mapOf(0 to '.', 1 to '.', 3 to '.', 5 to '.', 7 to '.', 9 to '.', 10 to '.'),
rooms = mapOf(2 to a, 4 to b, 6 to c, 8 to d),
)
var bestCost = Int.MAX_VALUE
val seen = mutableMapOf<Burrow, Int>()
fun simulate(prevBurrow: Burrow, move: Pair<Int, Int>, prevCost: Int): Int {
if (prevCost >= bestCost) return Int.MAX_VALUE
val (burrow, cost) = execute(prevBurrow, move)
val totalCost = prevCost + cost
if (burrow in seen) {
if (seen.getValue(burrow) <= totalCost) {
return Int.MAX_VALUE
}
}
seen[burrow] = totalCost
if (roomAssignments.all { (ri, amph) ->
val r = burrow.rooms.getValue(ri)
r.size == burrow.roomSize && r.all { it == amph }
}) {
if (totalCost < bestCost) bestCost = totalCost
return totalCost
}
val moves = getPossibleMoves(burrow)
if (moves.isEmpty()) return Int.MAX_VALUE
return moves.minOf { simulate(burrow, it, totalCost) }
}
return getPossibleMoves(initialBurrow).minOf { simulate(initialBurrow, it, 0) }
}
fun computeBestPath(input: List<String>) = organizeAmphipods(input)