From acee0e823b49244948750e4bedbfcfc838f538db Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 14 Dec 2018 00:34:55 +0100 Subject: [PATCH] (Kotlin) day 13 wip --- 13/Cart.kt | 84 ++++++++++++++++++++++++++++++++--------------------- 13/main.kt | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 13/notes | 3 ++ 3 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 13/main.kt create mode 100644 13/notes diff --git a/13/Cart.kt b/13/Cart.kt index a58e9d3..ec51d10 100644 --- a/13/Cart.kt +++ b/13/Cart.kt @@ -1,43 +1,61 @@ -package aoc.day13 +package aoc.thirteen -public class Cart() { - constructor(d: Direction, x: Int, y: Int) { - direction = d - x = x - y = y +public class Train { + constructor(dir: Int, x: Int, y: Int) { + this.direction = dir + this.x = x + this.y = y + this.nextTurn = Turn.LEFT } - var direction: Direction - var nextTurn: Turn = Turn.LEFT + + var direction: Int + var nextTurn: Turn var x: Int var y: Int + + fun makeTurn(turn: Turn) { + this.direction += turn.dir + correctDirection() + } + + fun crossIntersection() { + when (this.nextTurn) { + Turn.LEFT -> { + this.direction -= this.nextTurn.dir + this.nextTurn = Turn.STRAIGHT + } + Turn.STRAIGHT -> { + this.nextTurn = Turn.RIGHT + } + Turn.RIGHT -> { + this.direction += this.nextTurn.dir + this.nextTurn = Turn.LEFT + } + } + correctDirection() + } + + fun correctDirection() { + if (this.direction < 0) { + this.direction = (this.direction + 4) + } else if (this.direction > 3) { + this.direction = (this.direction + 1) % 4 + } + } } -enum class Direction(val code: Int) { - UP(0), - RIGHT(1), - DOWN(2) - LEFT(3) +public enum class Turn(val dir: Int) { + LEFT(-1), + STRAIGHT(0), + RIGHT(-1) } - -fun turnLeft(val oldDir: Direction): Direction { - return Direction((oldDir.code + 3) % 4) -} - -fun turnRight(val oldDir: Direction): Direction { - return Direction((oldDir.code + 1) % 4) -} - -fun crossIntersection(val cart: Cart): Cart { - var retCart = cart - retCart.direction = Direction((cart.direction.code + cart.nextTurn.dir - 1) % 4) - retCart.nextTurn = Turn((cart.nextTurn.dir + 1) % 3) - return retCart -} - -enum class Turn(val dir: Int) { - LEFT(0), - STRAIGHT(1), - RIGHT(2) +public enum class Field() { + VERTICAL, + HORIZONTAL, + TOP_LEFT, + TOP_RIGHT, + EMPTY, + INTERSECTION } diff --git a/13/main.kt b/13/main.kt new file mode 100644 index 0000000..8c3f4f7 --- /dev/null +++ b/13/main.kt @@ -0,0 +1,85 @@ +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> = mutableListOf>() + 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) + } + } + + 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.x][t.y] + 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 -> IllegalStateException("I shouldn’t be here") + } + return t + } + + @JvmStatic + fun main(args: Array) { + val inputStream = File("input").inputStream() + val inputLines = inputStream.bufferedReader().use { it.readText() }.split("\n") + val TRAINS = charArrayOf('<', '>', '^', 'v') + + var trains = mutableListOf() + + for ((x, line) in inputLines.withIndex()) { + var fields = mutableListOf() + for ((y, char) in line.toCharArray().withIndex()) { + if (char in TRAINS) { + trains.add(parseTrain(char, x, y)) + } + fields.add(parseField(char)) + } + field.add(fields) + } + var positions = HashSet>(trains.size) + positions.addAll(trains.map{ t -> Pair(t.x, t.y) }) + while (positions.size == trains.size) { + trains.forEach{ t -> moveTrain(t) } + positions.clear() + positions.addAll(trains.map{ t -> Pair(t.x, t.y) }) + } + println(trains) + } + } +} diff --git a/13/notes b/13/notes new file mode 100644 index 0000000..f6e988e --- /dev/null +++ b/13/notes @@ -0,0 +1,3 @@ +kotlinc main.kt Cart.kt -include-runtime -d out.jar +java -cp out.jar aoc.thirteen.Main +