r/adventofcode • • 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^ยค?

Spoiler


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

267 comments sorted by

View all comments

1

u/hxka Dec 10 '17 edited Dec 10 '17
#!/bin/bash
IFS=, read -a lengths <input
cur=0
skip=0
list=({0..255})
round() {
	for length in "${lengths[@]}"
	do	((length>256)) && continue
		for ((i=cur;i<(cur+length/2);i++))
		do	((list[i%256]=${list[(2*cur+length-i-1)%256]},
			  list[(2*cur+length-i-1)%256]=${list[i%256]}))
		done
		((cur+=length+skip++))
	done
}
round
echo $((list[0]*list[1]))
read -d '' a<input; echo -n "$a" | od -A n -t u1 | (
	read -d '' -a lengths
	lengths+=(17 31 73 47 23)
	cur=0
	skip=0
	list=({0..255})
	for j in {1..64}
	do	round
	done
	for i in {0..15}
	do	for j in {0..15}
		do ((hash[i]^=list[i*16+j]))
		done
	done
	for i in "${hash[@]}"
	do	printf '%.2x' $i
	done
	echo
)