(Kotlin) day 13 part 2 done

This commit is contained in:
kageru 2018-12-14 23:34:16 +01:00
parent 606f4f2ace
commit 21a27d5fc7
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 9 additions and 19 deletions

View File

@ -6,14 +6,12 @@ public class Train {
this.x = x
this.y = y
this.nextTurn = Turn.LEFT
this.alive = true
}
var direction: Int
var nextTurn: Turn
var x: Int
var y: Int
var alive: Boolean
fun makeTurn(turn: Turn) {
this.direction += turn.dir

View File

@ -26,8 +26,8 @@ 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) }
if (p !in positions) positions.add(p)
else println(p)
}
}
@ -50,13 +50,11 @@ class Main() {
3 -> t.x -= 1
else -> throw IllegalArgumentException("this shouldn’t happen either selphyDerp")
}
//println("x ${t.x}, y ${t.y}")
val current = field[t.y][t.x]
//println(current)
when (current) {
Field.VERTICAL, Field.HORIZONTAL -> {}
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 -> {println("hit $current, changing dir from ${t.direction} to ${t.direction xor 3}"); t.direction = t.direction xor 3}
Field.TOP_LEFT -> t.direction = t.direction xor 1
Field.TOP_RIGHT -> t.direction = t.direction xor 3
Field.INTERSECTION -> t.crossIntersection()
Field.EMPTY -> throw IllegalStateException("I shouldn’t be here")
}
@ -76,8 +74,6 @@ class Main() {
for ((x, char) in line.toCharArray().withIndex()) {
if (char in TRAINS) {
val newTrain = parseTrain(char, x, y)
println("train at $x, $y")
println(char)
trains.add(newTrain)
}
fields.add(parseField(char))
@ -86,13 +82,9 @@ class Main() {
}
var positions = HashSet<Pair<Int, Int>>(trains.size)
positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
//println(field[75])
var current = 0
while (positions.size == trains.size) {
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.addAll(trains.map{ t -> Pair(t.x, t.y) })
current = (current + 1) % trains.size

View File

@ -24,8 +24,8 @@ class Main() {
}
fun cleanUpAfterCrash(trains: MutableList<Train>): Pair<Int, Int> {
for ((i, train) in trains.filter{ it.alive }.withIndex()) {
for ((j, t2) in trains.filter{ it.alive }.withIndex()) {
for ((i, train) in trains.withIndex()) {
for ((j, t2) in trains.withIndex()) {
if (train != t2 && train.x == t2.x && train.y == t2.y) {
return Pair(i, j)
}
@ -92,9 +92,9 @@ class Main() {
trains[i] = moveTrain(train)
positions.clear()
positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
if (trains.size > positions.size) {
// This is not ideal, but there is never more than one crash per tick
if (trains.size > positions.size && !crash) {
remove = cleanUpAfterCrash(trains)
println("removing $remove")
crash = true
}
}
@ -102,9 +102,9 @@ class Main() {
trains.removeAll(listOf(trains[remove.first], trains[remove.second]))
crash = false
}
trains = trains.sortedBy{ it.y * 200 + it.x }.toMutableList()
}
println("${trains[0].x},${trains[0].y}")
println("done")
}
}
}