Skip to content

Commit

Permalink
Rename Direction.pair to Direction.toPair
Browse files Browse the repository at this point in the history
Rename the pair method to toPair to make it explicit what the type is being converted to, and to make it more in line with standard library functions, e.g. String::toInt
  • Loading branch information
GeistInDerSH committed Nov 15, 2024
1 parent 7299482 commit ba115a4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum class Direction(val rowInc: Int, val colInc: Int) {
East(0, 1),
West(0, -1);

fun pair() = rowInc to colInc
fun toPair() = rowInc to colInc

operator fun plus(pair: Pair<Int, Int>) = Pair(pair.first + rowInc, pair.second + colInc)
operator fun plus(direction: Direction) = Pair(rowInc + direction.rowInc, colInc + direction.colInc)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/geistindersh/aoc/helper/enums/Point2D.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ data class Point2D(val row: Int, val col: Int) {

fun neighbors() = listOf(Direction.North, Direction.East, Direction.South, Direction.West).map { this + it }
fun neighborsAll() = listOf(
Direction.North.pair(),
Direction.East.pair(),
Direction.South.pair(),
Direction.West.pair(),
Direction.North.toPair(),
Direction.East.toPair(),
Direction.South.toPair(),
Direction.West.toPair(),
Direction.North + Direction.East,
Direction.North + Direction.West,
Direction.South + Direction.East,
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/geistindersh/aoc/year2020/Day11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class Day11(dataFile: DataFile) {
// Use the "raw" directions as opposed to Point::neighborsAll so we can repeatedly
// add the current direction until we find a visible point
val directions = listOf(
Direction.North.pair(),
Direction.East.pair(),
Direction.South.pair(),
Direction.West.pair(),
Direction.North.toPair(),
Direction.East.toPair(),
Direction.South.toPair(),
Direction.West.toPair(),
Direction.North + Direction.East,
Direction.North + Direction.West,
Direction.South + Direction.East,
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/geistindersh/aoc/year2021/Day20.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class Day20(dataFile: DataFile) {

private fun orderedNeighbors() = listOf(
Direction.North + Direction.West,
Direction.North.pair(),
Direction.North.toPair(),
Direction.North + Direction.East,
Direction.West.pair(),
Direction.West.toPair(),
Pair(0, 0),
Direction.East.pair(),
Direction.East.toPair(),
Direction.South + Direction.West,
Direction.South.pair(),
Direction.South.toPair(),
Direction.South + Direction.East,
)

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/geistindersh/aoc/year2021/Day5.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Day5(dataFile: DataFile) {
val ewDirection = if (start.col < end.col) Direction.East else Direction.West

val direction = when {
start.row == end.row -> ewDirection.pair()
start.col == end.col -> nsDirection.pair()
start.row == end.row -> ewDirection.toPair()
start.col == end.col -> nsDirection.toPair()
else -> nsDirection + ewDirection
}
generateSequence(start) { it + direction }
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/com/geistindersh/aoc/year2022/Day23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ class Day23(dataFile: DataFile) {

private fun getAdjacency(direction: Direction) = when (direction) {
Direction.North, Direction.South -> listOf(
direction.pair(),
direction.toPair(),
direction + Direction.East,
direction + Direction.West,
)

Direction.East, Direction.West -> listOf(
direction.pair(),
direction.toPair(),
direction + Direction.North,
direction + Direction.South,
)
}

private fun getAllNeighbours() = listOf(
Direction.North.pair(),
Direction.North.toPair(),
Direction.North + Direction.East,
Direction.North + Direction.West,
Direction.South.pair(),
Direction.South.toPair(),
Direction.South + Direction.East,
Direction.South + Direction.West,
Direction.East.pair(),
Direction.West.pair(),
Direction.East.toPair(),
Direction.West.toPair(),
)

private fun rounds() = sequence {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class DigPlan(private val digLocations: List<DigLocation>) {
var area = 1.0
for (dig in digLocations) {
// Offsets that will be used to generate the ending position of the range
val (row, col) = dig.direction.pair()
val (row, col) = dig.direction.toPair()
// ending column position for the current location
colPosition += col * dig.distance
// The area added by the current location
Expand Down

0 comments on commit ba115a4

Please sign in to comment.