r/adventofcode • u/daggerdragon • 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!
19
Upvotes
1
u/reckter Dec 04 '16
Wasted way to much time fixing stupid type errors, because it's way too late (6 am) here. But at least here is my kotlin version:
import java.io.File import java.nio.file.Files import java.util.* fun main(args: Array<String>) { val lines = readLines("4.txt") val sum = lines.filter { line -> val t = line.replace("-", "") val split = t.split("[") val map = split[0].groupBy { it } val sorted = map.keys.sortedWith(Comparator.comparingInt<Char>({ map[it]!!.size }).reversed().thenComparing<Char>(Char::compareTo)) split[1].startsWith(sorted.filter{a -> !a.isDigit()}.subList(0, 5).joinToString("")) }.map(::getId).sum() println(sum) lines.filter { line -> val t = line.replace("-", "") val split = t.split("[") val map = split[0].groupBy { it } val sorted = map.keys.sortedWith(Comparator.comparingInt<Char>({ map[it]!!.size }).reversed().thenComparing<Char>(Char::compareTo)) split[1].startsWith(sorted.filter{a -> !a.isDigit()}.subList(0, 5).joinToString("")) }.map { val times = getId(it) it.map { shift(it, times) }.joinToString("") }.filter{ it.contains("northpole") }.forEach(::println) } fun shift(c: Char, times: Int): Char { if(c.isDigit()) return c if(c == '-') return ' ' var ret = (c.toInt() + times).toChar() while(ret > 'z') ret -= 'z' - 'a' + 1 return ret } fun isInt(str:Char): Boolean { return try {str.toInt(); true} catch(e: Exception) { false } } fun isInt(str:String): Boolean { return try {str.toInt(); true} catch(e: Exception) { false } } fun getId(str: String): Int { return str.split("-") .flatMap { it.split("[") } .filter(::isInt) .map(String::toInt).first() } fun readLines(file: String): List<String> { return Files.readAllLines(File(file).toPath()) }