(Kotlin) day 13 wip

This commit is contained in:
kageru 2018-12-14 00:34:55 +01:00
parent 25785e49c1
commit acee0e823b
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 139 additions and 33 deletions

View File

@ -1,43 +1,61 @@
package aoc.day13
package aoc.thirteen
public class Cart() {
constructor(d: Direction, x: Int, y: Int) {
direction = d
x = x
y = y
public class Train {
constructor(dir: Int, x: Int, y: Int) {
this.direction = dir
this.x = x
this.y = y
this.nextTurn = Turn.LEFT
}
var direction: Direction
var nextTurn: Turn = Turn.LEFT
var direction: Int
var nextTurn: Turn
var x: Int
var y: Int
fun makeTurn(turn: Turn) {
this.direction += turn.dir
correctDirection()
}
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()
}
fun correctDirection() {
if (this.direction < 0) {
this.direction = (this.direction + 4)
} else if (this.direction > 3) {
this.direction = (this.direction + 1) % 4
}
}
}
enum class Direction(val code: Int) {
UP(0),
RIGHT(1),
DOWN(2)
LEFT(3)
public enum class Turn(val dir: Int) {
LEFT(-1),
STRAIGHT(0),
RIGHT(-1)
}
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)
public enum class Field() {
VERTICAL,
HORIZONTAL,
TOP_LEFT,
TOP_RIGHT,
EMPTY,
INTERSECTION
}

85
13/main.kt Normal file
View File

@ -0,0 +1,85 @@
package aoc.thirteen
import aoc.thirteen.Field
import aoc.thirteen.Turn
import aoc.thirteen.Train
import java.io.File
import java.io.InputStream
class Main() {
companion object {
public var field: MutableList<MutableList<Field>> = mutableListOf<MutableList<Field>>()
var noCrash = true
fun parseField(c: Char): Field {
return when (c) {
'|', 'v', '^' -> Field.VERTICAL
'-', '>', '<' -> Field.HORIZONTAL
'/' -> Field.TOP_LEFT
'\\' -> Field.TOP_RIGHT
'+' -> Field.INTERSECTION
' ' -> Field.EMPTY
else -> throw IllegalArgumentException("can’t parse field " + c)
}
}
fun parseTrain(c: Char, x: Int, y: Int): Train {
val dir = when (c) {
'^' -> 0
'>' -> 1
'v' -> 2
'<' -> 3
else -> throw IllegalArgumentException("this shouldn’t happen rooDerp")
}
return Train(dir, x, y)
}
fun moveTrain(t: Train): Train {
when (t.direction) {
0 -> t.y += 1
1 -> t.x += 1
2 -> t.y -= 1
3 -> t.x -= 1
else -> throw IllegalArgumentException("this shouldn’t happen either selphyDerp")
}
val current = field[t.x][t.y]
when (current) {
Field.VERTICAL, Field.HORIZONTAL -> {}
Field.TOP_LEFT -> t.direction = t.direction xor 1
Field.TOP_RIGHT -> t.direction = t.direction xor 3
Field.INTERSECTION -> t.crossIntersection()
Field.EMPTY -> IllegalStateException("I shouldn’t be here")
}
return t
}
@JvmStatic
fun main(args: Array<String>) {
val inputStream = File("input").inputStream()
val inputLines = inputStream.bufferedReader().use { it.readText() }.split("\n")
val TRAINS = charArrayOf('<', '>', '^', 'v')
var trains = mutableListOf<Train>()
for ((x, line) in inputLines.withIndex()) {
var fields = mutableListOf<Field>()
for ((y, char) in line.toCharArray().withIndex()) {
if (char in TRAINS) {
trains.add(parseTrain(char, x, y))
}
fields.add(parseField(char))
}
field.add(fields)
}
var positions = HashSet<Pair<Int, Int>>(trains.size)
positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
while (positions.size == trains.size) {
trains.forEach{ t -> moveTrain(t) }
positions.clear()
positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
}
println(trains)
}
}
}

3
13/notes Normal file
View File

@ -0,0 +1,3 @@
kotlinc main.kt Cart.kt -include-runtime -d out.jar
java -cp out.jar aoc.thirteen.Main