You're an American spy during the heat of the Cold War and you've managed to intercept a Russian message. Immediately you recognise it as a code stick cipher and wrap it around a pencil. Looking at the paper on the pencil, you don't see anything. Then you try a bigger pencil, and you see it.
You have to write a program that deciphers a code stick. As this is code golf, the shortest program wins.
You may use any English dictionaries for looking up words. Some examples of dictionaries include:
- Querying a text file of words
- Using an inbuilt OS dictionary
- Querying an online dictionary (screen scraping etc.)
- Using an external lib
All words MUST be valid English words
Each of your pencils has four equally sized faces and are infinitely long. You have five differently sized pencils:
Pencil 1: Shows 1 character on each face Pencil 2: Shows 2 characters on each face Pencil 3: Shows 4 characters on each face Pencil 4: Shows 6 characters on each face Pencil 5: Shows 8 characters on each face
All ciphers can be deciphered by placing the start of the paper at the top of the pencil and wrapping it down.
The following is an example of a cipher:
Hepoge slljghadso bvvbgh
This can be deciphered by wrapping it around Pencil 2:
Side 1 | Side 2 | Side 3 | Side 4 -------+--------+--------+------- He | po | ge | s ll | jg | ha | ds o | bv | vb | gh
So now you have four combinations:
Hello , pojgbv, gehavb, sdsgh
You can now perform a dictionary lookup and see that
Hello is the only valid word. Therefore,
Hello is the cracked cipher.
Note: Deciphered codes may have spaces in. Example:
Ciphers can be inputted in any way apart from being hardcoded. The following are a few test cases. You will not be given the pencil size.
boasonespetrabbittonespongomezesray long lsbvmii derfghthstthasinlomnrtk fg dndthugssawerpoloasefnxafqeormnaqwge padhndi mbavwgam lipois lisptloopoolsisp lortfippinglthe asse rtedgoodcodeis the best of enlymoolfangthe leamingsoulsgodsingering antidisebd sldkfdw sld sja dmddystablishjzkapxmjhavzlx dld slzbfmentariabahdjdlljagzlxbxlffnd ldnism is hdbsh spjalcpdhstd flcnda very cbd skd alsbfo kfvwfd xlconvolutehs ska ckw dlc fjzlxbckxd word hs sla k dk skdnlcjcu dk
Any more test cases would be appreciated
Answers to the test cases
2: i think therefore i am
3: is lisp the code of the gods
4: antidisestablishmentarianism is a very convoluted word
Script to generate code stick ciphers:
import string, random, math word = input('Word:\t') n = int(input('Pencil:\t')) pencil = int(-((4*(17*n-77))/((n**4)-(19*n**3)+(136*n**2)-(444*n)+566))) alpha = string.ascii_lowercase+' ' cipher = '' side = random.randint(1, 4) sides =['','','',''] for j in range(len(sides)): if j+1 == side: sides[j] = word if len(word)%pencil != 0: sides[j] += ' '*(len(word)%pencil) else: for k in range(math.ceil(len(word)+(len(word)%pencil))): sides[j]+=random.choice(alpha) for l in range(0, math.ceil(len(word)/pencil)*pencil, pencil): for m in sides: cipher += m[l:l+pencil] print('Cipher:\t'+cipher) print('Side:\t'+str(side))