aboutsummaryrefslogtreecommitdiff
path: root/src/Day17.kt
blob: 5eaf498f48eeb41b7ada7a9d30f74e322c405873 (plain)
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
import kotlin.math.max
import kotlin.math.sign

data class Target(val left: Int, val right: Int, val bottom: Int, val top: Int)

class Probe(private var vel: Pos2D, private val target: Target) {
    companion object {
        fun search(target: Target): Pair<Int, Int> {
            var highest = 0
            var count = 0

            for (vx in 0..1000) {
                for (vy in -1000..1000) {
                    val probe = Probe(Pos2D(vx, vy), target)
                    var currentHighest = 0

                    while (probe.canHitTarget) {
                        probe.step()
                        currentHighest = max(currentHighest, probe.pos.y)

                        if (probe.isInTarget) {
                            count++
                            highest = max(highest, currentHighest)
                            break
                        }
                    }
                }
            }

            return highest to count
        }
    }

    private var pos = Pos2D(0, 0)

    fun step() {
        pos += vel
        vel = vel.copy(x = vel.x - vel.x.sign, y = vel.y - 1)
    }

    val canHitTarget
        get() = pos.y > target.bottom

    val isInTarget
        get() = pos.x in target.left..target.right && pos.y in target.bottom..target.top
}

fun main() {
    fun bothParts(input: Target): Pair<Int, Int> = Probe.search(input)

    val testInput = Target(20, 30, -10, -5)
    val testOutput = bothParts(testInput)
    check(testOutput.first == 45)
    check(testOutput.second == 112)

    val input = Target(192, 251, -89, -59)
    val output = bothParts(input)
    println(output.first)
    println(output.second)
}