Skip to content

Commit 9e3aa4d

Browse files
committed
Make Coordinates Comparable, add a turn function...
... with an enum Turn as argument.
1 parent cb9b007 commit 9e3aa4d

File tree

2 files changed

+100
-49
lines changed

2 files changed

+100
-49
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/grids/Coordinates.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package de.ronny_h.aoc.extensions.grids
33
import de.ronny_h.aoc.extensions.grids.Direction.*
44
import kotlin.math.abs
55

6-
data class Coordinates(val row: Int, val col: Int) {
6+
data class Coordinates(val row: Int, val col: Int) : Comparable<Coordinates> {
77

88
companion object {
99
val ZERO = Coordinates(0, 0)
@@ -34,6 +34,13 @@ data class Coordinates(val row: Int, val col: Int) {
3434
infix fun taxiDistanceTo(other: Coordinates): Int = abs(other.col - col) + abs(other.row - row)
3535

3636
override fun toString() = "($row,$col)"
37+
38+
override fun compareTo(other: Coordinates): Int {
39+
if (this.row == other.row) {
40+
return this.col - other.col
41+
}
42+
return this.row - other.row
43+
}
3744
}
3845

3946
operator fun Int.times(other: Coordinates) = Coordinates(this * other.row, this * other.col)
@@ -101,4 +108,14 @@ enum class Direction(val row: Int, val col: Int) {
101108
SOUTH -> "S"
102109
WEST -> "W"
103110
}
111+
112+
fun turn(turn: Turn): Direction = when (turn) {
113+
Turn.LEFT -> turnLeft()
114+
Turn.RIGHT -> turnRight()
115+
Turn.STRAIGHT -> this
116+
}
117+
}
118+
119+
enum class Turn {
120+
LEFT, RIGHT, STRAIGHT
104121
}

src/test/kotlin/de/ronny_h/aoc/extensions/grids/CoordinatesTest.kt

Lines changed: 82 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.ronny_h.aoc.extensions.grids
22

3+
import de.ronny_h.aoc.extensions.grids.Direction.*
4+
import de.ronny_h.aoc.extensions.grids.Turn.*
35
import io.kotest.core.spec.style.StringSpec
46
import io.kotest.data.forAll
57
import io.kotest.data.row
@@ -46,10 +48,10 @@ class CoordinatesTest : StringSpec({
4648

4749
"Add a direction" {
4850
forAll(
49-
row(Coordinates(5, 5), Direction.NORTH, Coordinates(4, 5)),
50-
row(Coordinates(5, 5), Direction.SOUTH, Coordinates(6, 5)),
51-
row(Coordinates(5, 5), Direction.EAST, Coordinates(5, 6)),
52-
row(Coordinates(5, 5), Direction.WEST, Coordinates(5, 4)),
51+
row(Coordinates(5, 5), NORTH, Coordinates(4, 5)),
52+
row(Coordinates(5, 5), SOUTH, Coordinates(6, 5)),
53+
row(Coordinates(5, 5), EAST, Coordinates(5, 6)),
54+
row(Coordinates(5, 5), WEST, Coordinates(5, 4)),
5355
) { coordinates, direction, result ->
5456
coordinates + direction shouldBe result
5557
}
@@ -79,76 +81,108 @@ class CoordinatesTest : StringSpec({
7981

8082
"Directed neighbours" {
8183
Coordinates(5, 5).directedNeighbours() shouldContainAll listOf(
82-
Direction.NORTH to Coordinates(4, 5),
83-
Direction.SOUTH to Coordinates(6, 5),
84-
Direction.EAST to Coordinates(5, 6),
85-
Direction.WEST to Coordinates(5, 4),
84+
NORTH to Coordinates(4, 5),
85+
SOUTH to Coordinates(6, 5),
86+
EAST to Coordinates(5, 6),
87+
WEST to Coordinates(5, 4),
8688
)
8789
}
8890

91+
"Coordinates are comparable and can be sorted" {
92+
forAll(
93+
row(listOf(Coordinates.ZERO), listOf(Coordinates.ZERO)),
94+
row(listOf(Coordinates(1, 0), Coordinates.ZERO), listOf(Coordinates.ZERO, Coordinates(1, 0))),
95+
row(
96+
listOf(Coordinates(1, 1), Coordinates(1, 0), Coordinates.ZERO),
97+
listOf(Coordinates.ZERO, Coordinates(1, 0), Coordinates(1, 1))
98+
),
99+
) { list, sorted ->
100+
list.sorted() shouldBe sorted
101+
}
102+
}
103+
89104
"Direction turnRight() turns right" {
90-
Direction.NORTH.turnRight() shouldBe Direction.EAST
91-
Direction.EAST.turnRight() shouldBe Direction.SOUTH
92-
Direction.SOUTH.turnRight() shouldBe Direction.WEST
93-
Direction.WEST.turnRight() shouldBe Direction.NORTH
105+
NORTH.turnRight() shouldBe EAST
106+
EAST.turnRight() shouldBe SOUTH
107+
SOUTH.turnRight() shouldBe WEST
108+
WEST.turnRight() shouldBe NORTH
94109
}
95110

96111
"Direction turnLeft() turns left" {
97-
Direction.NORTH.turnLeft() shouldBe Direction.WEST
98-
Direction.EAST.turnLeft() shouldBe Direction.NORTH
99-
Direction.SOUTH.turnLeft() shouldBe Direction.EAST
100-
Direction.WEST.turnLeft() shouldBe Direction.SOUTH
112+
NORTH.turnLeft() shouldBe WEST
113+
EAST.turnLeft() shouldBe NORTH
114+
SOUTH.turnLeft() shouldBe EAST
115+
WEST.turnLeft() shouldBe SOUTH
101116
}
102117

103118
"Direction reverse() does a u-turn" {
104-
Direction.NORTH.reverse() shouldBe Direction.SOUTH
105-
Direction.EAST.reverse() shouldBe Direction.WEST
106-
Direction.SOUTH.reverse() shouldBe Direction.NORTH
107-
Direction.WEST.reverse() shouldBe Direction.EAST
119+
NORTH.reverse() shouldBe SOUTH
120+
EAST.reverse() shouldBe WEST
121+
SOUTH.reverse() shouldBe NORTH
122+
WEST.reverse() shouldBe EAST
108123
}
109124

110125
"asChar gives a graphical representation" {
111-
Direction.NORTH.asChar() shouldBe ''
112-
Direction.EAST.asChar() shouldBe ''
113-
Direction.SOUTH.asChar() shouldBe ''
114-
Direction.WEST.asChar() shouldBe ''
126+
NORTH.asChar() shouldBe ''
127+
EAST.asChar() shouldBe ''
128+
SOUTH.asChar() shouldBe ''
129+
WEST.asChar() shouldBe ''
115130
}
116131

117132
"A Direction's orientation is checked right" {
118-
Direction.NORTH.isVertical() shouldBe true
119-
Direction.NORTH.isHorizontal() shouldBe false
120-
Direction.SOUTH.isVertical() shouldBe true
121-
Direction.SOUTH.isHorizontal() shouldBe false
133+
NORTH.isVertical() shouldBe true
134+
NORTH.isHorizontal() shouldBe false
135+
SOUTH.isVertical() shouldBe true
136+
SOUTH.isHorizontal() shouldBe false
122137

123-
Direction.EAST.isVertical() shouldBe false
124-
Direction.EAST.isHorizontal() shouldBe true
125-
Direction.WEST.isVertical() shouldBe false
126-
Direction.WEST.isHorizontal() shouldBe true
138+
EAST.isVertical() shouldBe false
139+
EAST.isHorizontal() shouldBe true
140+
WEST.isVertical() shouldBe false
141+
WEST.isHorizontal() shouldBe true
127142
}
128143

129144
"Opposite directions" {
130-
Direction.NORTH.isOpposite(Direction.SOUTH) shouldBe true
131-
Direction.SOUTH.isOpposite(Direction.NORTH) shouldBe true
132-
Direction.EAST.isOpposite(Direction.WEST) shouldBe true
133-
Direction.WEST.isOpposite(Direction.EAST) shouldBe true
145+
NORTH.isOpposite(SOUTH) shouldBe true
146+
SOUTH.isOpposite(NORTH) shouldBe true
147+
EAST.isOpposite(WEST) shouldBe true
148+
WEST.isOpposite(EAST) shouldBe true
134149

135-
Direction.NORTH.isOpposite(Direction.EAST) shouldBe false
136-
Direction.SOUTH.isOpposite(Direction.EAST) shouldBe false
137-
Direction.EAST.isOpposite(Direction.SOUTH) shouldBe false
138-
Direction.WEST.isOpposite(Direction.SOUTH) shouldBe false
150+
NORTH.isOpposite(EAST) shouldBe false
151+
SOUTH.isOpposite(EAST) shouldBe false
152+
EAST.isOpposite(SOUTH) shouldBe false
153+
WEST.isOpposite(SOUTH) shouldBe false
139154
}
140155

141156
"Difference between directions" {
142-
Direction.NORTH - Direction.NORTH shouldBe 0
143-
Direction.NORTH - Direction.EAST shouldBe 1
144-
Direction.NORTH - Direction.SOUTH shouldBe 2
145-
Direction.NORTH - Direction.WEST shouldBe 1
157+
NORTH - NORTH shouldBe 0
158+
NORTH - EAST shouldBe 1
159+
NORTH - SOUTH shouldBe 2
160+
NORTH - WEST shouldBe 1
146161
}
147162

148163
"toString returns the abbreviation letter" {
149-
Direction.NORTH.toString() shouldBe "N"
150-
Direction.SOUTH.toString() shouldBe "S"
151-
Direction.EAST.toString() shouldBe "E"
152-
Direction.WEST.toString() shouldBe "W"
164+
NORTH.toString() shouldBe "N"
165+
SOUTH.toString() shouldBe "S"
166+
EAST.toString() shouldBe "E"
167+
WEST.toString() shouldBe "W"
168+
}
169+
170+
"turning unsing Turns" {
171+
forAll(
172+
row(NORTH, STRAIGHT, NORTH),
173+
row(NORTH, LEFT, WEST),
174+
row(NORTH, RIGHT, EAST),
175+
row(SOUTH, STRAIGHT, SOUTH),
176+
row(SOUTH, LEFT, EAST),
177+
row(SOUTH, RIGHT, WEST),
178+
row(EAST, STRAIGHT, EAST),
179+
row(EAST, LEFT, NORTH),
180+
row(EAST, RIGHT, SOUTH),
181+
row(WEST, STRAIGHT, WEST),
182+
row(WEST, LEFT, SOUTH),
183+
row(WEST, RIGHT, NORTH),
184+
) { direction, turn, newDirection ->
185+
direction.turn(turn) shouldBe newDirection
186+
}
153187
}
154188
})

0 commit comments

Comments
 (0)