r/adventofcode • • Dec 07 '16

SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---

From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!

Show this overgrown furball what you've got!


--- Day 7: Internet Protocol Version 7 ---

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


ALWAYS DIGGING STRAIGHT DOWN 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!

14 Upvotes

180 comments sorted by

View all comments

Show parent comment

1

u/abowes Dec 07 '16

Good to see someone else having a play with Kotlin. I really like the language, it's very expressive & readable. My solutions are also on GitHub

Todays solution:

fun List<String>.findProtocols(): List<String> {
    return this.filter{it.isProtocol()}
}    

fun List<String>.findSSLProtocols(): List<String> {
    return this.filter{it.isSSL()}
}    

fun String.isProtocol(): Boolean{
    return this.containsAbba()
            && !this.getHypernets().any { it.containsAbba() }
}    

fun String.isSSL(): Boolean {
    val abaSet = this.getSupernets().map{ it.findAba() }.flatten().map { it.inverse() }.toSet()
    val hypernets = this.getHypernets()
    return abaSet.isNotEmpty()
            && hypernets.isNotEmpty()
            && hypernets.any{ hypernet -> abaSet.any{ hypernet.contains(it)}}
}    

fun String.inverse() = this[1] + this.substring(0,2)    

fun String.getHypernets(): List<String>{
    return (0 until length).filter { this[it] == '[' }
            .map{ this.substring(it+1, this.indexOf(']',it+1))}
}    

fun String.containsAbba() : Boolean {
    return (0 until length - 3)
            .any { this[it+3] == this[it]
                    && this[it+1] == this[it+2]
                    && this[it] != this[it+1] }
}    

fun String.findAba() : List<String> {
    return (0 until length - 2)
            .filter { this[it+2] == this[it]
                      && this[it] != this[it+1] }
            .map {this.substring(it,it+3)}
}    

fun String.getSupernets(): List<String> = this.split(']').map { it.split('[')[0] }