-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.kt
61 lines (57 loc) · 1.95 KB
/
Day13.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
package aoc2019.day13
import aoc2019.day09.processParameterMode
import aoc2019.day13.Tile.Companion.valueOf
import java.awt.Point
import java.util.*
fun processInput(numbers: ArrayList<Long>): Map<Point, Tile?> {
val tileMap: MutableMap<Point, Tile?> = HashMap()
var i = 0
var relativeBase = 0
val outputBuilder = StringBuilder()
while (i < numbers.size) {
val opCode = Math.toIntExact(numbers[i])
if (opCode == 99) {
break
}
val output = processParameterMode(numbers, i, opCode, 0, relativeBase)
if (output.code != "") {
outputBuilder.append(output.code).append(",")
}
if (output.relativeBase != 0) {
relativeBase = output.relativeBase
}
i += output.index
}
val outputs = outputBuilder.toString().split(",").toTypedArray()
i = 0
while (i < outputs.size-1) {
val x = outputs[i].toInt()
val y = outputs[i + 1].toInt()
val tileId = outputs[i + 2].toInt()
if (valueOf(tileId) != null)
tileMap[Point(x, y)] = valueOf(tileId)
i += 3
}
return tileMap
}
fun printTileMap(tileMap: Map<Point, Tile?>) {
val minX = tileMap.keys.stream().mapToInt { p: Point -> p.x }.min().orElse(-1)
val maxX = tileMap.keys.stream().mapToInt { p: Point -> p.x }.max().orElse(-1)
val minY = tileMap.keys.stream().mapToInt { p: Point -> p.y }.min().orElse(-1)
val maxY = tileMap.keys.stream().mapToInt { p: Point -> p.y }.max().orElse(-1)
for (y in minY..maxY) {
for (x in minX..maxX) {
val key = Point(x, y)
if (tileMap.containsKey(key)) {
print(tileMap[key]!!.symbol)
} else {
print(" ")
}
}
println()
}
}
fun countTilesOfType(tileType: Tile, processInput: Map<Point, Tile>): Int {
return processInput.values.stream().filter { tile: Tile -> tile == tileType }.count()
.toInt()
}