Decriptiamolo!
Nota
Questa è la sfida della decrittazione. La sfida della crittografia può essere trovata qui .
Sfida
La sfida è decrittografare una data stringa, utilizzando le regole come specificato di seguito. La stringa conterrà solo lettere minuscole , cifre e / o spazi vuoti . Se vuoi sapere come è stata crittografata la stringa di input, fai riferimento a questa sfida!
Equivalente di un personaggio
Ora, per prima cosa dovresti sapere come trovare l '"equivalente" di ogni personaggio.
Se il carattere è una consonante, questo è il modo per trovare l'equivalente:
1) List all the consonants in alphabetical order
b c d f g h j k l m n p q r s t v w x y z
2) Get the position of the consonant you are finding the equivalent of.
3) The equivalent is the consonant at that position when starting from the end.
es: "h" e "t" sono equivalenti perché "h", "t" sono rispettivamente nella sesta posizione dall'inizio e dalla fine.
La stessa procedura è seguita per trovare l'equivalente di vocali / cifre. Elenchi tutte le vocali o le cifre (a partire da 0) in ordine e trovi l'equivalente.
Di seguito è riportato l'elenco degli equivalenti di tutti i personaggi:
b <-> z
c <-> y
d <-> x
f <-> w
g <-> v
h <-> t
j <-> s
k <-> r
l <-> q
m <-> p
n <-> n
a <-> u
e <-> o
i <-> i
0 <-> 9
1 <-> 8
2 <-> 7
3 <-> 6
4 <-> 5
Regole di decrittografia
Inizi con due stringhe vuote, chiamiamole
s1
es2
. Ci sposteremo da sinistra a destra della stringa di input. All'inizio, dovremmo considerare la prima stringa vuota, ovveros1
. Cambieremo le due stringhe ogni volta che incontreremo una vocale.Prendiamo l'equivalente del carattere corrente della stringa di input, o uno spazio vuoto se è uno spazio vuoto. Chiamiamo questo personaggio
c
.c
è ora aggiunto a destra dis1
(se stiamo considerandos1
attualmente) oa sinistra dis2
(se stiamo considerandos2
attualmente).Se quel carattere era una vocale, passa all'altra stringa ora (
s1 <-> s2
).Ripeti il passaggio 2 (e 3 per le vocali) per ogni carattere nella stringa di input.
Concatena
s1
es2
e il risultato è la stringa decrittografata.
Ora lascia che decifra una stringa per te.
String = "htioj ixej uy "
Initially, s1 = s2 = ""
Current: s1
Moving left to right
"h" -> "t" (s1 = "t")
"t" -> "h" (s1 = "th")
"i" -> "i" (s1 = "thi")
Vowel encountered. Switching to s2 now.
"o" -> "e" (s2 = "e")
Vowel encountered. Switching to s1 now.
"j" -> "s" (s1 = "this")
" " -> " " (s1 = "this ")
"i" -> "i" (s1 = "this i")
Vowel encountered. Switching to s2 now.
"x" -> "d" (s2 = "de") [Note that when dealing with s2, we append to the left]
"e" -> "o" (s2 = "ode")
Vowel encountered. Switching to s1 now.
"j" -> "s" (s1 = "this is"
" " -> " " (s1 = "this is ")
"u" -> "a" (s1 = "this is a")
Vowel encountered. Switching to s2 now.
"y" -> "c" (s2 = "code")
" " -> " " (s2 = " code")
Now, append s1 and s2 to get:
"this is a code"
Output -> "this is a code"
Esempi
"wqcjmc" -> "flyspy"
"toek" -> "hero"
"toyike" -> "heroic"
"uo" -> "ae"
"uoz" -> "abe"
"htoo jmuy" -> "the space"
"uh68v8x " -> "a d1g13t"
"fo78i d" -> "we xi12"
"htioj ixej uy " -> "this is a code"
"jea ceeac hnx8x vni kokhj jiuth xoqqc xohmcky" -> "so you really decrypted this string d1dnt you"
Puoi anche scegliere di utilizzare lettere maiuscole anziché minuscole.
Punteggio
Questo è il golf in codice , quindi il codice più breve vince!
Risposte
Gelatina , 29 byte
ØḄ,ØẹØDṭ,U$F€yµe€ØẹŻœṗµ;ṚU$m2
Un programma completo che stampa il testo decodificato.
Provalo online!
Come?
ØḄ,ØẹØDṭ,U$F€yµe€ØẹŻœṗµ;ṚU$m2 - Main Link: list, S
ØḄ - consonant characters
Øẹ - vowel characters
, - pair
ØD - digit characters
ṭ - tack -> ["bcd...","aeiou","012..."]
$ - last two links as a monad: U - upend -> ["...dcb","uoiea","...210"] , - pair -> [["bcd...","aeiou","012..."],["...dcb","uoiea","...210"]] F€ - flatten each -> ["bcd...aeiou012...","...dcbuoiea...210"] y - translate S using that map µ - new monadic chain (i.e. f(A=that)) Øẹ - vowel characters e€ - (for c in A) exists in (vowels)? Ż - prepend a zero œṗ - partition (A) before truthy elements (of that) µ - new monadic chain (i.e. f(that)) ...e.g.: f(["thi","e","s i","do","s a","c "]) $ - last two links as a monad:
Ṛ - reverse ["c ","s a","do","s i","e","thi"]
U - upend [" c","a s","od","i s","e","iht"]
; - concatenate ["thi","e","s i","do","s a","c "," c","a s","od","i s","e","iht"]
m2 - mod-2 slice ["thi", "s i", "s a", ," c", "od", "e" ]
- implicit, smashing print -> this is a code
JavaScript (ES6), 112 110 109 byte
Si aspetta un array di caratteri.
s=>s.map(c=>(C="bzcydxfwgvhtjskrlqmpnn aueoii",c=C[j=C.search(c)^1]||9-c,+s?b=c+b:a+=c,s^=j>23),a=b='')&&a+b
Provalo online!
Come?
Cerchiamo l'indice di ogni carattere c
nella seguente stringa di ricerca:
0 1 2
012345678901234567890123456789
"bzcydxfwgvhtjskrlqmpnn aueoii"
Se il carattere non viene trovato, deve essere una cifra che viene sostituita con 9-c
. Altrimenti, otteniamo la posizione del carattere della controparte invertendo il bit meno significativo dell'indice.
Ad esempio: j
→ 12
→ 13
→s
Il nuovo carattere viene aggiunto a
o anteposto a b
.
Passiamo da a
e b
ogni volta che l'indice è maggiore di 23, ovvero c
è una vocale.
Python, 226, 160146 byte
Provalo online
Sono abbastanza nuovo in Python e volevo provare questa sfida. So che molto di questo può essere migliorato usando lambda o codice semplificato. Sto mostrando il mio codice in modo da poter ricevere feedback.
Grazie a @Arnauld per avermi aiutato a imparare nuovi modi di programmare. Ora a 160 byte
Più input e perfezionamenti. Adesso a 146.
def k(y):
x='aueoiibzcydxfwgvhtjskrlqmpnn0918273645 '
s=1
b=['']*2
for i in y:n=x.find(i);b[s]+=x[n+1-2*(n%2)];s^=n<6
print(b[1]+b[0][::-1])
Vecchio codice:
def k(y):
x='aueoiibzcydxfwgvhtjskrlqmpnn0918273645 '
s=1
b=c=''
def a(t):
m=n+1 if n%2==0 else n-1;t+=x[m];return t
for i in y:
n=x.find(i)
if s:
b=a(b)
else:
c=a(c)
if n<=5:s=not s
b+=c[::-1]
print(b)
k('wqcjmc')
k('toek')
k('toyike')
k('uo')
k('uoz')
k('htoo jmuy')
k('uh68v8x ')
k('fo78i d')
k('htioj ixej uy ')
k('jea ceeac hnx8x vni kokhj jiuth xoqqc xohmcky')
Ha testato tutti gli articoli campione e ha ottenuto la risposta corretta.
"wqcjmc" -> "flyspy"
"toek" -> "hero"
"toyike" -> "heroic"
"uo" -> "ae"
"uoz" -> "abe"
"htoo jmuy" -> "the space"
"uh68v8x " -> "a d1g13t"
"fo78i d" -> "we xi12"
"htioj ixej uy " -> "this is a code"
"jea ceeac hnx8x vni kokhj jiuth xoqqc xohmcky" -> "so you really decrypted this string d1dnt you"
Retina 0.8.2 , 89 83 80 71 byte
[aeiou]|$ $&¶
+`(¶.+)¶(.*)$ $2$1 O$^r`.\G
T`¶b-df-hj-maed\oup-tv-z_`Ro
Provalo online! Il collegamento include casi di test. Questo si è rivelato più facile della codifica, come dimostrato dal fatto che sono stato in grado di codificarlo in Retina 0.8.2 piuttosto che richiedere Retina 1. Spiegazione:
[aeiou]|$ $&¶
Suddividi l'input sulle sottostringhe che terminano con le vocali. Viene forzata una divisione extra alla fine della stringa in modo che ci siano almeno due righe.
+`(¶.+)¶(.*)$ $2$1
Unisci le linee alternate insieme, in modo che la prima linea sia la concatenazione di tutte le linee dispari e la seconda linea sia la concatenazione di tutte le linee pari.
r`.\G
Corrispondenza solo dei caratteri sull'ultima (seconda) riga ...
O$^`
.. invertire le partite, invertendo solo la seconda riga.
T`¶b-df-hj-maed\oup-tv-z_`Ro
Unisci le due linee insieme e decodifica le lettere. Ciò Ro
fa sì che la stringa si traslitteri al contrario. La consonante centrale n
e la vocale si i
mappa su se stesse, quindi non è necessario che siano elencate. La ¶
mappa allo speciale _
cancellandolo. Le prime e le ultime 10 consonanti e le prime e ultime due vocali circondano quindi le cifre. ( o
Normalmente è speciale quindi deve essere citato qui.)
R , 190 byte
function(i,`/`=strsplit){a=el(" 01234aeibcdfghjklmnpqrstvwxyziou98765 "/"")
names(a)=rev(a)
j=k=""
for(l in a[el(i/"")]){if(T)j=c(j,l)else k=c(l,k)
if(grepl(l,"aeiou"))T=!T}
cat(j,k,sep="")}
Provalo online!
Nota per te stesso: impara una nuova lingua, non R , per le sfide testuali del golf ...
Commentato:
decrypt=function(i,
`/`=strsplit){ # use infix '/' as alias to strsplit function
a=el(" 01234aeibcdfghjklmnpqrstvwxyziou98765 "/"")
# a=vector of characters with equivalents at reverse index
names(a)=rev(a) # name characters by equivalent
j=k="" # initialize j,k as empty strings
for(l in a[el(i/"")]){ # for each input letter (split using /), find its equivalent l
if(T)j=c(j,l)else k=c(l,k) # if T=1 (initial value) append l to j, otherwise put it at start of k
if(grepl(l,"aeiou"))T=!T} # if it was a vowel, T=not T
cat(j,k,sep="")} # finally, output j,k
C (gcc) , 144 143 140 byte
-1-3 byte grazie a Ceilingcat
k,v,p,a;f(char*s,char*o){for(k=p=0,v=strlen(s);a=*s++;p^=1065233>>a-97&1)o[p?--v:k++]=a<33?a:a<58?105-a:"uzyxowvtisrqpnemlkjhagfdcb"[a-97];}
Provalo online!
05AB1E , 27 26 byte
žMIå0šÅ¡ι€S`R«žN‡žM‡žh‡
I / O come elenco di caratteri.
Provalo online o verifica tutti i casi di test (con l'I / O unito a pretty-print).
Spiegazione:
žM # Push the vowels "aeiou"
Iå # Check for each character in the input-list if it's in the vowel string
# i.e. ["h","t","i","o","j"," ","i","x","e","j"," ","u","y"," "]
# → [0,0,1,1,0,0,0,1,0]
0š # Prepend a leading 0
# → [0,0,0,1,1,0,0,0,1,0]
Å¡ # Split the (implicit) input-string on the truthy values
# → [["h","t","i"],["o"],["j"," ","i"],["x","e"],["j"," ","u"],["y"," "]]
ι # Uninterleave the list of lists
# → [[["h","t","i"],["j"," ","i"],["j"," ","u"]],[["o"],["x","e"],["y"," "]]]
€S # Convert each inner list of lists to a flattened list of characters
# → [["h","t","i","j"," ","i","j"," ","u"],["o","x","e","y"," "]]
` # Push both lists of characters separated to the stack
# → ["h","t","i","j"," ","i","j"," ","u"] and ["o","x","e","y"," "]
R # Reverse the second string
# → ["h","t","i","j"," ","i","j"," ","u"] and [" ","y","e","x","o"]
« # Merge the two lists together
# → ["h","t","i","j"," ","i","j"," ","u"," ","y","e","x","o"]
žN # Push the consonants "bcdfghjklmnpqrstvwxyz"
 # Bifurcate it; short for Duplicate & Reverse copy
‡ # Transliterate the "bcdfghjklmnpqrstvwxyz" to "zyxwvtsrqpnmlkjhgfdcb"
# → ["t","h","i","s"," ","i","s"," ","u"," ","c","e","d","o"]
žM‡ # Do the same for the vowels "aeiou"
# → ["t","h","i","s"," ","i","s"," ","a"," ","c","o","d","e"]
žh‡ # And the digits "0123456789"
# → ["t","h","i","s"," ","i","s"," ","a"," ","c","o","d","e"]
# (after which the result is output implicitly)
Perl 5 -F
, 110 byte
map{push@{$o[$i]},y/0-9a-z/9876543210uzyxowvtisrqpnemlkjhagfdcb/r;$i^=/[aeiou]/}@F;say@{$o[0]},reverse@{$o[1]}
Provalo online!