generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay06.kt
221 lines (190 loc) · 6.34 KB
/
Day06.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
package year2024.`06`
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import readInput
import kotlin.system.measureTimeMillis
private const val CURRENT_DAY = "06"
enum class Direction {
BOTTOM,
TOP,
LEFT,
RIGHT;
fun rotate90(): Direction {
return when (this) {
Direction.BOTTOM -> Direction.LEFT
Direction.TOP -> Direction.RIGHT
Direction.LEFT -> Direction.TOP
Direction.RIGHT -> Direction.BOTTOM
}
}
}
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)
}
private fun parseMap(input: List<String>): Pair<Point, Set<Point>> {
val mutableMap = mutableSetOf<Point>()
var point: Point? = null
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
if (value == "#") {
mutableMap.add(Point(x, y))
}
if (value == "^") {
point = Point(x, y)
}
}
}
return point!! to mutableMap
}
fun Point.isInBounds(maxX: Int, maxY: Int): Boolean {
return (x > maxX || y > maxY || x < 0 || y < 0).not()
}
data class DirPoint(
val point: Point,
val direction: Direction,
)
fun Set<Point>.moveAcrossMap(initialPoint: Point): Set<Point> {
var currentPoint = initialPoint
var currentDirection = Direction.TOP
val visitedPoints = mutableSetOf(currentPoint)
val visitedPointsDir = mutableSetOf(DirPoint(currentPoint, Direction.TOP))
val maxX = this.maxOf { it.x }
val maxY = this.maxOf { it.y }
while (currentPoint.isInBounds(maxX, maxY)) {
val visitedPointDirsBefore = visitedPointsDir.toSet()
val newCurrentPoint = when (currentDirection) {
Direction.BOTTOM -> {
val nextPoint = currentPoint.moveDown()
if (nextPoint in this) {
currentDirection = currentDirection.rotate90()
currentPoint
} else {
nextPoint
}
}
Direction.TOP -> {
val nextPoint = currentPoint.moveUp()
if (nextPoint in this) {
currentDirection = currentDirection.rotate90()
currentPoint
} else {
nextPoint
}
}
Direction.LEFT -> {
val nextPoint = currentPoint.moveLeft()
if (nextPoint in this) {
currentDirection = currentDirection.rotate90()
currentPoint
} else {
nextPoint
}
}
Direction.RIGHT -> {
val nextPoint = currentPoint.moveRight()
if (nextPoint in this) {
currentDirection = currentDirection.rotate90()
currentPoint
} else {
nextPoint
}
}
}
if (newCurrentPoint.isInBounds(maxX, maxY)) {
visitedPoints.add(newCurrentPoint)
visitedPointsDir.add(DirPoint(newCurrentPoint, currentDirection))
if (visitedPointsDir == visitedPointDirsBefore) {
return emptySet()
}
}
currentPoint = newCurrentPoint
}
return visitedPoints
}
// part 2
fun Set<Point>.generateAllNewPossibleStepsOptimal(
allInitialPoints: Set<Point>,
initialPoint: Point,
): List<Set<Point>> {
val list = mutableListOf<Set<Point>>()
this.forEach { currentPoint ->
if (currentPoint == initialPoint) return@forEach
if (currentPoint in allInitialPoints) return@forEach
list.add(allInitialPoints + currentPoint)
}
return list
}
fun main() {
fun part1(input: List<String>): Int {
val (initpoint, map) = parseMap(input)
val res = map.moveAcrossMap(initpoint)
return res.size
}
fun part2(input: List<String>): Int {
val (initPoint, map) = parseMap(input)
val visitedMap = map.moveAcrossMap(initPoint)
val allNewSteps = visitedMap.generateAllNewPossibleStepsOptimal(
allInitialPoints = map,
initialPoint = initPoint,
)
println("PART 2 VISITED POINTS SIZE: " + allNewSteps.size)
var resresres = 0
val job = GlobalScope.launch {
coroutineScope {
var totalCount = 0
val parallelised =
allNewSteps.windowed(allNewSteps.size / 24, allNewSteps.size / 24, partialWindows = true)
check(parallelised.flatten() == allNewSteps)
val reses = parallelised.map { currentList ->
async {
currentList.count { curr ->
val size = curr.moveAcrossMap(initPoint)
totalCount++
if (totalCount % 500 == 0) println("TOTAL PROCESSED: $totalCount")
size.isEmpty()
}
}
}
val resDeffereds = reses.awaitAll()
val sumOfDeffered = resDeffereds.sum()
println(resDeffereds)
resresres = sumOfDeffered
println("Final Sum Answer = $sumOfDeffered")
}
}
runBlocking { job.join() }
return resresres
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 41)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 6)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 4433)
// Part 2
val time = measureTimeMillis {
val part2 = part2(input)
println(part2)
check(part2 == 1516)
}
println("TOOK $time ms")
}