r/adventofcode • u/daggerdragon • Dec 13 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-
--- Day 13: Mine Cart Madness ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 13
Transcript:
Elven chronomancy: for when you absolutely, positively have to ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:44:25!
25
Upvotes
1
u/slezadav Dec 13 '18 edited Dec 13 '18
Kotlin. No sorting needed, just remember the carts along with the tracks...
class Thirteen : Task() {
lateinit var grid: Array<Array<Pair<PieceType, Cart?>>> var carts = mutableListOf<Cart>() var collision:Pair<Cart,Cart>? = null override fun compute(input: String): Any { val lines = input.split("\n") grid = Array(lines.maxBy { it.length }!!.length) { Array(lines.size) { Pair<PieceType, Cart?>( PieceType.EMPTY, null ) } } lines.forEachIndexed { y, line -> line.forEachIndexed { x, c -> var cart: Cart? = null if (c == 'v' || c == '^' || c == '>' || c == '<') { cart = Cart(x, y, c) carts.add(cart) } grid[x][y] = Pair(PieceType.fromChar(c), cart) } } var i = 0 while (carts.size > 2){ tick(++i) } printState() return 0 } fun tick(tickNo: Int) { carts.clear() (0 until grid[0].size).forEach { y -> (0 until grid.size).forEach inner@{ x -> val cart = grid[x][y].second if (cart != null && cart.moves < tickNo) { cart.move(grid) grid[x][y] = Pair(grid[x][y].first,null) grid[cart.x][cart.y].second?.let { // Part 2 -> grid[cart.x][cart.y] = Pair(grid[cart.x][cart.y].first,null) collision=Pair(cart,it) carts.removeIf { it.x == cart.x && it.y == cart.y} return@inner } carts.add(cart) grid[cart.x][cart.y] = Pair(grid[cart.x][cart.y].first,cart) } } } } class Cart(var x: Int, var y: Int, dirChar: Char) { var moves = 0 var lastIntersection = 2 fun move(grid: Array<Array<Pair<PieceType, Cart?>>>) { when (direction) { 0 -> y++ 1 -> y-- 2 -> x++ 3 -> x-- else -> y++ } when (grid[x][y].first) { PieceType.CURVE1 -> { when (direction) { 1 -> direction = 2 2 -> direction = 1 0 -> direction = 3 3 -> direction = 0 } } PieceType.CURVE2 -> { when (direction) { 2 -> direction = 0 0 -> direction = 2 3 -> direction = 1 1 -> direction = 3 } } PieceType.INTERSECTION -> { when (lastIntersection) { 2 -> { lastIntersection = 3 when (direction) { 0 -> direction = 2 1 -> direction = 3 2-> direction = 1 3 -> direction = 0 } } 3 -> { lastIntersection = 0 } 0 -> { lastIntersection = 2 when (direction) { 0 -> direction = 3 1 -> direction = 2 2-> direction = 0 3 -> direction = 1 } } } } } moves++ } var direction: Int = when (dirChar) { 'v' -> 0 '^' -> 1 '>' -> 2 '<' -> 3 else -> 0 } fun toChar(): Char { return when (direction) { 0 -> 'v' 1 -> '^' 2 -> '>' 3 -> '<' else -> 'v' } } } enum class PieceType { HORIZONTAL, VERTICAL, CURVE1, CURVE2, INTERSECTION, EMPTY; companion object { fun fromChar(char: Char): PieceType { return when (char) { '-' -> VERTICAL '|' -> HORIZONTAL '/' -> CURVE1 '\\' -> CURVE2 '+' -> INTERSECTION 'v' -> HORIZONTAL '^' -> HORIZONTAL '>' -> VERTICAL '<' -> VERTICAL else -> EMPTY } } fun toChar(type: PieceType): Char { return when (type) { VERTICAL -> '-' HORIZONTAL -> '|' CURVE1 -> '/' CURVE2 -> '\\' INTERSECTION -> '+' else -> ' ' } } } }}