advent-of-code/13/Cart.kt
2019-09-24 16:52:46 +02:00

44 lines
837 B
Kotlin

package aoc.day13
public class Cart() {
constructor(d: Direction, x: Int, y: Int) {
direction = d
x = x
y = y
}
var direction: Direction
var nextTurn: Turn = Turn.LEFT
var x: Int
var y: Int
}
enum class Direction(val code: Int) {
UP(0),
RIGHT(1),
DOWN(2)
LEFT(3)
}
fun turnLeft(val oldDir: Direction): Direction {
return Direction((oldDir.code + 3) % 4)
}
fun turnRight(val oldDir: Direction): Direction {
return Direction((oldDir.code + 1) % 4)
}
fun crossIntersection(val cart: Cart): Cart {
var retCart = cart
retCart.direction = Direction((cart.direction.code + cart.nextTurn.dir - 1) % 4)
retCart.nextTurn = Turn((cart.nextTurn.dir + 1) % 3)
return retCart
}
enum class Turn(val dir: Int) {
LEFT(0),
STRAIGHT(1),
RIGHT(2)
}