generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.kt
244 lines (209 loc) · 6.12 KB
/
Day18.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
238
239
240
241
242
243
244
package year2023.`18`
import readInput
import utils.printDebug
import utils.printlnDebug
import kotlin.math.absoluteValue
private const val CURRENT_DAY = "18"
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]"
}
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
}
}
enum class Direction {
BOTTOM,
TOP,
LEFT,
RIGHT;
override fun toString(): String {
return when (this) {
BOTTOM -> "B"
TOP -> "T"
LEFT -> "L"
RIGHT -> "R"
}
}
companion object {
fun fromString(string: String): Direction {
return when (string) {
"R" -> RIGHT
"L" -> LEFT
"U" -> TOP
"D" -> BOTTOM
else -> error("")
}
}
fun fromHex(string: String): Direction {
return when (string) {
"0" -> RIGHT
"1" -> BOTTOM
"2" -> LEFT
"3" -> TOP
else -> error("")
}
}
}
}
private data class Line(
val start: Point,
val end: Point,
)
data class Command(
val direction: Direction,
val amount: Int,
)
private fun parseLineIntoCommands(
line: String
): Command {
val list = line.split(" ")
return Command(
Direction.fromString(list[0]),
list[1].toInt(),
)
}
private fun parseHexLineIntoCommand(
line: String
): Command {
val list = line.split(" ")
val last = list[2].drop(1).dropLast(1)
return Command(
Direction.fromHex(last.last().toString()),
last.drop(1).dropLast(1).toInt(16),
)
}
private fun processLines(list: List<Command>): Set<Line> {
val resSet = mutableSetOf<Line>()
var currentPoint = Point(0, 0)
list.forEach { command ->
val listOf = mutableListOf<Point>()
var cur = currentPoint
repeat(command.amount) {
val newPoint = when (command.direction) {
Direction.BOTTOM -> cur.bottom
Direction.TOP -> cur.top
Direction.LEFT -> cur.left
Direction.RIGHT -> cur.right
}
listOf.add(newPoint)
cur = newPoint
}
resSet.add(Line(currentPoint, listOf.last()))
currentPoint = listOf.last()
}
return resSet
}
private fun processCommandsIntoPoints(list: List<Command>): Set<Point> {
val resSet = mutableSetOf<Point>()
var currentPoint = Point(0, 0)
list.forEach { command ->
repeat(command.amount) {
val newPoint = when (command.direction) {
Direction.BOTTOM -> currentPoint.bottom
Direction.TOP -> currentPoint.top
Direction.LEFT -> currentPoint.left
Direction.RIGHT -> currentPoint.right
}
resSet.add(currentPoint)
currentPoint = newPoint
}
}
return resSet
}
private fun printSet(set: Set<Point>) {
val minX = set.minOf { it.x }
val minY = set.minOf { it.y }
val maxX = set.maxOf { it.x }
val maxY = set.maxOf { it.y }
println()
for (i in minY..maxY) {
for (x in minX..maxX) {
val symbol = if (Point(x, i) in set) "#" else "."
printDebug { symbol }
}
printlnDebug { }
}
printlnDebug { }
}
private fun Point.neighbours(): Set<Point> {
return setOf(
Point(x + 1, y),
Point(x - 1, y),
Point(x, y + 1),
Point(x, y - 1),
)
}
private fun searchForInternalPoints(set: Set<Point>): Set<Point> {
val minX = set.minOf { it.x }
val minY = set.minOf { it.y }
val maxX = set.maxOf { it.x }
val maxY = set.maxOf { it.y }
val initialPoint = Point(1, 1)
val queue = ArrayDeque(listOf(initialPoint))
val visitedPoints = mutableSetOf<Point>()
while (queue.isNotEmpty()) {
val currentPoint = queue.removeFirst()
if (currentPoint in visitedPoints) continue
visitedPoints.add(currentPoint)
currentPoint.neighbours()
.filter { it !in set }
.filter { it.x in minX..maxX && it.y in minY..maxY }
.forEach { queue.add(it) }
}
visitedPoints.addAll(set)
return visitedPoints
}
fun main() {
fun part1(input: List<String>): Int {
val commands = input.map { parseLineIntoCommands(it) }
val points = processCommandsIntoPoints(commands)
printSet(points)
printlnDebug { }
val allPoints = searchForInternalPoints(points)
printSet(allPoints)
return allPoints.size
}
fun part2(input: List<String>): Long {
val map = input.map { parseHexLineIntoCommand(it) }
val lines = processLines(map)
val outerArea = lines.sumOf {
val xDif = (it.start.x - it.end.x).absoluteValue
val yDif = (it.start.y - it.end.y).absoluteValue
xDif + yDif
}
val internalArea = lines.sumOf {
val firstDif = it.start.x.toLong() * it.end.y.toLong()
val secondDif = it.end.x.toLong() * it.start.y.toLong()
firstDif - secondDif
}.absoluteValue / 2L
return internalArea + outerArea / 2 + 1
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 62)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 35244)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 952408144115L)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 85070763635666L)
}