(Kotlin) day 13 part 2 wip

This commit is contained in:
kageru 2018-12-14 18:58:40 +01:00
parent 1f4296122f
commit a1cf3ad627
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 110 additions and 1 deletions

View File

@ -6,12 +6,14 @@ 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

@ -10,7 +10,6 @@ class Main() {
companion object {
public var field: MutableList<MutableList<Field>> = mutableListOf<MutableList<Field>>()
var noCrash = true
fun parseField(c: Char): Field {
return when (c) {

108
13/main_2.kt Normal file
View File

@ -0,0 +1,108 @@
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>>()
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 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()) {
if (train != t2 && train.x == t2.x && train.y == t2.y) {
return Pair(i, j)
}
}
}
return Pair(-1, -1)
}
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.y][t.x]
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 -> throw 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 ((y, line) in inputLines.withIndex()) {
var fields = mutableListOf<Field>()
for ((x, char) in line.toCharArray().withIndex()) {
if (char in TRAINS) {
val newTrain = parseTrain(char, x, y)
trains.add(newTrain)
}
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) })
var current = 0
while (trains.filter{ it.alive }.size > 1) {
while (positions.size == trains.filter{ it.alive }.size) {
if (!trains[current].alive)
continue
trains[current] = moveTrain(trains[current])
positions.clear()
positions.addAll(trains.filter{ it.alive }.map{ t -> Pair(t.x, t.y) })
current = (current + 1) % trains.size
}
val remove = cleanUpAfterCrash(trains)
println("removing $remove")
trains[remove.first].alive = false
trains[remove.second].alive = false
positions.clear()
positions.addAll(trains.filter{ it.alive }.map{ t -> Pair(t.x, t.y) })
}
}
}
}