Sha256: 789b2a111808047057c885581725f3365719cad03e03cf1551e5887bf10f09a1

Contents?: true

Size: 1.66 KB

Versions: 245

Compression:

Stored size: 1.66 KB

Contents

module CryptoSquare
  ( normalizedPlaintext
  , plaintextSegments
  , encoded
  , ciphertext
  ) where

import Prelude
import Data.Array as A
import Data.Array (filter, fromFoldable, replicate, toUnfoldable, (:))
import Data.Char.Unicode (isAlphaNum)
import Data.Foldable (maximum)
import Data.Int (ceil, toNumber)
import Data.List (transpose)
import Data.Maybe (fromMaybe)
import Data.String (drop, fromCharArray, joinWith, length, take, toCharArray, toLower)
import Math (sqrt)

normalizedPlaintext :: String -> String
normalizedPlaintext
  = toCharArray
    >>> filter isAlphaNum
    >>> fromCharArray
    >>> toLower

plaintextSegments :: String -> Array String
plaintextSegments str = toSquare norm
  where norm = normalizedPlaintext str
        cols = sqrt (length norm # toNumber) # ceil
        toSquare "" = []
        toSquare s  = take cols s : toSquare (drop cols s)

transposeArray :: forall a. Array (Array a) -> Array (Array a)
transposeArray
  = toUnfoldable
    >>> map toUnfoldable
    >>> transpose
    >>> map fromFoldable
    >>> fromFoldable

encoded :: String -> String
encoded = plaintextSegments
          >>> map toCharArray
          >>> transposeArray
          >>> map fromCharArray
          >>> joinWith ""

spaces :: Int -> Array Char
spaces n = replicate n ' '

equalPad :: Array (Array Char) -> Array (Array Char)
equalPad arr = map pad arr
  where width = fromMaybe 0 (maximum $ map A.length arr)
        pad el = el <> spaces (width - A.length el)

ciphertext :: String -> String
ciphertext = plaintextSegments
             >>> map toCharArray
             >>> transposeArray
             >>> equalPad
             >>> map fromCharArray
             >>> joinWith " "

Version data entries

245 entries across 245 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.179 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.178 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.177 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.176 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.175 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.174 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.173 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.172 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.171 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.170 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.169 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.167 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.166 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.165 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.164 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.163 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.162 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.161 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs
trackler-2.2.1.160 tracks/purescript/exercises/crypto-square/examples/src/CryptoSquare.purs