(Kotlin) day 13 part 1 done

This commit is contained in:
kageru 2018-12-14 16:27:13 +01:00
parent 2cba901a37
commit 1f4296122f
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 25 additions and 11 deletions

View File

@ -21,7 +21,7 @@ public class Train {
fun crossIntersection() { fun crossIntersection() {
when (this.nextTurn) { when (this.nextTurn) {
Turn.LEFT -> { Turn.LEFT -> {
this.direction -= this.nextTurn.dir this.direction += this.nextTurn.dir
this.nextTurn = Turn.STRAIGHT this.nextTurn = Turn.STRAIGHT
} }
Turn.STRAIGHT -> { Turn.STRAIGHT -> {
@ -39,7 +39,7 @@ public class Train {
if (this.direction < 0) { if (this.direction < 0) {
this.direction = (this.direction + 4) this.direction = (this.direction + 4)
} else if (this.direction > 3) { } else if (this.direction > 3) {
this.direction = (this.direction + 1) % 4 this.direction = (this.direction) % 4
} }
} }
} }
@ -48,7 +48,7 @@ public class Train {
public enum class Turn(val dir: Int) { public enum class Turn(val dir: Int) {
LEFT(-1), LEFT(-1),
STRAIGHT(0), STRAIGHT(0),
RIGHT(-1) RIGHT(1)
} }
public enum class Field() { public enum class Field() {

View File

@ -24,6 +24,14 @@ class Main() {
} }
} }
fun findCrash(trains: MutableList<Train>) {
var positions = HashSet<Pair<Int, Int>>(trains.size - 1)
trains.map{ t -> Pair(t.x, t.y) }.forEach{ p ->
if (!(p in positions)) { positions.add(p); println("adding $p") }
else { println(p) }
}
}
fun parseTrain(c: Char, x: Int, y: Int): Train { fun parseTrain(c: Char, x: Int, y: Int): Train {
val dir = when (c) { val dir = when (c) {
'^' -> 0 '^' -> 0
@ -43,13 +51,13 @@ class Main() {
3 -> t.x -= 1 3 -> t.x -= 1
else -> throw IllegalArgumentException("this shouldn’t happen either selphyDerp") else -> throw IllegalArgumentException("this shouldn’t happen either selphyDerp")
} }
println("x ${t.x}, y ${t.y}") //println("x ${t.x}, y ${t.y}")
val current = field[t.x][t.y] val current = field[t.y][t.x]
println(current) //println(current)
when (current) { when (current) {
Field.VERTICAL, Field.HORIZONTAL -> {} Field.VERTICAL, Field.HORIZONTAL -> {}
Field.TOP_LEFT -> t.direction = t.direction xor 1 Field.TOP_LEFT -> {println("hit $current, changing dir from ${t.direction} to ${t.direction xor 1}"); t.direction = t.direction xor 1}
Field.TOP_RIGHT -> t.direction = t.direction xor 3 Field.TOP_RIGHT -> {println("hit $current, changing dir from ${t.direction} to ${t.direction xor 3}"); t.direction = t.direction xor 3}
Field.INTERSECTION -> t.crossIntersection() Field.INTERSECTION -> t.crossIntersection()
Field.EMPTY -> throw IllegalStateException("I shouldn’t be here") Field.EMPTY -> throw IllegalStateException("I shouldn’t be here")
} }
@ -69,7 +77,7 @@ class Main() {
for ((x, char) in line.toCharArray().withIndex()) { for ((x, char) in line.toCharArray().withIndex()) {
if (char in TRAINS) { if (char in TRAINS) {
val newTrain = parseTrain(char, x, y) val newTrain = parseTrain(char, x, y)
println(newTrain) println("train at $x, $y")
println(char) println(char)
trains.add(newTrain) trains.add(newTrain)
} }
@ -79,12 +87,18 @@ class Main() {
} }
var positions = HashSet<Pair<Int, Int>>(trains.size) var positions = HashSet<Pair<Int, Int>>(trains.size)
positions.addAll(trains.map{ t -> Pair(t.x, t.y) }) positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
//println(field[75])
var current = 0
while (positions.size == trains.size) { while (positions.size == trains.size) {
trains.forEach{ t -> moveTrain(t) } trains[current] = moveTrain(trains[current])
//trains.forEach{ t -> moveTrain(t) }
//val pos = Pair(trains[15].y, trains[15].x)
//println("$pos: ${field[pos.first][pos.second]}: ${trains[15].direction}")
positions.clear() positions.clear()
positions.addAll(trains.map{ t -> Pair(t.x, t.y) }) positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
current = (current + 1) % trains.size
} }
println(trains) findCrash(trains)
} }
} }
} }