Created
March 18, 2017 22:15
-
-
Save inlinestyle/5ed5af58d6e49904963c27931fcd9d70 to your computer and use it in GitHub Desktop.
Advent Of Code Day 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Hacking extends App { | |
val input = "ojvtpuvg" | |
val messageDigest = java.security.MessageDigest.getInstance("md5") | |
val emptyPassword: Seq[Option[Char]] = Seq(None, None, None, None, None, None, None, None) | |
def fiveZeroesIterator(id: String): Iterator[String] = { | |
Iterator.from(0) | |
.map {id + _} | |
.map { string => | |
val bytes = messageDigest.digest(string.getBytes) | |
new java.math.BigInteger(1, bytes).toString(16) | |
} | |
.filter {_.length <= 27} | |
.map {"%27s".format(_).replace(' ', '0')} | |
} | |
def solvePartB(id: String): String = { | |
fiveZeroesIterator(id) | |
.scanLeft(emptyPassword) { (password, hash) => | |
val index = hash(0).asDigit | |
if (index < 8 && password(index).isEmpty) | |
password.updated(index, Some(hash(1))) | |
else password | |
} | |
.dropWhile {_.contains(None)} | |
.take(1) | |
.flatten | |
.flatten | |
.mkString | |
} | |
def solvePartA(id: String): String = { | |
fiveZeroesIterator(id).take(8).foldLeft("") {_ + _(0)} | |
} | |
try { | |
println("Part A:") | |
println(solvePartA(input)) | |
println("Part B:") | |
println(solvePartB(input)) | |
} catch { | |
case exception: Exception => println(exception) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment