generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay21.kt
235 lines (199 loc) · 7.06 KB
/
Day21.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
package year2023.`21`
import readInput
import kotlin.math.pow
import kotlin.math.roundToLong
private const val CURRENT_DAY = "21"
private data class Point(
val x: Int,
val y: Int,
) {
override fun toString(): String {
return "[$x,$y]"
}
}
private fun Point.neighbours(): Set<Point> {
return setOf(
Point(x = x, y = y + 1),
Point(x = x, y = y - 1),
Point(x = x + 1, y = y),
Point(x = x - 1, y = y),
)
}
private fun parseMap(input: List<String>, initialPoint: Point): Map<Point, String> {
val mutableMap = mutableMapOf<Point, String>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
val currentPoint = Point(x, y)
mutableMap[currentPoint] = if (currentPoint == initialPoint) {
"S"
} else {
value.takeIf { it != "S" } ?: "."
}
}
}
return mutableMap
}
private fun processMap(
initialMap: Map<Point, String>,
setOfCurrentPoints: Set<Point>,
): Map<Point, String> {
val newPoints = setOfCurrentPoints.flatMap { point ->
point.neighbours()
}
.filter { it in initialMap.keys }
.filter { initialMap[it] != "#" }
val mutableMap = mutableMapOf<Point, String>()
initialMap.forEach { (point, value) ->
when (value) {
"#" -> mutableMap[point] = "#"
"S", "." -> mutableMap[point] = "O".takeIf { point in newPoints } ?: "."
"O" -> mutableMap[point] = "O".takeIf { mutableMap[point] == "O" } ?: "."
else -> error("Illegal Point: $point Value:$value")
}
}
return mutableMap
}
fun main() {
fun part1(
input: List<String>,
repeatCount: Int = 64,
sPoint: Point = Point(input.size / 2, input.size / 2)
): Int {
val map = parseMap(input, sPoint)
var currentMap = map
repeat(repeatCount) {
currentMap = processMap(
initialMap = currentMap,
setOfCurrentPoints = currentMap.keys.filter { currentMap[it] in listOf("O", "S") }
.toSet(),
)
}
return currentMap.count { it.value == "O" }
}
fun part2(input: List<String>): Long {
val useCachedValue = true
assert(input.size == input[0].length)
val size = input.size
val startX = size / 2
val startY = size / 2
val steps = 26501365
println("size:$size")
assert(steps % size == size / 2)
val gridWidth = steps.floorDiv(size) - 1
println("gridWidth:$gridWidth")
val odd = (gridWidth.floorDiv(2) * 2 + 1).toDouble().pow(2).roundToLong()
val even = (gridWidth.inc().floorDiv(2) * 2).toDouble().pow(2).roundToLong()
println("odd : $odd")
println("even : $even")
val oddPoints = if (useCachedValue) 7496 else part1(input, size * 2 + 1)
val evenPoints = if (useCachedValue) 7570 else part1(input, size * 2)
println()
println("oddPoints : $oddPoints")
println("evenPoints : $evenPoints")
val edgeTop = if (useCachedValue) 5670 else part1(
input = input,
repeatCount = size - 1,
sPoint = Point(startX, size - 1),
)
val edgeRight = if (useCachedValue) 5637 else part1(
input = input,
repeatCount = size - 1,
sPoint = Point(0, startY),
)
val edgeBottom = if (useCachedValue) 5623 else part1(
input = input,
repeatCount = size - 1,
sPoint = Point(startX, 0),
)
val edgeLeft = if (useCachedValue) 5656 else part1(
input = input,
repeatCount = size - 1,
sPoint = Point(size - 1, startY),
)
println()
println("edgeTop: $edgeTop")
println("edgeRight: $edgeRight")
println("edgeBottom: $edgeBottom")
println("edgeLeft: $edgeLeft")
val smallTopRight = if (useCachedValue) 980 else part1(
input = input,
repeatCount = size.floorDiv(2) - 1,
sPoint = Point(0, size - 1),
)
val smallTopLeft = if (useCachedValue) 965 else part1(
input = input,
repeatCount = size.floorDiv(2) - 1,
sPoint = Point(size - 1, size - 1),
)
val smallBottomRight = if (useCachedValue) 963 else part1(
input = input,
repeatCount = size.floorDiv(2) - 1,
sPoint = Point(0, 0),
)
val smallBottomLeft = if (useCachedValue) 945 else part1(
input = input,
repeatCount = size.floorDiv(2) - 1,
sPoint = Point(size - 1, 0),
)
val totalSmallCount: Long = (gridWidth + 1L) *
(smallBottomRight + smallBottomLeft + smallTopRight + smallTopLeft)
println()
println("smallTopRight: $smallTopRight")
println("smallTopLeft: $smallTopLeft")
println("smallBottomRight: $smallBottomRight")
println("smallBottomLeft: $smallBottomLeft")
println()
println("totalSmallCount: $totalSmallCount")
val largeTopRight = if (useCachedValue) 6584 else part1(
input = input,
repeatCount = size.times(3).floorDiv(2) - 1,
sPoint = Point(0, size - 1),
)
val largeTopLeft = if (useCachedValue) 6582 else part1(
input = input,
repeatCount = size.times(3).floorDiv(2) - 1,
sPoint = Point(size - 1, size - 1),
)
val largeBottomRight = if (useCachedValue) 6549 else part1(
input = input,
repeatCount = size.times(3).floorDiv(2) - 1,
sPoint = Point(0, 0),
)
val largeBottomLeft = if (useCachedValue) 6570 else part1(
input = input,
repeatCount = size.times(3).floorDiv(2) - 1,
sPoint = Point(size - 1, 0),
)
val totalLargeCount = (gridWidth.toLong()) *
(largeBottomRight + largeBottomLeft + largeTopRight + largeTopLeft)
println()
println("largeTopRight: $largeTopRight")
println("largeTopLeft: $largeTopLeft")
println("largeBottomRight: $largeBottomRight")
println("largeBottomLeft: $largeBottomLeft")
println()
val countTotal = odd * oddPoints +
even * evenPoints +
edgeTop + edgeBottom + edgeLeft + edgeRight +
totalSmallCount +
totalLargeCount
println("Total Count: $countTotal")
return countTotal
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 42)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 3716)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 1L)
}