generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.kt
237 lines (198 loc) · 6.71 KB
/
Day16.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
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
package year2023.`16`
import readInput
import utils.printDebug
import utils.printlnDebug
private const val CURRENT_DAY = "16"
private data class Point(
val x: Int,
val y: Int,
) {
val left get() = Point(x - 1, y)
val right get() = Point(x + 1, y)
val top get() = Point(x, y - 1)
val bottom get() = Point(x, y + 1)
override fun toString(): String = "[$x,$y]"
}
enum class PointConfig(val value: String) {
Vertical("|"),
Horizontal("-"),
Angle1("/"),
Angle2("\\"),
EmptySpace(".");
override fun toString(): String = value;
}
private fun String.toPointConfig(): PointConfig {
return when (this) {
"|" -> PointConfig.Vertical
"-" -> PointConfig.Horizontal
"/" -> PointConfig.Angle1
"\\" -> PointConfig.Angle2
"." -> PointConfig.EmptySpace
else -> error("ILLEGAL PIPE $this")
}
}
private fun parseMap(input: List<String>): Map<Point, PointConfig> {
val mutableMap = mutableMapOf<Point, PointConfig>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
mutableMap[Point(x, y)] = value.toPointConfig()
}
}
return mutableMap
}
enum class Direction {
BOTTOM,
TOP,
LEFT,
RIGHT;
override fun toString(): String {
return when (this) {
BOTTOM -> "B"
TOP -> "T"
LEFT -> "L"
RIGHT -> "R"
}
}
}
private data class PointWithDirection(
val point: Point,
val direction: Direction
)
private fun Direction.calculateNextPoint(currentPoint: Point): Point {
return when (this) {
Direction.LEFT -> currentPoint.left
Direction.RIGHT -> currentPoint.right
Direction.TOP -> currentPoint.top
Direction.BOTTOM -> currentPoint.bottom
}
}
private fun printMap(
initialMap: Map<Point, PointConfig>,
visitedMaps: Set<PointWithDirection>,
) {
val number = initialMap.maxOf { it.key.x + 1 }
val sortedPoints = initialMap.keys.toList()
.sortedWith(compareBy({ it.y }, { it.x }))
sortedPoints.forEachIndexed { index, point ->
if (index % number == 0) {
printlnDebug { "" }
}
printDebug {
if (visitedMaps.any { it.point == point }) {
"#"
} else {
"."
}
}
}
printlnDebug { "" }
}
private fun newPointWithDirection(p: Point, newDir: Direction): PointWithDirection {
val newP = newDir.calculateNextPoint(p)
return PointWithDirection(newP, newDir)
}
private fun processStartPoint(
startPoint: Point,
startDirection: Direction,
configuration: Map<Point, PointConfig>,
): Int {
val visitedPointsAndDirections = mutableSetOf<PointWithDirection>()
val queue = ArrayDeque(listOf(PointWithDirection(startPoint, startDirection)))
while (queue.isNotEmpty()) {
val currentPoint = queue.removeFirst()
val p = currentPoint.point
val dir = currentPoint.direction
val currentConfig = configuration[p] ?: continue
if (visitedPointsAndDirections.contains(currentPoint)) continue
visitedPointsAndDirections.add(currentPoint)
when (currentConfig) {
PointConfig.Vertical -> {
when (dir) {
Direction.LEFT, Direction.RIGHT -> {
queue.add(newPointWithDirection(p, Direction.BOTTOM))
queue.add(newPointWithDirection(p, Direction.TOP))
}
Direction.TOP, Direction.BOTTOM -> queue.add(newPointWithDirection(p, dir))
}
}
PointConfig.Horizontal -> when (dir) {
Direction.LEFT, Direction.RIGHT -> queue.add(newPointWithDirection(p, dir))
Direction.TOP, Direction.BOTTOM -> {
queue.add(newPointWithDirection(p, Direction.LEFT))
queue.add(newPointWithDirection(p, Direction.RIGHT))
}
}
PointConfig.Angle1 -> when (dir) {
Direction.LEFT -> queue.add(newPointWithDirection(p, Direction.BOTTOM))
Direction.RIGHT -> queue.add(newPointWithDirection(p, Direction.TOP))
Direction.TOP -> queue.add(newPointWithDirection(p, Direction.RIGHT))
Direction.BOTTOM -> queue.add(newPointWithDirection(p, Direction.LEFT))
}
PointConfig.Angle2 -> when (dir) {
Direction.LEFT -> queue.add(newPointWithDirection(p, Direction.TOP))
Direction.RIGHT -> queue.add(newPointWithDirection(p, Direction.BOTTOM))
Direction.TOP -> queue.add(newPointWithDirection(p, Direction.LEFT))
Direction.BOTTOM -> queue.add(newPointWithDirection(p, Direction.RIGHT))
}
PointConfig.EmptySpace -> queue.add(newPointWithDirection(p, dir))
}
}
// printMap(
// initialMap = configuration,
// visitedMaps = visitedPointsAndDirections,
// )
return visitedPointsAndDirections.distinctBy { it.point }.size
}
fun main() {
fun part1(input: List<String>): Int {
val map = parseMap(input)
return processStartPoint(
startPoint = Point(0, 0),
startDirection = Direction.RIGHT,
configuration = map,
)
}
fun part2(input: List<String>): Int {
val map = parseMap(input)
val allVariants = map.keys.flatMap {
listOf(
PointWithDirection(it, Direction.RIGHT),
PointWithDirection(it, Direction.LEFT),
PointWithDirection(it, Direction.TOP),
PointWithDirection(it, Direction.BOTTOM),
)
}
var counter = 0
return allVariants
.maxOf {
counter++
if (counter % 500 == 0) {
printlnDebug { "Processed ${counter.toFloat() / allVariants.size * 100}%" }
}
processStartPoint(
startPoint = it.point,
startDirection = it.direction,
configuration = map,
)
}
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 46)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 51)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 7496)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 7932)
}