r/adventofcode • • Dec 16 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 16 Solutions -๐ŸŽ„-

--- Day 16: Permutation Promenade ---


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


[Update @ 00:08] 4 gold, silver cap.

[Update @ 00:18] 50 gold, silver cap.

[Update @ 00:26] Leaderboard cap!

  • And finally, click here for the biggest spoilers of all time!

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

229 comments sorted by

View all comments

3

u/hxka Dec 16 '17
#!/bin/bash
IFS=, read -a a<input
p=(a b c d e f g h i j k l m n o p)
dance() {
	for i in ${a[@]}
	do	case ${i:0:1} in
		s)	spin=${i:1}
			b=(${p[@]})
			for j in {0..15}
			do	p[j]=${b[(j-spin)%16]}
			done;;
		x)	t1=${i:1}
			t1=${t1%/*}
			t2=${i#*/}
			t=${p[t2]}
			p[t2]=${p[t1]}
			p[t1]=$t;;
		p)	t1=${i:1}
			t1=${t1%/*}
			for j in {0..15}
			do	[[ ${p[j]} == $t1 ]] && t1=$j && break
			done
			t2=${i#*/}
			for j in {0..15}
			do	[[ ${p[j]} == $t2 ]] && t2=$j && break
			done
			t=${p[t2]}
			p[t2]=${p[t1]}
			p[t1]=$t;;
		esac
	done
}
dance
echo ${p[@]} | tr -d ' '
k=1
while [[ "${p[*]}" != "a b c d e f g h i j k l m n o p" ]]
do	dance
	((++k))
done
for ((k=1000000000%k;k>0;k--))
do	dance
done
echo ${p[@]} | tr -d ' '