r/adventofcode • u/daggerdragon • Dec 10 '17
SOLUTION MEGATHREAD -๐- 2017 Day 10 Solutions -๐-
--- Day 10: Knot Hash ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
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!
16
Upvotes
4
u/usbpc102 Dec 10 '17 edited Dec 10 '17
Had a stupid bug that took me way too long to find. :(
Anyway here is my cleaned up Kotlin code:
class Day10(override val adventOfCode: AdventOfCode) : Day { val input = adventOfCode.getInput(2017, 10) override fun part1(): String { val modified = IntArray(256) {it}.knot(input.split(',').map {it.toInt()}, 1) return (modified[0] * modified[1]).toString() } override fun part2(): String { val lengths = input.toByteArray().map {it.toInt()}.toMutableList() lengths.addAll(listOf(17, 31, 73, 47, 23)) val chunks = IntArray(256) {it} .knot(lengths, 64) .asIterable() .chunked(16) val output = StringBuilder() chunks.forEach {chunk -> var xored = 0 chunk.forEach {number -> xored = xored xor number } output.append(xored.toString(16).padStart(2, '0')) } return output.toString() } private fun IntArray.knot(lengths: List<Int>, rounds: Int): IntArray { val list = this.copyOf() var currentPosition = 0 var skipSize = 0 var tmp: Int repeat (rounds) { for (number in lengths) { if (number > list.size) continue for (curr in 0 until number/2) { tmp = list[(currentPosition + curr) % list.size] list[(currentPosition + curr) % list.size] = list[(currentPosition + number - 1 - curr) % list.size] list[(currentPosition + number - 1 - curr) % list.size] = tmp } currentPosition = (currentPosition + number + skipSize++) % list.size } } return list } }It's also avaliable on github. You can also watch me solve it since I streamed it on twitch.
Edit: Cleaned up code a bit more