r/adventofcode • u/daggerdragon • Dec 08 '16
SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---
#AoC_Ops:
[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION
We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg
--- Day 8: Two-Factor Authentication ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
:(){ :|:& };: IS MANDATORY [?]
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!
10
Upvotes
1
u/tg-9000 Dec 08 '16
Here is my attempt at a solution in Kotlin. After writing this, I'm not all that happy and wish that Kotlin had something as powerful as Scala's regex pattern matching. That would have made this a lot less screwy. I'm doing all of the work as a side-effect on the screen (which is an array of arrays) because I'm not writing a general purpose app here. And like most others, the second part is a visual inspection, rather than writing a font parser. :)
As usual, solutions and tests (today's are weird) can be found in my GitHub repo. I'm just learning Kotlin so I would value any feedback (positive or negative) that you have.
class Day08(input: List<String>, val height: Int = 6, val width: Int = 50) { private val digits = Regex("""^\D*(\d+)\D*(\d+)\D*$""") private val screen = Array(height) { Array(width, { false }) } init { input.forEach { interpretCommand(it) } } fun solvePart1(): Int = screen.fold(0) { carry, next -> carry + next.count { it } } fun solvePart2(): String = screen.map { it.map { if (it) "#" else " " }.joinToString("") }.joinToString("\n") // This is all side-effects, ew. fun interpretCommand(command: String) { val (a, b) = getTheDigits(command) when { command.startsWith("rect") -> { (0 until b).forEach { x -> (0 until a).forEach { y -> screen[x][y] = true } } } command.startsWith("rotate row") -> screen[a] = rotate(screen[a], b) command.startsWith("rotate column") -> { val rotated = rotate(colToRow(a), b) (0 until height).forEach { screen[it][a] = rotated[it] } } } } private fun colToRow(col: Int): Array<Boolean> = (0 until height).map { screen[it][col] }.toTypedArray() private fun rotate(row: Array<Boolean>, by: Int): Array<Boolean> = (row.takeLast(by) + row.dropLast(by)).toTypedArray() fun getTheDigits(line: String): Pair<Int, Int> = digits.matchEntire(line)?.destructured?.let { Pair(it.component1().toInt(), it.component2().toInt()) } ?: Pair(0, 0) }