From 42c8b7db803d8923b262111c5f158af8fff3e2e9 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Fri, 31 Aug 2012 00:27:08 +0530 Subject: [PATCH] Added cabal setup scripts. Modified files to work with cabal. Added main function in SolataireCipher. --- .gitignore | 2 ++ Cryptograms.hs | 2 +- EnglishNumerals.hs | 2 +- GedcomParser.hs | 2 +- KnightsTravails.hs | 2 +- PhoneNumberWords.hs | 2 +- Setup.hs | 2 ++ SolataireCipher.hs | 34 ++++++++++++++++++++++--- TicTacToe.hs | 2 +- rubyquiz.cabal | 62 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 Setup.hs create mode 100644 rubyquiz.cabal diff --git a/.gitignore b/.gitignore index 6b45255..ad405da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.hi *.o input +bin +dist \ No newline at end of file diff --git a/Cryptograms.hs b/Cryptograms.hs index 80fc922..6d9c301 100644 --- a/Cryptograms.hs +++ b/Cryptograms.hs @@ -8,7 +8,7 @@ {-# LANGUAGE BangPatterns #-} -module Cryptograms where +module Main where import qualified Data.Map as M import qualified Data.Set as S diff --git a/EnglishNumerals.hs b/EnglishNumerals.hs index 2e1a6de..de604a8 100644 --- a/EnglishNumerals.hs +++ b/EnglishNumerals.hs @@ -46,7 +46,7 @@ Copyright 2012 Abhinav Sarkar -} -module EnglishNumerals where +module Main where import qualified Data.Sequence as Seq import Data.List (maximumBy, nub) diff --git a/GedcomParser.hs b/GedcomParser.hs index 2944a76..226b7c6 100644 --- a/GedcomParser.hs +++ b/GedcomParser.hs @@ -5,7 +5,7 @@ {-# LANGUAGE NoMonomorphismRestriction, RecordWildCards #-} -module GedcomParser where +module Main where import Text.Parsec hiding (spaces, Line) import System.IO diff --git a/KnightsTravails.hs b/KnightsTravails.hs index 11f7271..97b091d 100644 --- a/KnightsTravails.hs +++ b/KnightsTravails.hs @@ -20,7 +20,7 @@ {-# LANGUAGE MultiParamTypeClasses, RecordWildCards #-} -module KnightsTravails where +module Main where import qualified Data.Set as S import AStar diff --git a/PhoneNumberWords.hs b/PhoneNumberWords.hs index 4bfb619..7a4a413 100644 --- a/PhoneNumberWords.hs +++ b/PhoneNumberWords.hs @@ -15,7 +15,7 @@ {-# LANGUAGE BangPatterns #-} -module PhoneNumberWords where +module Main where import qualified Data.Set as S import qualified Data.Map as M diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..bf68901 --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain \ No newline at end of file diff --git a/SolataireCipher.hs b/SolataireCipher.hs index 102f8f9..83145f0 100644 --- a/SolataireCipher.hs +++ b/SolataireCipher.hs @@ -1,8 +1,11 @@ -module SolataireCipher where +module Main where +import qualified Options.Applicative as Op import Data.Char (toUpper, ord, chr) import Data.List (unfoldr, splitAt, sort, elemIndex) import Data.Maybe (fromJust) +import Control.Applicative ((<*>)) +import Control.Monad ((>=>)) data Card = RankCard Int | JokerA | JokerB deriving (Eq) @@ -43,7 +46,7 @@ keyChar deck = deck'' = moveCard deck' (bJokerIdx deck') 2 -- triple cut around the jokers - [i, j] = sort [(aJokerIdx deck''), (bJokerIdx deck'')] + [i, j] = sort [aJokerIdx deck'', bJokerIdx deck''] (top, rest) = splitAt i deck'' (mid, bottom) = splitAt (j + 1 - i) rest cards' = bottom ++ mid ++ top @@ -51,14 +54,14 @@ keyChar deck = -- count cut using the value of the bottom card c = cardValue (last cards') (top', bottom') = splitAt c cards' - cards'' = (init bottom') ++ top' ++ [last cards'] + cards'' = init bottom' ++ top' ++ [last cards'] -- output value cV = cardValue (cards'' !! (cardValue . head $ cards'')) in if cV == 53 then keyChar cards'' - else (numToChar $ (if cV > 26 then cV - 26 else cV), cards'') + else (numToChar (if cV > 26 then cV - 26 else cV), cards'') moveCard :: [a] -> Int -> Int -> [a] moveCard lst idx move = @@ -119,3 +122,26 @@ decrypt deck encText = ks = keyStream deck (length encText) encTextNums = charsToNums encText ksNums = charsToNums ks + +data Command = Encrypt String | Decrypt String + +optParser = Op.subparser $ + Op.command "encrypt" + (Op.info + (Op.helper <*> + (Op.argument (Op.str >=> Just . Encrypt) (Op.metavar "CLEARTEXT"))) + (Op.progDesc "Encrypts a cleartext")) + Op.& Op.command "decrypt" + (Op.info + (Op.helper <*> + (Op.argument (Op.str >=> Just . Decrypt) (Op.metavar "CRYPTTEXT"))) + (Op.progDesc "Decrypts a crypttext")) + +opts = Op.info (Op.helper <*> optParser) $ + Op.progDesc "Encrypt or decrypts a string using the solataire cipher algorithm" + +main = do + command <- Op.execParser opts + case command of + (Encrypt clearText) -> putStrLn $ encrypt serialDeck clearText + (Decrypt cryptText) -> putStrLn $ decrypt serialDeck cryptText \ No newline at end of file diff --git a/TicTacToe.hs b/TicTacToe.hs index e9ce82f..e65e7db 100644 --- a/TicTacToe.hs +++ b/TicTacToe.hs @@ -10,7 +10,7 @@ {-# LANGUAGE BangPatterns #-} -module TicTacToe where +module Main where import Data.List (sort, nub, maximumBy) import Data.List.Split (chunk) diff --git a/rubyquiz.cabal b/rubyquiz.cabal new file mode 100644 index 0000000..3fbcd88 --- /dev/null +++ b/rubyquiz.cabal @@ -0,0 +1,62 @@ +name: RubyQuiz +version: 1.0 +synopsis: Solutions to RubyQuiz problems in Haskell +homepage: https://github.com/abhin4v/rubyquiz +bug-reports: https://github.com/abhin4v/rubyquiz/issues +license: BSD3 +copyright: Copyright 2012 Abhinav Sarkar +author: Abhinav Sarkar +cabal-version: >= 1.14 +build-type: Simple + +source-repository head + type: git + location: git@github.com:abhin4v/rubyquiz.git + +executable TicTacToe + build-depends : base == 4.*, + containers == 0.4.*, + mtl == 2.1.*, + random == 1.0.*, + split == 0.1.4.* + main-is : TicTacToe.hs + default-language : Haskell2010 + +executable KnightsTravails + build-depends : base == 4.*, + containers == 0.4.*, + pqueue == 1.2.* + main-is : KnightsTravails.hs + default-language : Haskell2010 + other-modules : AStar + +executable Cryptograms + build-depends : base == 4.*, + containers == 0.4.* + main-is : Cryptograms.hs + default-language : Haskell2010 + +executable EnglishNumerals + build-depends : base == 4.*, + containers == 0.4.*, + split == 0.1.4.* + main-is : EnglishNumerals.hs + default-language : Haskell2010 + +executable GedcomParser + build-depends : base == 4.*, + parsec == 3.1.* + main-is : GedcomParser.hs + default-language : Haskell2010 + +executable PhoneNumberWords + build-depends : base == 4.*, + containers == 0.4.* + main-is : PhoneNumberWords.hs + default-language : Haskell2010 + +executable SolataireCipher + build-depends : base == 4.*, + optparse-applicative == 0.1.* + main-is : SolataireCipher.hs + default-language : Haskell2010 \ No newline at end of file