generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day17.kt
75 lines (61 loc) · 1.59 KB
/
Day17.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
import java.lang.Integer.max
import java.lang.Integer.min
fun main() {
data class Area(
val x1: Int,
val y1: Int,
val x2: Int,
val y2: Int
)
data class Shot(
val xVelocity: Int,
val yVelocity: Int,
val maxY: Int
)
fun List<String>.parse() = first()
.removePrefix("target area: ")
.split(", ")
.map { it.removeRange(0..1).split("..") }
.flatten()
.map { it.toInt() }
.let {
Area(
x1 = min(it[0], it[1]),
y1 = min(it[2], it[3]),
x2 = max(it[0], it[1]),
y2 = max(it[2], it[3])
)
}
fun Area.shoot(xVelocity: Int, yVelocity: Int): Shot? {
var x = 0
var y = 0
var maxY = 0
var xVel = xVelocity
var yVel = yVelocity
while (x < x2 && y > y1) {
x += xVel
xVel = max(0, xVel - 1)
y += yVel
yVel -= 1
maxY = max(maxY, y)
if (x in x1..x2 && y in y1..y2) {
return Shot(xVelocity, yVelocity, maxY)
}
}
return null
}
fun Area.solve() = (0..1000).map { xVelocity ->
(-1000..1000).mapNotNull { yVelocity ->
shoot(xVelocity, yVelocity)
}
}.flatten()
fun part1(
input: List<String>
) = input.parse().solve().maxOf { it.maxY }
fun part2(
input: List<String>
) = input.parse().solve().size
val input = readInput("Day17")
println(part1(input))
println(part2(input))
}