Skip to content

Commit a255ad0

Browse files
committed
A 3D vector implementation
With * addition * subtraction * absolute * taxi distance (= Manhattan distance) * scalar multiplication with Int and Long
1 parent 21a4d35 commit a255ad0

File tree

2 files changed

+86
-0
lines changed
  • src
    • main/kotlin/de/ronny_h/aoc/extensions/threedim
    • test/kotlin/de/ronny_h/aoc/extensions/threedim

2 files changed

+86
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.ronny_h.aoc.extensions.threedim
2+
3+
import kotlin.math.abs
4+
import kotlin.math.cbrt
5+
6+
data class Vector(val x: Long, val y: Long, val z: Long) {
7+
companion object {
8+
val ZERO = Vector(0, 0, 0)
9+
}
10+
11+
operator fun plus(other: Vector) = Vector(x + other.x, y + other.y, z + other.z)
12+
operator fun minus(other: Vector) = Vector(x - other.x, y - other.y, z - other.z)
13+
infix fun taxiDistanceTo(other: Vector): Long = abs(other.x - x) + abs(other.y - y) + abs(other.z - z)
14+
fun abs(): Double = cbrt(x * x.toDouble() + y * y.toDouble() + z * z.toDouble())
15+
}
16+
17+
operator fun Int.times(vector: Vector) = Vector(this * vector.x, this * vector.y, this * vector.z)
18+
operator fun Long.times(vector: Vector) = Vector(this * vector.x, this * vector.y, this * vector.z)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package de.ronny_h.aoc.extensions.threedim
2+
3+
import de.ronny_h.aoc.extensions.threedim.Vector.Companion.ZERO
4+
import io.kotest.core.spec.style.StringSpec
5+
import io.kotest.data.forAll
6+
import io.kotest.data.row
7+
import io.kotest.matchers.shouldBe
8+
9+
class VectorTest : StringSpec({
10+
"vectors can be added" {
11+
forAll(
12+
row(Vector(0, 0, 0), Vector(0, 0, 0), Vector(0, 0, 0)),
13+
row(Vector(10, 20, 30), Vector(1, 2, 3), Vector(11, 22, 33)),
14+
row(Vector(10, 20, 30), Vector(-1, -2, -3), Vector(9, 18, 27)),
15+
) { a, b, sum ->
16+
a + b shouldBe sum
17+
}
18+
}
19+
20+
"vectors can be subtracted" {
21+
forAll(
22+
row(Vector(0, 0, 0), Vector(0, 0, 0), Vector(0, 0, 0)),
23+
row(Vector(10, 20, 30), Vector(1, 2, 3), Vector(9, 18, 27)),
24+
row(Vector(10, 20, 30), Vector(-1, -2, -3), Vector(11, 22, 33)),
25+
) { a, b, difference ->
26+
a - b shouldBe difference
27+
}
28+
}
29+
30+
"the taxi distance of vectors" {
31+
forAll(
32+
row(Vector(0, 0, 0), Vector(0, 0, 0), 0),
33+
row(Vector(10, 20, 30), Vector(11, 22, 33), 6),
34+
row(Vector(10, 20, 30), Vector(9, 18, 27), 6),
35+
) { a, b, distance ->
36+
a taxiDistanceTo b shouldBe distance
37+
}
38+
}
39+
40+
"the absolute of a vector" {
41+
forAll(
42+
row(Vector(0, 0, 0), 0.0),
43+
row(Vector(3, 3, 3), 3.0),
44+
) { v, absolute ->
45+
v.abs() shouldBe absolute
46+
}
47+
}
48+
49+
"scalar multiplication with an Int" {
50+
forAll(
51+
row(0, Vector(1, 2, 3), ZERO),
52+
row(1, Vector(1, 2, 3), Vector(1, 2, 3)),
53+
row(10, Vector(1, 2, 3), Vector(10, 20, 30)),
54+
) { a, v, product ->
55+
a * v shouldBe product
56+
}
57+
}
58+
59+
"scalar multiplication with a Long" {
60+
forAll(
61+
row(0L, Vector(1, 2, 3), ZERO),
62+
row(1L, Vector(1, 2, 3), Vector(1, 2, 3)),
63+
row(10L, Vector(1, 2, 3), Vector(10, 20, 30)),
64+
) { a, v, product ->
65+
a * v shouldBe product
66+
}
67+
}
68+
})

0 commit comments

Comments
 (0)