mirror of
https://github.com/correl/elm.git
synced 2024-12-01 11:09:57 +00:00
64 lines
1.3 KiB
Elm
64 lines
1.3 KiB
Elm
|
module AtbashCipher exposing (..)
|
||
|
|
||
|
import String
|
||
|
import Dict
|
||
|
import Char
|
||
|
import Regex exposing (HowMany(All), regex)
|
||
|
|
||
|
|
||
|
encode : String -> String
|
||
|
encode plain =
|
||
|
let
|
||
|
translate =
|
||
|
toTranslator alphabet reversedAlphabet
|
||
|
in
|
||
|
plain
|
||
|
|> String.toLower
|
||
|
|> String.filter (\c -> Char.isLower c || Char.isDigit c)
|
||
|
|> String.map translate
|
||
|
|> insertEvery 5 " "
|
||
|
|
||
|
|
||
|
decode : String -> String
|
||
|
decode cipher =
|
||
|
let
|
||
|
translate =
|
||
|
toTranslator reversedAlphabet alphabet
|
||
|
in
|
||
|
cipher
|
||
|
|> String.filter ((/=) ' ')
|
||
|
|> String.map translate
|
||
|
|
||
|
|
||
|
alphabet : String
|
||
|
alphabet =
|
||
|
"abcdefghijklmnopqrstuvwxyz"
|
||
|
|
||
|
|
||
|
reversedAlphabet : String
|
||
|
reversedAlphabet =
|
||
|
-- AKA tebahpla
|
||
|
String.reverse alphabet
|
||
|
|
||
|
|
||
|
toTranslator : String -> String -> Char -> Char
|
||
|
toTranslator from to =
|
||
|
let
|
||
|
table =
|
||
|
List.map2 (,) (String.toList from) (String.toList to)
|
||
|
|> Dict.fromList
|
||
|
|
||
|
translate key =
|
||
|
Dict.get key table
|
||
|
|> Maybe.withDefault key
|
||
|
in
|
||
|
translate
|
||
|
|
||
|
|
||
|
insertEvery : Int -> String -> String -> String
|
||
|
insertEvery size insertion string =
|
||
|
Regex.replace All
|
||
|
(regex (".{" ++ toString size ++ "}(?!$)"))
|
||
|
(\{ match } -> match ++ insertion)
|
||
|
string
|