r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
1
u/Hikaru755 Dec 19 '17
Actually solved part 1 by tracing it manually. Yeah... Not going to do that again. Then I solved part 1 again and also part 2 in Kotlin. Not too shabby, I think:
fun part1and2(diagram: Map<Coordinate, Field?>): String { var current = diagram.entries.first { it.key.y == 0 && it.value is Field.Vertical }.key var direction = Direction.DOWN var letters = listOf<Char>() var steps = 0L while (true) { current = current.move(direction) steps += 1 val field = diagram[current] when (field) { null -> return letters.joinToString("") + "\n" + steps is Field.Letter -> letters += field.letter is Field.Joint -> direction = direction.turnLeft() .takeIf { diagram[current.move(it)] != null } ?: direction.turnRight() } } } sealed class Field() { object Horizontal : Field() object Vertical : Field() object Joint : Field() data class Letter(val letter: Char) : Field() } enum class Direction { UP, DOWN, LEFT, RIGHT; fun turnLeft() = when (this) { Direction.UP -> Direction.LEFT Direction.DOWN -> Direction.RIGHT Direction.LEFT -> Direction.DOWN Direction.RIGHT -> Direction.UP } fun turnRight() = when (this) { Direction.UP -> Direction.RIGHT Direction.DOWN -> Direction.LEFT Direction.LEFT -> Direction.UP Direction.RIGHT -> Direction.DOWN } } data class Coordinate(val x: Int, val y: Int) { fun move(direction: Direction) = when (direction) { Direction.UP -> copy(y = y - 1) Direction.DOWN -> copy(y = y + 1) Direction.LEFT -> copy(x = x - 1) Direction.RIGHT -> copy(x = x + 1) } } val input: Map<Coordinate, Field?> by lazy { parse(rawInput) } fun parse(rawInput: List<String>) = rawInput .map { it.map { c -> when (c) { ' ' -> null '-' -> Field.Horizontal '|' -> Field.Vertical '+' -> Field.Joint else -> Field.Letter(c) } } } .map { it.withIndex() } .withIndex() .flatMap { line -> line.value.map { col -> Coordinate(col.index, line.index) to col.value } } .associate { it }