-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.hs
57 lines (52 loc) · 1.21 KB
/
Day04.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
-- |
-- Module : AOC2016.Day04
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 4. See "AOC.Solver" for the types used in this module!
module AOC2016.Day04 (
day04a,
day04b,
) where
import AOC.Common (caeser, firstJust, freqList)
import AOC.Solver ((:~>) (..))
import Control.Monad (guard)
import Data.Finite (modulo)
import Data.List (isInfixOf)
import Data.List.Split (splitOneOf)
import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)
data Room = Room
{ rName :: [String]
, rId :: Int
}
deriving stock (Show)
parseRoom :: String -> Maybe Room
parseRoom str = do
_ : c : n : rs <- Just . reverse . splitOneOf "-[]" $ str
guard
. all (uncurry (==))
. zip c
. map snd
. freqList
. concat
$ rs
Room (reverse rs) <$> readMaybe n
day04a :: _ :~> _
day04a =
MkSol
{ sParse = Just . mapMaybe parseRoom . lines
, sShow = show
, sSolve = Just . sum . map rId
}
day04b :: _ :~> _
day04b =
MkSol
{ sParse = Just . mapMaybe parseRoom . lines
, sShow = show
, sSolve = firstJust $ \(Room n i) ->
i <$ do
guard $ "north" `isInfixOf` (concatMap . map) (caeser (modulo (fromIntegral i))) n
}