-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecretSanta.hs
80 lines (68 loc) · 2.07 KB
/
SecretSanta.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module Main where
import Control.Applicative (liftA3)
import Data.Maybe
import File.Format.Participant.Internal
import Data.List.NonEmpty
import Prelude
import Safe
{-
import SecretSanta.Arrangement
import SecretSanta.Mailing
import SecretSanta.Participant
import SecretSanta.Solver
import SecretSanta.Types
-}
import System.Environment
import Text.Megaparsec
main :: IO ()
main = do
stream <- getContents
case (parse fileDefinition "STDIN" stream :: Either (ParseError Char Dec) (NonEmpty Participant)) of
Left error -> putStrLn $ parseErrorPretty error
Right result -> print result
{-
main :: IO ()
main = getArgs
>>= fmap parseParameters . mapM readFile
>>= maybe errorParametersMessage performLottery
performLottery :: Parameters -> IO ()
performLottery params = do
lot <- solveSecretSanta params
case lot of
Nothing -> putStrLn $ "Constraints unsatisfiable!" ++ show params
Just x -> notifyParticipants params x
>> print x
parseParameters :: [String] -> Maybe Parameters
parseParameters xs
= liftA3 Parameters participants' emailSetting' arrangements'
where
participants' = xs !? 0 >>= parseParticipants
emailSetting' = xs !? 1 >>= parseEmailSettings
arrangements' = whenNothingJust []
$ xs !? 2 >>= parseSecretSantaHistory
parseParticipants :: String -> Maybe [Participant]
parseParticipants = readMay
parseEmailSettings :: String -> Maybe EmailSettings
parseEmailSettings = readMay
parseSecretSantaHistory :: String -> Maybe [Arrangement]
parseSecretSantaHistory = readMay
errorParametersMessage :: IO ()
errorParametersMessage
= getProgName
>>= \x -> putStr
$ unlines
[ ""
, "Failed to parse parameters!"
, "Expected:"
, " ./" ++ x ++ " <Participants> <EmailSettings> <PreviousArangements>"
, ""
]
whenNothingJust :: a -> Maybe a -> Maybe a
whenNothingJust = (Just .) . fromMaybe
(!?) :: [a] -> Int -> Maybe a
(!?) xs n
| null xs
|| n < 0 = Nothing
| n == 0 = Just $ head xs
| otherwise = tail xs !? (n-1)
-}