|
1 | | -package de.ronny_h.aoc.extensions |
2 | | - |
3 | | -import kotlin.math.abs |
4 | | - |
5 | | -data class Coordinates(val row: Int, val col: Int) { |
6 | | - operator fun plus(other: Coordinates) = Coordinates(row + other.row, col + other.col) |
7 | | - operator fun minus(other: Coordinates) = Coordinates(row - other.row, col - other.col) |
8 | | - |
9 | | - operator fun times(other: Int) = Coordinates(row * other, col * other) |
10 | | - |
11 | | - operator fun plus(direction: Direction) = Coordinates(row + direction.row, col + direction.col) |
12 | | - |
13 | | - fun neighbours() = listOf(this + Direction.EAST, this + Direction.SOUTH, this + Direction.WEST, this + Direction.NORTH) |
14 | | - fun directedNeighbours() = listOf( |
15 | | - Direction.EAST to this + Direction.EAST, |
16 | | - Direction.SOUTH to this + Direction.SOUTH, |
17 | | - Direction.WEST to this + Direction.WEST, |
18 | | - Direction.NORTH to this + Direction.NORTH, |
19 | | - ) |
20 | | - |
21 | | - /** |
22 | | - * Calculates the taxi distance, a.k.a. Manhattan distance, between this and the other Coordinates instance. |
23 | | - */ |
24 | | - infix fun taxiDistanceTo(other: Coordinates): Int = abs(other.col - col) + abs(other.row - row) |
25 | | - |
26 | | - override fun toString() = "($row,$col)" |
27 | | -} |
28 | | - |
29 | | -operator fun Int.times(other: Coordinates) = Coordinates(this * other.row, this * other.col) |
30 | | - |
31 | | -enum class Direction(val row: Int, val col: Int) { |
32 | | - NORTH(-1, 0), |
33 | | - EAST(0, +1), |
34 | | - SOUTH(+1, 0), |
35 | | - WEST(0, -1), |
36 | | - ; |
37 | | - |
38 | | - fun turnRight() = when (this) { |
39 | | - NORTH -> EAST |
40 | | - EAST -> SOUTH |
41 | | - SOUTH -> WEST |
42 | | - WEST -> NORTH |
43 | | - } |
44 | | - |
45 | | - fun turnLeft() = when (this) { |
46 | | - NORTH -> WEST |
47 | | - EAST -> NORTH |
48 | | - SOUTH -> EAST |
49 | | - WEST -> SOUTH |
50 | | - } |
51 | | - |
52 | | - fun asChar() = when (this) { |
53 | | - NORTH -> '↑' |
54 | | - EAST -> '→' |
55 | | - SOUTH -> '↓' |
56 | | - WEST -> '←' |
57 | | - } |
58 | | - |
59 | | - fun isHorizontal(): Boolean = this == EAST || this == WEST |
60 | | - fun isVertical(): Boolean = this == NORTH || this == SOUTH |
61 | | - |
62 | | - fun isOpposite(other: Direction) = when (this) { |
63 | | - NORTH -> other == SOUTH |
64 | | - EAST -> other == WEST |
65 | | - SOUTH -> other == NORTH |
66 | | - WEST -> other == EAST |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * Returns the minimal number of 90° rotations necessary to rotate this |
71 | | - * to other. |
72 | | - */ |
73 | | - operator fun minus(other: Direction): Int { |
74 | | - return when { |
75 | | - this == other -> 0 |
76 | | - this.isOpposite(other) -> 2 |
77 | | - else -> 1 |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - override fun toString() = when (this) { |
82 | | - NORTH -> "N" |
83 | | - EAST -> "E" |
84 | | - SOUTH -> "S" |
85 | | - WEST -> "W" |
86 | | - } |
87 | | -} |
| 1 | +package de.ronny_h.aoc.extensions |
| 2 | + |
| 3 | +import kotlin.math.abs |
| 4 | + |
| 5 | +data class Coordinates(val row: Int, val col: Int) { |
| 6 | + operator fun plus(other: Coordinates) = Coordinates(row + other.row, col + other.col) |
| 7 | + operator fun minus(other: Coordinates) = Coordinates(row - other.row, col - other.col) |
| 8 | + |
| 9 | + operator fun times(other: Int) = Coordinates(row * other, col * other) |
| 10 | + |
| 11 | + operator fun plus(direction: Direction) = Coordinates(row + direction.row, col + direction.col) |
| 12 | + |
| 13 | + fun neighbours() = listOf(this + Direction.EAST, this + Direction.SOUTH, this + Direction.WEST, this + Direction.NORTH) |
| 14 | + fun directedNeighbours() = listOf( |
| 15 | + Direction.EAST to this + Direction.EAST, |
| 16 | + Direction.SOUTH to this + Direction.SOUTH, |
| 17 | + Direction.WEST to this + Direction.WEST, |
| 18 | + Direction.NORTH to this + Direction.NORTH, |
| 19 | + ) |
| 20 | + |
| 21 | + /** |
| 22 | + * Calculates the taxi distance, a.k.a. Manhattan distance, between this and the other Coordinates instance. |
| 23 | + */ |
| 24 | + infix fun taxiDistanceTo(other: Coordinates): Int = abs(other.col - col) + abs(other.row - row) |
| 25 | + |
| 26 | + override fun toString() = "($row,$col)" |
| 27 | +} |
| 28 | + |
| 29 | +operator fun Int.times(other: Coordinates) = Coordinates(this * other.row, this * other.col) |
| 30 | + |
| 31 | +enum class Direction(val row: Int, val col: Int) { |
| 32 | + NORTH(-1, 0), |
| 33 | + EAST(0, +1), |
| 34 | + SOUTH(+1, 0), |
| 35 | + WEST(0, -1), |
| 36 | + ; |
| 37 | + |
| 38 | + fun turnRight() = when (this) { |
| 39 | + NORTH -> EAST |
| 40 | + EAST -> SOUTH |
| 41 | + SOUTH -> WEST |
| 42 | + WEST -> NORTH |
| 43 | + } |
| 44 | + |
| 45 | + fun turnLeft() = when (this) { |
| 46 | + NORTH -> WEST |
| 47 | + EAST -> NORTH |
| 48 | + SOUTH -> EAST |
| 49 | + WEST -> SOUTH |
| 50 | + } |
| 51 | + |
| 52 | + fun asChar() = when (this) { |
| 53 | + NORTH -> '↑' |
| 54 | + EAST -> '→' |
| 55 | + SOUTH -> '↓' |
| 56 | + WEST -> '←' |
| 57 | + } |
| 58 | + |
| 59 | + fun isHorizontal(): Boolean = this == EAST || this == WEST |
| 60 | + fun isVertical(): Boolean = this == NORTH || this == SOUTH |
| 61 | + |
| 62 | + fun isOpposite(other: Direction) = when (this) { |
| 63 | + NORTH -> other == SOUTH |
| 64 | + EAST -> other == WEST |
| 65 | + SOUTH -> other == NORTH |
| 66 | + WEST -> other == EAST |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the minimal number of 90° rotations necessary to rotate this |
| 71 | + * to other. |
| 72 | + */ |
| 73 | + operator fun minus(other: Direction): Int { |
| 74 | + return when { |
| 75 | + this == other -> 0 |
| 76 | + this.isOpposite(other) -> 2 |
| 77 | + else -> 1 |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun toString() = when (this) { |
| 82 | + NORTH -> "N" |
| 83 | + EAST -> "E" |
| 84 | + SOUTH -> "S" |
| 85 | + WEST -> "W" |
| 86 | + } |
| 87 | +} |
0 commit comments