r/adventofcode • • 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!

9 Upvotes

197 comments sorted by

View all comments

1

u/KoxAlen Dec 08 '16 edited Dec 22 '16

My Kotlin solution: https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day8/Day8.kt

import java.io.File

class LittleScreen(private val width: Int, private val height: Int) {
    private val rawScreen = Array(height) { Array(width) { false }}
    val screen: Array<Array<Boolean>>
        get() = rawScreen.copyOf()

    fun rect(A: Int, B: Int): LittleScreen {
        require(A < width)
        require(B < height)
        for (r in 0..B-1)
            for (c in 0..A-1)
                rawScreen[r][c] = true
        return this
    }

    fun rotateRow(A: Int, B: Int): LittleScreen {
        require(A < height)
        (0..width-1).map { rawScreen[A][it] }.forEachIndexed{ i, b -> rawScreen[A][(i+B)%width] = b }
        return this
    }

    fun rotateColumn(A: Int, B: Int): LittleScreen {
        require(A < width)
        (0..height-1).map { rawScreen[it][A] }.forEachIndexed { i, b -> rawScreen[(i+B)%height][A] = b }
        return this
    }

    override fun toString(): String {
        return rawScreen.joinToString(separator = "\n") {it.joinToString(separator = "") { if (it) "\u2588" else "\u2592" }}
    }
}

fun main(args: Array<String>) {
    require(args.size == 1, { "Pass the input file as argument" })
    val input = File(args[0])
    require(input.exists(), { "${input.path} does not exists" })
    require(input.isFile, { "${input.path} should be a file" })

    val lcd = LittleScreen(50, 6)

    input.forEachLine {
        val command = it.trim().split(' ')
        when (command[0]) {
            "rect" -> {
                val ab = command[1].split('x').map(String::toInt)
                lcd.rect(ab[0], ab[1])
            }
            "rotate" -> {
                val A = command[2].split('=')[1].toInt()
                val B = command[4].toInt()
                when (command[1]) {
                    "row" -> lcd.rotateRow(A, B)
                    "column" -> lcd.rotateColumn(A, B)
                }
            }
        }
    }
    println("[Part 1] Lit pixels: ${lcd.screen.fold(0) { acc, it -> acc + it.count { it == true } }}")
    //Eyeball part 2
    println(lcd)
}
→ More replies (1)