Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
most short solution in Creative category for Grille Cipher Attack by tamagoyaki
from typing import List
def find_grille(plaintext: str, cryptogram: str) -> List[str]:
xs = [[]]
for i,p in enumerate(plaintext[:16]):
temp = []
for j,c in enumerate(cryptogram):
if p == c:
if (tempf := plaintext.find(cryptogram[abs(7-j//8)+j%8*8],i+1)) <= 0 :
continue
if (tempf := plaintext.find(cryptogram[63-j],tempf+1)) <= 0 :
continue
if (plaintext.find(cryptogram[abs(7-(63-j)//8)+(63-j)%8*8],tempf+1)) <= 0 :
continue
for xn in xs:
if not xn or xn[-1] < j:
tempx = xn = xn + [j]
arr = []
for _ in range(4):
for x in xn:
arr.append(cryptogram[x])
xn = sorted([abs(7-i//8)+i%8*8 for i in xn])
tempf = -1
for a in arr:
if (tempf := plaintext.find(a,tempf+1)) < 0 :
break
else:
temp.append(tempx)
xs = temp
return ["".join(["X" if i+j*8 in xs[0] else "." for i in range(8)]) for j in range(8)]
if __name__ == "__main__":
print("Example:")
print(
find_grille(
"quickbrownfoxjumpsoverthelazydogandjackdawslovesmysphinxofquartz",
"quicpsovkbroerthwnfoelazxjumydogmyspandjhinxackdofquawslartzoves",
)
)
# These "asserts" are used for self-checking and not for an auto-testing
assert find_grille(
"quickbrownfoxjumpsoverthelazydogandjackdawslovesmysphinxofquartz",
"quicpsovkbroerthwnfoelazxjumydogmyspandjhinxackdofquawslartzoves",
) == [
"XXXX....",
"XXXX....",
"XXXX....",
"XXXX....",
"........",
"........",
"........",
"........",
]
assert find_grille(
"weareallfromxanthcubesaidquicklyjustvisitingphazewewontbeforlong",
"wejhewucuaeswtbrveeoisantsalilbifdteifrqunooigrmplxcakhonnlagtyz",
) == [
"X...X...",
".X.....X",
"..X...X.",
"...X.X..",
"X.....X.",
"...X...X",
"..X.X...",
".X...X..",
]
assert find_grille(
"theideaofcognitivebiasinpsychologyworksinananalogouswayacognitiv",
"tgovgeubyhsiawseiinorkdepaswoasifcyncyaanaognconaginihlttoiivloo",
) == [
"X.......",
".X.....X",
"X.....XX",
".X..X...",
"XX......",
"..XXX...",
"..X....X",
"...X....",
]
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 28, 2024
Comments: