diff --git a/13/Cart.kt b/13/Cart.kt index ec51d10..34cc7d0 100644 --- a/13/Cart.kt +++ b/13/Cart.kt @@ -21,7 +21,7 @@ public class Train { fun crossIntersection() { when (this.nextTurn) { Turn.LEFT -> { - this.direction -= this.nextTurn.dir + this.direction += this.nextTurn.dir this.nextTurn = Turn.STRAIGHT } Turn.STRAIGHT -> { @@ -39,7 +39,7 @@ public class Train { if (this.direction < 0) { this.direction = (this.direction + 4) } 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) { LEFT(-1), STRAIGHT(0), - RIGHT(-1) + RIGHT(1) } public enum class Field() { diff --git a/13/main.kt b/13/main.kt index f93cec3..8004747 100644 --- a/13/main.kt +++ b/13/main.kt @@ -24,6 +24,14 @@ class Main() { } } + fun findCrash(trains: MutableList) { + var positions = HashSet>(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 { val dir = when (c) { '^' -> 0 @@ -43,13 +51,13 @@ 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.x][t.y] - println(current) + //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 -> t.direction = t.direction xor 1 - Field.TOP_RIGHT -> t.direction = t.direction xor 3 + 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.INTERSECTION -> t.crossIntersection() Field.EMPTY -> throw IllegalStateException("I shouldn’t be here") } @@ -69,7 +77,7 @@ class Main() { for ((x, char) in line.toCharArray().withIndex()) { if (char in TRAINS) { val newTrain = parseTrain(char, x, y) - println(newTrain) + println("train at $x, $y") println(char) trains.add(newTrain) } @@ -79,12 +87,18 @@ class Main() { } var positions = HashSet>(trains.size) positions.addAll(trains.map{ t -> Pair(t.x, t.y) }) + //println(field[75]) + var current = 0 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.addAll(trains.map{ t -> Pair(t.x, t.y) }) + current = (current + 1) % trains.size } - println(trains) + findCrash(trains) } } }