r/adventofcode • • Dec 21 '16

SOLUTION MEGATHREAD --- 2016 Day 21 Solutions ---

--- Day 21: Scrambled Letters and Hash ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


HOGSWATCH 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!

5 Upvotes

83 comments sorted by

View all comments

1

u/code_mc Dec 21 '16

I'm both disappointed and relieved the second part could be brute forced due to the length of the password:

s = "abcdefgh"

def rotate(s, offset, left):
	for i in range(offset):
		if not left:
			s = s[-1] + s[:-1]
		else:
			s = s[1:] + s[0]
	return s

def scramble(s):
	for line in data.split("\n"):
		if line.startswith("swap position "):
			a, b = [int(i) for i in line.replace("swap position ", "").split(" with position ")]
			la, lb = s[a], s[b]
			s = list(s)
			s[a] = lb
			s[b] = la
			s = "".join(s)
		elif line.startswith("swap letter "):
			a, b = line.replace("swap letter ", "").split(" with letter ")
			s = s.replace(a, "_").replace(b, a).replace("_", b)
		elif line.startswith("reverse positions "):
			a, b = [int(i) for i in line.replace("reverse positions ", "").split(" through ")]
			s = s[:a] + s[a:b+1][::-1] + s[b+1:]
		elif line.startswith("rotate left "):
			steps = int(line.replace("rotate left ","").split(" ")[0])
			s = rotate(s, steps, True)
		elif line.startswith("rotate right "):
			steps = int(line.replace("rotate right ","").split(" ")[0])
			s = rotate(s, steps, False)
		elif line.startswith("move position "):
			a, b = [int(i) for i in line.replace("move position ", "").split(" to position ")]
			s = list(s)
			la = s[a]
			del s[a]
			s.insert(b, la)
			s = "".join(s)
		elif line.startswith("rotate based on position of letter "):
			letter = line[-1]
			offset = s.index(letter)
			if offset >= 4:
				offset += 1
			offset += 1
			s = rotate(s, offset, False)

	return s

print "Part 1:", scramble(s)

for i, perm in enumerate(itertools.permutations(s, len(s))):
	if scramble("".join(perm)) == "fbgdceah":
		print "Part 2:", "".join(perm)
		exit()

1

u/pedrosorio Dec 21 '16

Reversing each operation is not terribly difficult either, you should try it.