advent-of-code/13/main.kt

105 lines
3.6 KiB
Kotlin
Raw Normal View History

2018-12-14 00:34:55 +01:00
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)
}
}
2018-12-14 16:27:13 +01:00
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) }
}
}
2018-12-14 00:34:55 +01:00
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) {
2018-12-14 08:31:49 +01:00
0 -> t.y -= 1
2018-12-14 00:34:55 +01:00
1 -> t.x += 1
2018-12-14 08:31:49 +01:00
2 -> t.y += 1
2018-12-14 00:34:55 +01:00
3 -> t.x -= 1
else -> throw IllegalArgumentException("this shouldn’t happen either selphyDerp")
}
2018-12-14 16:27:13 +01:00
//println("x ${t.x}, y ${t.y}")
val current = field[t.y][t.x]
//println(current)
2018-12-14 00:34:55 +01:00
when (current) {
Field.VERTICAL, Field.HORIZONTAL -> {}
2018-12-14 16:27:13 +01:00
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}
2018-12-14 00:34:55 +01:00
Field.INTERSECTION -> t.crossIntersection()
2018-12-14 08:31:49 +01:00
Field.EMPTY -> throw IllegalStateException("I shouldn’t be here")
2018-12-14 00:34:55 +01:00
}
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>()
2018-12-14 08:31:49 +01:00
for ((y, line) in inputLines.withIndex()) {
2018-12-14 00:34:55 +01:00
var fields = mutableListOf<Field>()
2018-12-14 08:31:49 +01:00
for ((x, char) in line.toCharArray().withIndex()) {
2018-12-14 00:34:55 +01:00
if (char in TRAINS) {
2018-12-14 08:31:49 +01:00
val newTrain = parseTrain(char, x, y)
2018-12-14 16:27:13 +01:00
println("train at $x, $y")
2018-12-14 08:31:49 +01:00
println(char)
trains.add(newTrain)
2018-12-14 00:34:55 +01:00
}
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) })
2018-12-14 16:27:13 +01:00
//println(field[75])
var current = 0
2018-12-14 00:34:55 +01:00
while (positions.size == trains.size) {
2018-12-14 16:27:13 +01:00
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}")
2018-12-14 00:34:55 +01:00
positions.clear()
positions.addAll(trains.map{ t -> Pair(t.x, t.y) })
2018-12-14 16:27:13 +01:00
current = (current + 1) % trains.size
2018-12-14 00:34:55 +01:00
}
2018-12-14 16:27:13 +01:00
findCrash(trains)
2018-12-14 00:34:55 +01:00
}
}
}