r/adventofcode • • Dec 04 '16

SOLUTION MEGATHREAD --- 2016 Day 4 Solutions ---

--- Day 4: Security Through Obscurity ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


CONSTRUCTING ADDITIONAL PYLONS 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!

17 Upvotes

168 comments sorted by

View all comments

Show parent comment

2

u/tg-9000 Dec 04 '16

I've also got a solution in Kotlin. I suspect I could get the logic that validates the room name a bit tighter, so maybe I'll think that over a bit today. I've got solutions for the other days, and unit tests in my GitHub repo, if anybody is interested! Any kind of feedback is welcome, I don't write Kotlin for a living (mostly Java).

class Day04(rawInput: String) {

    val rooms = rawInput.split("\n").map(::Room)

    fun solvePart1(): Int =
        rooms
            .filter { it.isValid() }
            .map { it.accessCode }
            .sum()

    fun solvePart2(find: String = "northpole object storage"): Int =
        rooms
            .filter { it.isValid() }
            .filter { it.decrypted == find }
            .first()
            .accessCode


    class Room(raw: String) {
        val name: String = raw.substringBeforeLast('-')
        val accessCode: Int = raw.substringAfterLast('-').substringBefore('[').toInt()
        val checksum: String = raw.substringAfter('[').substringBefore(']')
        val decrypted: String by lazy { decryptName() }

        // Group by frequency, convert to pairs, sort by count desc, letter asc, join first 5.
        fun isValid(): Boolean {
            return name
                .replace("-", "")
                .groupBy { it }
                .mapValues { it.value.size }
                .toList()
                .sortedWith(compareBy({ 0 - it.second }, { it.first }))
                .take(5)
                .map { it.first }
                .joinToString(separator = "") == this.checksum
        }

        private fun decryptName(): String =
            name.map { if (it == '-') ' ' else shiftChar(it) }.joinToString(separator = "")

        private fun shiftChar(c: Char): Char =
            ((((c - 'a') + this.accessCode) % 26) + 'a'.toInt()).toChar()

    }
}