generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay20.kt
195 lines (164 loc) · 5.07 KB
/
Day20.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
package year2024.`20`
import readInput
import utils.findShortestPathByPredicate
import kotlin.math.abs
private const val CURRENT_DAY = "20"
private data class Point(
val x: Int,
val y: Int,
) {
fun moveUp(): Point = copy(y = y - 1)
fun moveDown(): Point = copy(y = y + 1)
fun moveLeft(): Point = copy(x = x - 1)
fun moveRight(): Point = copy(x = x + 1)
override fun toString(): String {
return "[$x,$y]"
}
}
private sealed class BlockType {
abstract val position: Point
data class Start(
override val position: Point,
) : BlockType() {
override fun toString(): String = "S"
}
data class End(
override val position: Point,
) : BlockType() {
override fun toString(): String = "E"
}
data class Wall(
override val position: Point,
) : BlockType() {
override fun toString(): String = "#"
}
data class Empty(
override val position: Point,
) : BlockType() {
override fun toString(): String = "."
}
}
private fun parseMap(input: List<String>): Map<Point, BlockType> {
val mutableMap = mutableMapOf<Point, BlockType>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
val point = Point(x, y)
val result = when (value) {
"#" -> BlockType.Wall(point)
"." -> BlockType.Empty(point)
"S" -> BlockType.Start(point)
"E" -> BlockType.End(point)
else -> error("AAAA $value")
}
mutableMap[point] = result
}
}
return mutableMap
}
private data class StepWithScore(
val position: Point,
)
private fun StepWithScore.calculateNextSteps(
initialMap: Map<Point, BlockType>,
visited: Set<Point>,
minX: Int,
minY: Int,
maxX: Int,
maxY: Int,
): List<StepWithScore> {
return listOfNotNull(
StepWithScore(
position.moveUp(),
).takeIf { initialMap[it.position] !is BlockType.Wall },
StepWithScore(
position.moveDown(),
).takeIf { initialMap[it.position] !is BlockType.Wall },
StepWithScore(
position.moveLeft(),
).takeIf { initialMap[it.position] !is BlockType.Wall },
StepWithScore(
position.moveRight(),
).takeIf { initialMap[it.position] !is BlockType.Wall },
).filter {
it.position.x in minX..maxX &&
it.position.y >= minY && it.position.y <= maxY
}
.filter {
it.position !in visited
}
}
private fun pathSearch(
initialMap: Map<Point, BlockType>,
currentRaindeerPosition: Point,
topPosition: Point,
): Pair<Long, List<StepWithScore>> {
val minX = initialMap.keys.minOf { it.x }
val minY = initialMap.keys.minOf { it.y }
val maxX = initialMap.keys.maxOf { it.x }
val maxY = initialMap.keys.maxOf { it.y }
val path = findShortestPathByPredicate(
start = StepWithScore(
position = currentRaindeerPosition,
),
endFunction = { (p) -> p == topPosition },
neighbours = {
it.calculateNextSteps(
initialMap = initialMap,
visited = emptySet(),
minX = minX,
minY = minY,
maxX = maxX,
maxY = maxY,
)
},
cost = { _, _ -> 1 },
heuristic = { (it.position.x + it.position.y) },
)
return path.getScore().toLong() to path.getPath()
}
private fun findSavedDistance(steps: List<Point>, cheatSize: Int): Long {
var count = 0L
for (i1 in steps.indices) {
for (i2 in i1..steps.lastIndex) {
val p1 = steps[i1]
val p2 = steps[i2]
val dist = p1.manhattanDistance(p2)
if (dist > cheatSize) continue
val totalSaveTime = i2 - i1 - dist
if (totalSaveTime >= 100) {
count++
}
}
}
return count
}
private fun Point.manhattanDistance(point: Point): Int {
return abs(point.x - x) + abs(point.y - y)
}
fun main() {
fun part1(input: List<String>): Long {
val map = parseMap(input)
val position = map.keys.find { map[it] is BlockType.Start }!!
val topPos = map.keys.find { map[it] is BlockType.End }!!
val (_, path) = pathSearch(map, position, topPos)
return findSavedDistance(path.map { it.position }, 2)
}
fun part2(input: List<String>): Long {
val map = parseMap(input)
val position = map.keys.find { map[it] is BlockType.Start }!!
val topPos = map.keys.find { map[it] is BlockType.End }!!
val (_, path) = pathSearch(map, position, topPos)
return findSavedDistance(path.map { it.position }, 20)
}
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 1321L)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 971737L)
}