advent-of-code/13/Cart.kt

62 lines
1.2 KiB
Kotlin
Raw Normal View History

2018-12-14 00:34:55 +01:00
package aoc.thirteen
public class Train {
constructor(dir: Int, x: Int, y: Int) {
this.direction = dir
this.x = x
this.y = y
this.nextTurn = Turn.LEFT
2018-12-13 19:07:28 +01:00
}
2018-12-14 00:34:55 +01:00
var direction: Int
var nextTurn: Turn
2018-12-13 19:07:28 +01:00
var x: Int
var y: Int
2018-12-14 00:34:55 +01:00
fun makeTurn(turn: Turn) {
this.direction += turn.dir
correctDirection()
}
2018-12-13 19:07:28 +01:00
2018-12-14 00:34:55 +01:00
fun crossIntersection() {
when (this.nextTurn) {
Turn.LEFT -> {
this.direction -= this.nextTurn.dir
this.nextTurn = Turn.STRAIGHT
}
Turn.STRAIGHT -> {
this.nextTurn = Turn.RIGHT
}
Turn.RIGHT -> {
this.direction += this.nextTurn.dir
this.nextTurn = Turn.LEFT
}
}
correctDirection()
}
2018-12-13 19:07:28 +01:00
2018-12-14 00:34:55 +01:00
fun correctDirection() {
if (this.direction < 0) {
this.direction = (this.direction + 4)
} else if (this.direction > 3) {
this.direction = (this.direction + 1) % 4
}
}
2018-12-13 19:07:28 +01:00
}
2018-12-14 00:34:55 +01:00
public enum class Turn(val dir: Int) {
LEFT(-1),
STRAIGHT(0),
RIGHT(-1)
2018-12-13 19:07:28 +01:00
}
2018-12-14 00:34:55 +01:00
public enum class Field() {
VERTICAL,
HORIZONTAL,
TOP_LEFT,
TOP_RIGHT,
EMPTY,
INTERSECTION
2018-12-13 19:07:28 +01:00
}