Skip to content

Instantly share code, notes, and snippets.

@karel1980
Created December 4, 2019 20:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save karel1980/2cb3653f4d3df3fbf125b75bd20bbe67 to your computer and use it in GitHub Desktop.
import itertools
I_PATTERN = [5,0,7,2,1,4,3,6]
J_PATTERN = [6,3,0,5,2,7,4,1]
def swapi(items):
return swap_with_pattern(items, I_PATTERN)
def swapj(items):
return swap_with_pattern(items, J_PATTERN)
def swap_with_pattern(items, pattern):
return [ items[pattern[i]] for i in range(len(pattern)) ]
def decode(k_pattern):
code='iANUEZRLPjPLNAEURZiAjTAHLiAENAJEVLRIjiNEjLEjiEKSNEijDNkEEBUNTiPkIZkiITj1HK-EkjiS-iORjO-kPk1JVERjN-AAUEEZNikNVOjZLkiRAkiVjASiED-jOIGikTNSELkjiGE-iDAWIkijDEN-kVH1AjASATNME-kRikTEiOjPkEAkiIiAT-EjRDijUDkPkNURkjiA-iLEZFijI-kNIMLV1jAkDE-NUSikiIkTYjMMX?'
result = []
remainder = [ l for l in code ]
remainder.extend([' ' for i in range(100)])
done = False
while not done:
ipos = remainder.index('i') if 'i' in remainder else 999999
jpos = remainder.index('j') if 'j' in remainder else 999999
kpos = remainder.index('k') if 'k' in remainder else 999999
pos = min(ipos, jpos, kpos)
if pos == ipos:
patternlen = len(I_PATTERN)
if pos == jpos:
patternlen = len(J_PATTERN)
if pos == kpos:
patternlen = len(k_pattern)
if pos < 999999:
if pos == 0:
before, instruction, piece, remainder = [], remainder[pos], remainder[pos+1:pos+patternlen+1], remainder[pos+patternlen+1:]
else:
before, instruction, piece, remainder = remainder[:pos], remainder[pos], remainder[pos+1:pos+patternlen+1], remainder[pos+patternlen+1:]
result.extend(before)
if instruction == 'i':
piece = swapi(piece)
remainder = piece + remainder
if instruction == 'j':
piece = swapj(piece)
remainder = piece + remainder
if instruction == 'k':
piece = swap_with_pattern(piece, k_pattern)
remainder = piece + remainder
else:
result.extend(remainder)
done = True
return result
def solve():
for n in range(1,9):
all_k_patterns = list(itertools.permutations(list(range(n))))
for permutation in all_k_patterns:
print(''.join(decode(permutation)))
if __name__=="__main__":
solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment