-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day14.scala
30 lines (22 loc) · 1.06 KB
/
Day14.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package AdventOfCode2016
object Day14:
val md5 = java.security.MessageDigest.getInstance("MD5")
val three = "(.)\\1{2}".r.unanchored
val five = "(.)\\1{4}".r.unanchored
def single(string: String): String = md5.digest(string.getBytes).map("%02x".format(_)).mkString
def stretched(string: String): String = Iterator.iterate(string)(single).drop(2017).next()
def generatePad(hash: Int => String): Int =
def check(window: Seq[(String, Int)]): Boolean = window.head match
case (three(t), _) => window.tail.exists {
case (five(f), _) if f == t => true
case _ => false
}
case _ => false
Iterator.from(0).map(hash).zipWithIndex.sliding(1001).filter(check).drop(63).next().head._2
end generatePad
def part1(input: String): Int = generatePad(index => single(s"$input$index"))
def part2(input: String): Int = generatePad(index => stretched(s"$input$index"))
def main(args: Array[String]): Unit =
val data = io.Source.fromResource("AdventOfCode2016/Day14.txt").mkString.trim
println(part1(data))
println(part2(data))