generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2016D05.kt
42 lines (33 loc) · 1.29 KB
/
Y2016D05.kt
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
31
32
33
34
35
36
37
38
39
40
41
42
package aockt.y2016
import io.github.jadarma.aockt.core.Solution
import java.security.MessageDigest
import kotlin.experimental.and
object Y2016D05 : Solution {
/** Returns the hexadecimal value of this string's MD5 hash. */
private val String.md5
get() = MessageDigest
.getInstance("MD5")
.digest(this.toByteArray())
/** Brute forces MD5 hashes that start with five zeroes, in ascending order. */
private fun bruteForce(input: String) =
generateSequence(0, Int::inc)
.map { "$input$it".md5 }
.filter { it[0] == 0.toByte() && it[1] == 0.toByte() && it[2].and(240.toByte()) == 0.toByte() }
.map { digest -> digest.joinToString("") { "%02x".format(it) } }
override fun partOne(input: String) =
bruteForce(input)
.take(8)
.map { it[5] }
.joinToString("")
override fun partTwo(input: String): String {
val password = CharArray(8) { '_' }
bruteForce(input)
.map { (it[5] - '0') to it[6] }
.filter { it.first in 0..7 }
.filter { password[it.first] == '_' }
.onEach { password[it.first] = it.second }
.takeWhile { password.any { it == '_' } }
.count()
return String(password)
}
}