diff options
author | tchojnacki <tomaszchojnacki2001@gmail.com> | 2022-08-11 12:01:44 +0200 |
---|---|---|
committer | tchojnacki <tomaszchojnacki2001@gmail.com> | 2022-08-11 12:01:44 +0200 |
commit | 34c2414304d59e76d52b96efe6bebebb4e75f086 (patch) | |
tree | 2b78254a2a909521993caa459b24ab68fae43324 /2021-kotlin/src/Day17.kt | |
parent | 8742021f42d2c5417abad25bfdb6d7abbf6e759e (diff) | |
download | gleam_aoc2020-34c2414304d59e76d52b96efe6bebebb4e75f086.tar.gz gleam_aoc2020-34c2414304d59e76d52b96efe6bebebb4e75f086.zip |
Move year 2021 into a subfolder
Diffstat (limited to '2021-kotlin/src/Day17.kt')
-rw-r--r-- | 2021-kotlin/src/Day17.kt | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/2021-kotlin/src/Day17.kt b/2021-kotlin/src/Day17.kt new file mode 100644 index 0000000..5e0170b --- /dev/null +++ b/2021-kotlin/src/Day17.kt @@ -0,0 +1,62 @@ +import kotlin.math.max +import kotlin.math.sign + +object Day17 { + data class Target(val left: Int, val right: Int, val bottom: Int, val top: Int) + + private 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) + + private fun step() { + pos += vel + vel = vel.copy(x = vel.x - vel.x.sign, y = vel.y - 1) + } + + private val canHitTarget + get() = pos.y > target.bottom + + private val isInTarget + get() = pos.x in target.left..target.right && pos.y in target.bottom..target.top + } + + fun bothParts(input: Target) = Probe.search(input) +} + +fun main() { + val testInput = Day17.Target(20, 30, -10, -5) + val testOutput = Day17.bothParts(testInput) + check(testOutput.first == 45) + check(testOutput.second == 112) + + val input = Day17.Target(192, 251, -89, -59) + val output = Day17.bothParts(input) + println(output.first) + println(output.second) +} |