aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2022-08-05 15:00:04 +0200
committertchojnacki <tomaszchojnacki2001@gmail.com>2022-08-05 15:00:04 +0200
commit2321e17437700648a3c78a914750118a7a942ba9 (patch)
treefff2eed69f1c9d15e2f09a0874e6f8098b83c720
parent0c97ce0ce9cf56b927c875a00fbe417dc7593a48 (diff)
downloadgleam_aoc2020-2321e17437700648a3c78a914750118a7a942ba9.tar.gz
gleam_aoc2020-2321e17437700648a3c78a914750118a7a942ba9.zip
Finish day 17
-rw-r--r--README.md23
-rw-r--r--src/Day17.kt57
2 files changed, 72 insertions, 8 deletions
diff --git a/README.md b/README.md
index a6cb346..a055eac 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,14 @@
# Advent of Code 2021 in Kotlin
+
![Kotlin](https://img.shields.io/badge/Kotlin-grey?logo=Kotlin)
-![](https://img.shields.io/badge/⭐%20stars-34-yellow)
-![](https://img.shields.io/badge/📅%20days-17-blue)
+![](https://img.shields.io/badge/⭐%20stars-36-yellow)
+![](https://img.shields.io/badge/📅%20days-18-blue)
-Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][github] using the [Advent of Code Kotlin Template][template] delivered by JetBrains.
+Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][github] using
+the [Advent of Code Kotlin Template][template] delivered by JetBrains. See other solutions [here][awesome].
## Progress
+
| Day | Part 1 | Part 2 |
|---------------------------------|:------:|:------:|
| Day 1: Sonar Sweep | 🌟 | 🌟 |
@@ -24,7 +27,7 @@ Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][githu
| Day 14: Extended Polymerization | 🌟 | 🌟 |
| Day 15: Chiton | 🌟 | 🌟 |
| Day 16: Packet Decoder | 🌟 | 🌟 |
-| Day 17: Trick Shot | | |
+| Day 17: Trick Shot | 🌟 | 🌟 |
| Day 18: Snailfish | 🌟 | 🌟 |
| Day 19: Beacon Scanner | | |
| Day 20: Trench Map | | |
@@ -34,12 +37,16 @@ Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][githu
| Day 24: Arithmetic Logic Unit | | |
| Day 25: Sea Cucumber | | |
-
[^aoc]:
- [Advent of Code][aoc] – An annual event of Christmas-oriented programming challenges started December 2015.
- Every year since then, beginning on the first day of December, a programming puzzle is published every day for twenty-four days.
- You can solve the puzzle and provide an answer using the language of your choice.
+[Advent of Code][aoc] – An annual event of Christmas-oriented programming challenges started December 2015.
+Every year since then, beginning on the first day of December, a programming puzzle is published every day for
+twenty-four days.
+You can solve the puzzle and provide an answer using the language of your choice.
[aoc]: https://adventofcode.com
+
[github]: https://github.com/tchojnacki
+
[template]: https://github.com/kotlin-hands-on/advent-of-code-kotlin-template
+
+[awesome]: https://github.com/Bogdanp/awesome-advent-of-code
diff --git a/src/Day17.kt b/src/Day17.kt
index 32f47fe..5eaf498 100644
--- a/src/Day17.kt
+++ b/src/Day17.kt
@@ -1,3 +1,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)
}