-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.kt
85 lines (78 loc) · 2.71 KB
/
Day10.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
package aoc2018.day10
import java.awt.Point
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.util.*
import java.util.function.Consumer
import java.util.function.Function
import java.util.stream.Collectors
import kotlin.math.abs
fun readInput(inputFile: String): List<MovingPoint> {
val pointList: MutableList<MovingPoint> = ArrayList<MovingPoint>()
val reader: BufferedReader
try {
reader = BufferedReader(FileReader(inputFile))
var line = reader.readLine()
while (line != null) {
val input = line.trim { it <= ' '}.split(Regex("\\s+")).toTypedArray().map { s: String -> s.toInt() }.toIntArray()
pointList.add(MovingPoint(input[0], input[1], input[2], input[3]))
line = reader.readLine()
}
reader.close()
} catch (e: IOException) {
e.printStackTrace()
}
return pointList
}
private fun getListOfCurrentPoints(movingPoints: List<MovingPoint>, seconds: Int): List<Point?> {
return movingPoints.stream().map(
Function<MovingPoint, Point?> { p: MovingPoint ->
Point(
p.positionX + p.speedX * seconds,
p.positionY + p.speedY * seconds
)
}
).collect(Collectors.toList())
}
fun computeCloseCoordinates(movingPoints: List<MovingPoint>, time: Int) {
var timeVar = time
var points = getListOfCurrentPoints(movingPoints, 0)
while (abs(points[0]!!.x - points[1]!!.x) > 80) {
points = getListOfCurrentPoints(movingPoints, timeVar)
timeVar += 1
}
println(timeVar)
println(points)
}
fun compute(movingPoints: List<MovingPoint>, seconds: Int) {
val points = getListOfCurrentPoints(movingPoints, seconds)
val minX = points.minByOrNull { it!!.x }!!.x
val minY = points.minByOrNull { it!!.y }!!.y
points.forEach(Consumer { p: Point? -> p!!.setLocation(p.getX() - minX, p.getY() - minY) })
val maxX = points.maxByOrNull { it!!.x }!!.x
val maxY = points.maxByOrNull { it!!.y }!!.y
val matrix = Array(maxY) { arrayOfNulls<String>(maxX) }
for (y in 0 until maxY) {
for (x in 0 until maxX) {
matrix[y][x] = "."
}
}
// points.forEach(Consumer { p: Point? -> matrix[p!!.y][p.x] = "#" })
// for (y in 0 until maxY) {
// for (x in 0 until maxX) {
// print(matrix[y][x].toString() + " ")
// }
// println()
// }
// println()
}
class MovingPoint(val positionX: Int, val positionY: Int, val speedX: Int, val speedY: Int) {
override fun toString(): String {
return "{" + positionX +
", " + positionY +
", " + speedX +
", " + speedY +
'}'
}
}