2
2
module Main (main ) where
3
3
4
4
import System.Environment
5
- import System.IO (openFile , hGetContents , IOMode (.. ))
5
+ import System.IO (openFile , hGetContents' , IOMode (.. ))
6
6
import Data.List (isInfixOf )
7
- import Data.Maybe (fromJust )
8
7
9
8
import qualified Data.ByteString.Lazy.Char8 as L8
10
9
import qualified Data.ByteString.Char8 as C8
@@ -13,45 +12,23 @@ import Network.HTTP.Client.TLS
13
12
import Network.HTTP.Simple
14
13
15
14
import Lib
16
- import Day1
17
- import Day2
18
- import Day3
19
- import Day4
20
- import Day5
21
- import Day6
15
+ import Utils (maybeAt )
16
+ import Days
22
17
23
- days :: [Day ]
24
- days = [ day1
25
- , day2
26
- , day3
27
- , day4
28
- , day5
29
- , day6
30
- ]
31
-
32
- getDayFromDaySpec :: DaySpec -> Day
33
- getDayFromDaySpec = (days !! ) . (subtract 1 )
34
-
35
- getPartFromDayPartSpec :: DayPartSpec -> Maybe Part
36
- getPartFromDayPartSpec spec =
37
- (getPartFromPartSpec $ partSpec spec) day
38
- where day = getDayFromDaySpec $ daySpec spec
39
-
40
- parseDayPartSpec :: [String ] -> DayPartSpec
41
- parseDayPartSpec args = case args of
42
- [] -> let daySp = (length days)
43
- in DayPartSpec daySp $ getLatestPartSpec $ getDayFromDaySpec daySp
44
- [dayStr] -> let daySp = (read dayStr)
45
- in DayPartSpec daySp $ getLatestPartSpec $ getDayFromDaySpec daySp
46
- (dayStr: partStr: _) -> let daySp = (read dayStr)
47
- in DayPartSpec daySp $ getPartSpecByNum $ read partStr
18
+ parseDayPartSpec :: [String ] -> Maybe DayPartSpec
19
+ parseDayPartSpec args =
20
+ DayPartSpec daySpec' <$> partSpec'
21
+ where daySpec' = maybe (length days) read
22
+ . maybeAt 0 $ args
23
+ partSpec' = maybe (getLatestPartSpec <$> getDayFromDaySpec daySpec') (getPartSpecByNum . read )
24
+ . maybeAt 1 $ args
48
25
49
26
getSessionId :: IO String
50
27
getSessionId = do
51
28
handle <- openFile " session_id" ReadMode
52
- hGetContents handle
29
+ hGetContents' handle
53
30
54
- downloadInput :: DaySpec -> IO ( Maybe String )
31
+ downloadInput :: DaySpec -> IO String
55
32
downloadInput day = do
56
33
manager <- newManager tlsManagerSettings
57
34
sessionId <- getSessionId
@@ -62,23 +39,30 @@ downloadInput day = do
62
39
response <- httpLBS request'
63
40
let body = L8. unpack $ responseBody response
64
41
if isInfixOf " Please log in" body then
65
- error " session id not supplied"
42
+ fail " session id not supplied"
43
+ else if isInfixOf " Please don't repeatedly request this endpoint before it unlocks!" body then
44
+ fail " input for day not yet released"
66
45
else
67
- return $ Just body
46
+ return body
68
47
69
48
main :: IO ()
70
49
main = do
71
50
args <- getArgs
72
- (dayPartSpec, input) <-
73
- if args /= [] && head args == " --download" then do
74
- let dayPartSpec = parseDayPartSpec $ tail args
75
- maybeInput <- downloadInput $ daySpec dayPartSpec
76
- case maybeInput of
77
- (Just input) -> return (dayPartSpec, input)
78
- Nothing -> error $ " no input to download for day " ++ (show $ daySpec dayPartSpec)
79
- else do
80
- let dayPartSpec = parseDayPartSpec args
81
- input <- getContents
82
- return (dayPartSpec, input)
83
- let part = fromJust $ getPartFromDayPartSpec $ dayPartSpec
84
- putStrLn $ part input
51
+ let (args', shouldDownload) = if args /= [] && head args == " --download"
52
+ then (tail args, True )
53
+ else (args, False )
54
+
55
+ dayPartSpec <- case parseDayPartSpec args' of
56
+ Just x -> return x
57
+ Nothing -> fail " no such day and/or part exists"
58
+
59
+ part <- case getPartFromDayPartSpec dayPartSpec of
60
+ Just x -> return x
61
+ Nothing -> fail " no such part exists"
62
+
63
+ input <-
64
+ if shouldDownload
65
+ then downloadInput $ daySpec dayPartSpec
66
+ else getContents
67
+
68
+ putStrLn . part $ input
0 commit comments