Skip to content

Commit 91b3738

Browse files
committed
Swap x an y values in Coordinates
This is a more common notation of coordinates.
1 parent 30cd7f2 commit 91b3738

File tree

25 files changed

+172
-157
lines changed

25 files changed

+172
-157
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ 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 y: Int, val x: Int) : Comparable<Coordinates> {
6+
data class Coordinates(val x: Int, val y: Int) : Comparable<Coordinates> {
77

88
companion object {
99
val ZERO = Coordinates(0, 0)
1010
}
1111

12-
operator fun plus(other: Coordinates) = Coordinates(y + other.y, x + other.x)
13-
operator fun minus(other: Coordinates) = Coordinates(y - other.y, x - other.x)
12+
operator fun plus(other: Coordinates) = Coordinates(x + other.x, y + other.y)
13+
operator fun minus(other: Coordinates) = Coordinates(x - other.x, y - other.y)
1414

15-
operator fun times(other: Int) = Coordinates(y * other, x * other)
15+
operator fun times(other: Int) = Coordinates(x * other, y * other)
1616

17-
operator fun plus(direction: Direction) = Coordinates(y + direction.y, x + direction.x)
17+
operator fun plus(direction: Direction) = Coordinates(x + direction.x, y + direction.y)
1818

1919
fun neighbours() = listOf(this + EAST, this + SOUTH, this + WEST, this + NORTH)
2020

@@ -39,20 +39,20 @@ data class Coordinates(val y: Int, val x: Int) : Comparable<Coordinates> {
3939
* Orders [Coordinates] in reading order: top-to-bottom, then left-to-right.
4040
*/
4141
override fun compareTo(other: Coordinates): Int {
42-
if (this.y == other.y) {
43-
return this.x - other.x
42+
if (y == other.y) {
43+
return x - other.x
4444
}
45-
return this.y - other.y
45+
return y - other.y
4646
}
4747
}
4848

49-
operator fun Int.times(other: Coordinates) = Coordinates(this * other.y, this * other.x)
49+
operator fun Int.times(other: Coordinates) = Coordinates(this * other.x, this * other.y)
5050

51-
enum class Direction(val y: Int, val x: Int) {
52-
NORTH(-1, 0),
53-
EAST(0, +1),
54-
SOUTH(+1, 0),
55-
WEST(0, -1),
51+
enum class Direction(val x: Int, val y: Int) {
52+
NORTH(0, -1),
53+
EAST(+1, 0),
54+
SOUTH(0, +1),
55+
WEST(-1, 0),
5656
;
5757

5858
fun turnRight() = when (this) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ abstract class Grid<T>(
120120
fun <R> forEachCoordinates(action: (position: Coordinates, element: T) -> R): Sequence<R> = sequence {
121121
for (row in grid.indices) {
122122
for (col in grid[0].indices) {
123-
yield(action(Coordinates(row, col), get(row, col)))
123+
yield(action(Coordinates(col, row), get(row, col)))
124124
}
125125
}
126126
}
@@ -131,7 +131,7 @@ abstract class Grid<T>(
131131
* @throws NoSuchElementException If no matching element can be found.
132132
*/
133133
fun find(value: T): Coordinates = forEachElement { row, col, element ->
134-
if (element == value) Coordinates(row, col) else null
134+
if (element == value) Coordinates(col, row) else null
135135
}.filterNotNull().first()
136136

137137
override fun toString(): String = toString(setOf())

src/main/kotlin/de/ronny_h/aoc/year2015/day06/FireHazard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FireHazard : AdventOfCode<Int>(2015, 6) {
3939
val (from, to) = line.substring(action.text.length).split(" through ")
4040
val (fromRow, fromCol) = from.split(',').map(String::toInt)
4141
val (toRow, toCol) = to.split(',').map(String::toInt)
42-
return SwitchLight(action, Coordinates(fromRow, fromCol), Coordinates(toRow, toCol))
42+
return SwitchLight(action, Coordinates(fromCol, fromRow), Coordinates(toCol, toRow))
4343
}
4444

4545
private val dimmingGrid = List(1000) {

src/main/kotlin/de/ronny_h/aoc/year2015/day18/LikeAGIFForYourYard.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class LikeAGIFForYourYard : AdventOfCode<Int>(2015, 18) {
3838

3939
private fun GameOfLightGrid.withCornersStuckOn(): GameOfLightGrid {
4040
setAt(Coordinates(0, 0), on)
41-
setAt(Coordinates(0, width - 1), on)
42-
setAt(Coordinates(height - 1, 0), on)
43-
setAt(Coordinates(height - 1, width - 1), on)
41+
setAt(Coordinates(width - 1, 0), on)
42+
setAt(Coordinates(0, height - 1), on)
43+
setAt(Coordinates(width - 1, height - 1), on)
4444
return this
4545
}
4646
}

src/main/kotlin/de/ronny_h/aoc/year2015/day25/LetItSnow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fun List<String>.parseCoordinates(): Coordinates {
1818
val (row, col) = first()
1919
.substringAfter("Enter the code at row ")
2020
.split(", column ")
21-
return Coordinates(row.toInt(), col.substring(0, col.lastIndex).toInt())
21+
return Coordinates(col.substring(0, col.lastIndex).toInt(), row.toInt())
2222
}
2323

2424
fun nextCodeFor(code: Int): Int = ((code * 252533L) % 33554393).toInt()

src/main/kotlin/de/ronny_h/aoc/year2017/day21/FractalArt.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FractalArtGrid(size: Int) : Grid<Char>(size, size, off) {
6666
if (squareSize == 2 && rule.onCount != 2 || rule.ruleMatches(square) || rule.ruleMatches(square.reversed())) {
6767
newGrid.applyRuleAt(
6868
rule,
69-
Coordinates(start.y + (start.y / squareSize), start.x + (start.x / squareSize))
69+
Coordinates(start.x + (start.x / squareSize), start.y + (start.y / squareSize))
7070
)
7171
applied = true
7272
break
@@ -123,7 +123,7 @@ class FractalArtGrid(size: Int) : Grid<Char>(size, size, off) {
123123
private fun forEachSquareOfSize(size: Int, block: (Coordinates, List<List<Char>>) -> Unit) {
124124
for (row in 0..<height step size) {
125125
for (col in 0..<height step size) {
126-
block(Coordinates(row, col), subGridAt(row, col, size))
126+
block(Coordinates(col, row), subGridAt(row, col, size))
127127
}
128128
}
129129
}

src/main/kotlin/de/ronny_h/aoc/year2017/day22/SporificaVirus.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ class SeeminglyInfiniteGrid(input: List<String>) {
3131
init {
3232
input.forEachIndexed { row, line ->
3333
line.forEachIndexed { col, node ->
34-
grid[Coordinates(row, col)] = node
34+
grid[Coordinates(col, row)] = node
3535
}
3636
}
3737
}
3838

3939
private var direction = NORTH
40-
private var position = Coordinates(input.size / 2, input[0].length / 2)
40+
private var position = Coordinates(input[0].length / 2, input.size / 2)
4141
var causedInfection = 0
4242
private set
4343

src/main/kotlin/de/ronny_h/aoc/year2018/day03/NoMatterHowYouSliceIt.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ClaimedAreas(input: List<String>) {
3535
claims.forEach {
3636
for (x in it.x..<it.x + it.width) {
3737
for (y in it.y..<it.y + it.height) {
38-
claimedAreas[Coordinates(y, x)] = claimedAreas.getValue(Coordinates(y, x)) + 1
38+
claimedAreas[Coordinates(x, y)] = claimedAreas.getValue(Coordinates(x, y)) + 1
3939
}
4040
}
4141
}
@@ -47,7 +47,7 @@ class ClaimedAreas(input: List<String>) {
4747
.filter {
4848
for (x in it.x..<it.x + it.width) {
4949
for (y in it.y..<it.y + it.height) {
50-
if (claimedAreas.getValue(Coordinates(y, x)) > 1) return@filter false
50+
if (claimedAreas.getValue(Coordinates(x, y)) > 1) return@filter false
5151
}
5252
}
5353
true

src/main/kotlin/de/ronny_h/aoc/year2018/day06/ChronalCoordinates.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ChronalCoordinates : AdventOfCode<Int>(2018, 6) {
1818

1919
fun List<String>.parseCoordinates() = map {
2020
val (col, row) = it.split(", ")
21-
Coordinates(row.toInt(), col.toInt())
21+
Coordinates(col.toInt(), row.toInt())
2222
}
2323

2424
class AreaGrid(private val coordinates: List<Coordinates>) {
@@ -52,7 +52,7 @@ class AreaGrid(private val coordinates: List<Coordinates>) {
5252
private fun allGridCoordinates() = sequence {
5353
for (row in min.y..max.y) {
5454
for (col in min.x..max.x) {
55-
yield(Coordinates(row, col))
55+
yield(Coordinates(col, row))
5656
}
5757
}
5858
}
@@ -61,12 +61,12 @@ class AreaGrid(private val coordinates: List<Coordinates>) {
6161
// coordinates on the border are part of infinite areas -> filter them out
6262
val borderAreas = mutableSetOf<Coordinates>()
6363
for (col in min.x..max.x) {
64-
grid[Coordinates(min.y, col)]?.let { borderAreas.add(it) }
65-
grid[Coordinates(max.y, col)]?.let { borderAreas.add(it) }
64+
grid[Coordinates(col, min.y)]?.let { borderAreas.add(it) }
65+
grid[Coordinates(col, max.y)]?.let { borderAreas.add(it) }
6666
}
6767
for (row in min.y..max.y) {
68-
grid[Coordinates(row, min.x)]?.let { borderAreas.add(it) }
69-
grid[Coordinates(row, max.x)]?.let { borderAreas.add(it) }
68+
grid[Coordinates(min.x, row)]?.let { borderAreas.add(it) }
69+
grid[Coordinates(max.x, row)]?.let { borderAreas.add(it) }
7070
}
7171
return coordinates.toSet() - borderAreas
7272
}
@@ -82,6 +82,6 @@ class AreaGrid(private val coordinates: List<Coordinates>) {
8282
maxRow = max(maxRow, it.y)
8383
maxCol = max(maxCol, it.x)
8484
}
85-
return Coordinates(minRow, minCol) to Coordinates(maxRow, maxCol)
85+
return Coordinates(minCol, minRow) to Coordinates(maxCol, maxRow)
8686
}
8787
}

src/main/kotlin/de/ronny_h/aoc/year2018/day10/TheStarsAlign.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class StarAlignmentGrid(initStars: List<Star>) {
4747
val (min, max) = findBorders()
4848
for (row in min.y..max.y) {
4949
for (col in min.x..max.x) {
50-
if (Coordinates(row, col) in starPositions()) {
50+
if (Coordinates(col, row) in starPositions()) {
5151
print('#')
5252
} else {
5353
print(' ')
@@ -88,15 +88,15 @@ class StarAlignmentGrid(initStars: List<Star>) {
8888
private fun coordinatesColumnWise(min: Coordinates, max: Coordinates) = sequence {
8989
for (col in min.x..max.x) {
9090
for (row in min.y..max.y) {
91-
yield(Coordinates(row, col))
91+
yield(Coordinates(col, row))
9292
}
9393
}
9494
}
9595

9696
private fun coordinatesRowWise(min: Coordinates, max: Coordinates) = sequence {
9797
for (row in min.y..max.y) {
9898
for (col in min.x..max.x) {
99-
yield(Coordinates(row, col))
99+
yield(Coordinates(col, row))
100100
}
101101
}
102102
}
@@ -106,7 +106,7 @@ class StarAlignmentGrid(initStars: List<Star>) {
106106
val minCol = stars.minOf { it.position.x }
107107
val maxRow = stars.maxOf { it.position.y }
108108
val maxCol = stars.maxOf { it.position.x }
109-
return Coordinates(minRow, minCol) to Coordinates(maxRow, maxCol)
109+
return Coordinates(minCol, minRow) to Coordinates(maxCol, maxRow)
110110
}
111111

112112
companion object {
@@ -119,7 +119,7 @@ class StarAlignmentGrid(initStars: List<Star>) {
119119
val (pCol, pRow, vCol, vRow) = (1..4).map {
120120
matcher.group(it).trim().toInt()
121121
}
122-
Star(Coordinates(pRow, pCol), Coordinates(vRow, vCol))
122+
Star(Coordinates(pCol, pRow), Coordinates(vCol, vRow))
123123
}
124124
}
125125
}

0 commit comments

Comments
 (0)