Skip to content

Commit aa7d1a1

Browse files
committed
Refactor
1 parent e5a548a commit aa7d1a1

File tree

6 files changed

+93
-55
lines changed

6 files changed

+93
-55
lines changed

advent-of-code-y2022.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ library
3131
Day4
3232
Day5
3333
Day6
34+
Day7
35+
Days
3436
Lib
3537
Utils
3638
other-modules:

app/Main.hs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
module Main (main) where
33

44
import System.Environment
5-
import System.IO (openFile, hGetContents, IOMode (..))
5+
import System.IO (openFile, hGetContents', IOMode (..))
66
import Data.List (isInfixOf)
7-
import Data.Maybe (fromJust)
87

98
import qualified Data.ByteString.Lazy.Char8 as L8
109
import qualified Data.ByteString.Char8 as C8
@@ -13,45 +12,23 @@ import Network.HTTP.Client.TLS
1312
import Network.HTTP.Simple
1413

1514
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
2217

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
4825

4926
getSessionId :: IO String
5027
getSessionId = do
5128
handle <- openFile "session_id" ReadMode
52-
hGetContents handle
29+
hGetContents' handle
5330

54-
downloadInput :: DaySpec -> IO (Maybe String)
31+
downloadInput :: DaySpec -> IO String
5532
downloadInput day = do
5633
manager <- newManager tlsManagerSettings
5734
sessionId <- getSessionId
@@ -62,23 +39,30 @@ downloadInput day = do
6239
response <- httpLBS request'
6340
let body = L8.unpack $ responseBody response
6441
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"
6645
else
67-
return $ Just body
46+
return body
6847

6948
main :: IO ()
7049
main = do
7150
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

src/Day7.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Day7 (day7) where
2+
3+
import Lib
4+
5+
day7 :: Day
6+
day7 = Day { part1 = id
7+
, part2 = Nothing
8+
}

src/Days.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Days ( days
2+
, getDayFromDaySpec
3+
, getPartFromDayPartSpec
4+
) where
5+
6+
import Control.Monad (join)
7+
8+
import Lib
9+
import Utils (maybeAt)
10+
11+
import Day1
12+
import Day2
13+
import Day3
14+
import Day4
15+
import Day5
16+
import Day6
17+
import Day7
18+
19+
days :: [Day]
20+
days = [ day1
21+
, day2
22+
, day3
23+
, day4
24+
, day5
25+
, day6
26+
, day7
27+
]
28+
29+
getDayFromDaySpec :: DaySpec -> Maybe Day
30+
getDayFromDaySpec = (flip maybeAt) days . subtract 1
31+
32+
getPartFromDayPartSpec :: DayPartSpec -> Maybe Part
33+
getPartFromDayPartSpec (DayPartSpec daySpec' partSpec') =
34+
join $ getPartFromPartSpec partSpec' <$> getDayFromDaySpec daySpec'
35+

src/Lib.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ getLatestPartSpec day =
3131
(Just _) -> Part2
3232
Nothing -> Part1
3333

34-
getPartSpecByNum :: Int -> PartSpec
35-
getPartSpecByNum 1 = Part1
36-
getPartSpecByNum 2 = Part2
37-
getPartSpecByNum _ = error "too many parts for me!"
34+
getPartSpecByNum :: Int -> Maybe PartSpec
35+
getPartSpecByNum 1 = Just Part1
36+
getPartSpecByNum 2 = Just Part2
37+
getPartSpecByNum _ = Nothing

src/Utils.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
module Utils (pair, splitOn) where
1+
module Utils ( pair
2+
, splitOn
3+
, maybeAt
4+
) where
5+
6+
-- (!!) but returns Maybe
7+
maybeAt :: Int -> [a] -> Maybe a
8+
maybeAt idx xs = case drop idx xs of
9+
[] -> Nothing
10+
(x:_) -> (Just x)
211

312
-- Split a list at the appearance of a given item. This removes said item.
413
splitOn :: (Eq a) => a -> [a] -> [[a]]

0 commit comments

Comments
 (0)