advent-of-code/13/Cart.kt

64 lines
1.3 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-14 18:58:40 +01:00
this.alive = true
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 18:58:40 +01:00
var alive: Boolean
2018-12-13 19:07:28 +01:00
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 -> {
2018-12-14 16:27:13 +01:00
this.direction += this.nextTurn.dir
2018-12-14 00:34:55 +01:00
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) {
2018-12-14 16:27:13 +01:00
this.direction = (this.direction) % 4
2018-12-14 00:34:55 +01:00
}
}
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),
2018-12-14 16:27:13 +01:00
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
}