# Write a function to decrypt Feynman Challenge Cipher #1

The Feynman Challenge Cipher #1 is as follows:

MEOTAIHSIBRTEWDGLGKNLANEAINOEEPEYST
NPEUOOEHRONLTIROSDHEOTNPHGAAETOHSZO
TTENTKEPADLYPHEODOWCFORRRNLCUEEEEOP
GMRLHNNDFTOENEALKEHHEATTHNMESCNSHIR
AETDAHLHEMTETRFSWEDOEOENEGFHETAEDGH
RLNNGOAAEOCMTURRSLTDIDOREHNHEHNAYVT
IERHEENECTRNVIOUOEHOTRNWSAYIFSNSHOE
MRTRREUAUUHOHOOHCDCHTEEISEVRLSKLIHI
IAPCHRHSIHPSNWTOIISISHHNWEMTIEYAFEL
NRENLEERYIPHBEROTEVPHNTYATIERTIHEEA
WTWVHTASETHHSDNGEIEAYNHHHNNHTW


The solution is described as:

It is a simple transposition cipher: split the text into 5-column pieces, then read from lower right upward. What results are the opening lines of Chaucer's Canterbury Tales in Middle English.

Which is:

WHANTHATAPRILLEWITHHISSHOURESSOOTET
HEDROGHTEOFMARCHHATHPERCEDTOTHEROOT
EANDBATHEDEVERYVEYNEINSWICHLICOUROF
WHICHVERTUENGENDREDISTHEFLOURWHANZE
PHIRUSEEKWITHHISSWEETEBREFTHINSPIRE
DHATHINEVERYHOLTANDHEETHTHETENDRECR
OPPESANDTHEYONGESONNEHATHINTHERAMHI
SHALVECOURSYRONNEANDSMALEFOWELESMAK
ENMELODYETHATSLEPENALTHENYGHTWITHOP
ENYESOPRIKETHHEMNATUREINHIRCORAGEST
HANNELONGENFOLKTOGOONONPILGRIM


## The Challenge:

Write a function to decrypt the cipher.

The output does not need line breaks or spaces.

Input is has no line breaks or spaces.

Shortest solution wins.

(Bonus points if you can solve the other 2 ciphers :P)

### My attempt (PHP 77 70):

$b=strrev($a);for($i=0;$i<5;$i++)for($j=0;$j<381;$j++)$r.=$b[$i+$j*5];


http://codepad.org/PFj9tGb1

• What restrictions are there on input assumptions? E.g. both solutions so far assume input as a single line with no whitespace; yours assumes that it's in a variable and gnibbler's assumes that it's on stdin. – Peter Taylor Nov 4 '11 at 9:34

## 12 Answers

### Golfscript, 10 chars

n--1%5/zip


Takes input from stdin. It may be separated by newlines, as in the question presentation, or not, as many answers assume. (Stripping the newlines is necessary anyway given that it's likely to arrive with one on the end, which messes up the blocks of 5). If we assume that the input arrives with precisely one newline, being the last character, we can shave one character to

)!(%5/zip


### Mathematica, 51

Mathematica is quite verbose here, but it is also easy to guess what it's doing.

Thread[Characters@#~Partition~5]~Reverse~{1,2}<>""&


## J, 13 characters

|.@(/:5|i.@#)


# Haskell, 52

f s=[s!!(x-i)|x<-[length s],j<-[1..5],i<-[j,j+5..x]]

• x<-[length s]... disgusting. I love it! – Thomas Eding Nov 4 '11 at 20:24

# Japt -P, 4 3 bytes

Ôó5


Try it online!

I feel like that was too easy? On a positive note, this can't possibly be golfed too much assuming I am getting the correct result :)

EDIT -1 byte thanks to @Shaggy!

Ôó5    # full program
Ô      # reverse input string
ó5    # create a 5-element array by repeatedly
# picking every 5th character.
# -P flag concatenates array on output!

• There's a shortcut for w  ;) – Shaggy Mar 2 at 10:36

## Python - 56 chars

s=raw_input()
print"".join(s[-1-x::-5]for x in range(5))

• 2 characters shorter with python 3 (-4 for input instead of raw_input; +2 for adding parenthesis to print. – Steven Rumbalski Nov 7 '11 at 17:00

## Scala, 62 characters

print((for(x<-0 to 4;y<-379-x to 0 by -5)yield s(y)).mkString)


Making the assumption that the string is already in a variable s as hammar and stevether do.
Taking the input from stdin is a bit uglier (92 chars):

var s=io.Source.stdin.mkString
print((for(x<-0 to 4;y<-379-x to 0 by -5)yield s(y)).mkString)


and it only works if there are no newlines in the input.

## Perl, 58 characters

perl -lne'$c[$c++%5].=$_ for split//}END{$"=$,,print$_=reverse"@c"'


### Scala 63 chars:

(1 to 5).map(n=>s.reverse.drop(n)).map(_.sliding(1,5).mkString)


Assuming the input is in s.

# K, 34

{a::(5*!76)_x;1'{a[;x]@|!#a}@'|!5}


# ECMAScript 6 - 48 Characters

for(c='',i=5;i--;)c+=(x=>s[x]?f(x+5)+s[x]:'')(i)


Assumes that the variable s contains the input string (without any white space characters) and creates the variable c containing the output.

# Python 3.6, 48 bytes

Recursion + f-Strings

f=lambda a:a and f'{a[::-5]+f(a[:-1])}'[:len(a)]


Try it online!