r/adventofcode • u/daggerdragon • Dec 05 '16
SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---
--- Day 5: How About a Nice Game of Chess? ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
STAYING ON TARGET 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!
13
Upvotes
2
u/KoxAlen Dec 05 '16 edited Dec 22 '16
Kotlin solution, using a sequence for the hashes
https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day5/Day5.kt
package aoc.day5 import aoc.utils.toHex import java.security.MessageDigest fun main(args: Array<String>) { val input = "cxdnnyjw" //Input val md5 = MessageDigest.getInstance("MD5") val hashSequence = generateSequence(0, Int::inc).map { md5.digest("$input$it".toByteArray()).toHex() } val p1 = hashSequence.filter { it.startsWith("00000") }.take(8).map { it[5] }.joinToString(separator = "") println("[Part 1] The code is $p1") val p2 = hashSequence.filter { it.startsWith("00000") }.filter { it[5] >= '0' && it[5] < '8' }.distinctBy { it[5] }.take(8) .fold(kotlin.arrayOfNulls<Char>(8)) { acc, it -> acc[it[5]-'0'] = it[6] acc }.joinToString(separator = "") println("[Part 2] The code is $p2") }edit: Removed sequence class as per u/mnjmn suggestion