Format files

pull/1/head
Gaël Depreeuw 4 years ago
parent e358607a33
commit 0e125a1cb0
Signed by: Mithror
GPG Key ID: 8AB218ABA4867F78
  1. 14
      src/Day1.hs
  2. 53
      src/Day2.hs

@ -4,7 +4,9 @@ import qualified Data.IntSet as DIS
import Data.Maybe (mapMaybe)
type Target = Int
type Expenses = [Int]
type Expenses' = DIS.IntSet
day1 :: IO ()
@ -20,14 +22,14 @@ day1 = do
-- the two numbers.
fixExpenseReport :: Target -> Int -> Expenses -> Maybe Int
fixExpenseReport t c es
| c <= 0 = error "unsupported"
| otherwise = fixExpenseReport' t c (DIS.fromList es)
| c <= 0 = error "unsupported"
| otherwise = fixExpenseReport' t c (DIS.fromList es)
fixExpenseReport' :: Target -> Int -> Expenses' -> Maybe Int
fixExpenseReport' t 1 es = if DIS.member t es then Just t else Nothing
fixExpenseReport' t c es =
let foo x = (*x) <$> fixExpenseReport' (t-x) (c-1) (DIS.delete x es)
bar = mapMaybe foo (DIS.toList es) in
case bar of
let foo x = (* x) <$> fixExpenseReport' (t - x) (c -1) (DIS.delete x es)
bar = mapMaybe foo (DIS.toList es)
in case bar of
[] -> Nothing
(x:_) -> Just x
(x : _) -> Just x

@ -2,41 +2,45 @@
module Day2 (day2) where
import qualified Data.Text as T
import qualified Data.Attoparsec.Text as P
import Data.List (elemIndices)
import qualified Data.Text as T
type Max = Int
type Min = Int
type Letter = Char
type Validator = Policy -> String -> Bool
data Policy = Policy { lBound :: Min
, uBound :: Max
, letter :: Letter
}
data Policy = Policy
{ lBound :: Min,
uBound :: Max,
letter :: Letter
}
parserPolicy :: P.Parser Policy
parserPolicy = do
lB <- P.decimal
_ <- P.char '-'
uB <- P.decimal
_ <- P.char ' '
le <- P.letter
_ <- P.string ": "
return $ Policy lB uB le
lB <- P.decimal
_ <- P.char '-'
uB <- P.decimal
_ <- P.char ' '
le <- P.letter
_ <- P.string ": "
return $ Policy lB uB le
validatePolicy :: Validator
validatePolicy p s =
let l = length $ filter (== letter p) s in
l >= lBound p && l <= uBound p
let l = length $ filter (== letter p) s
in l >= lBound p && l <= uBound p
validateLine :: Validator -> String -> Bool
validateLine v s =
case P.parse parserPolicy (T.pack s) of
P.Fail {} -> error "day2 - parser error"
P.Partial _ -> error "day2 - input error"
P.Done i r -> v r (T.unpack i)
case P.parse parserPolicy (T.pack s) of
P.Fail {} -> error "day2 - parser error"
P.Partial _ -> error "day2 - input error"
P.Done i r -> v r (T.unpack i)
xor :: Bool -> Bool -> Bool
xor True = not
@ -44,16 +48,15 @@ xor False = id
validateCorrectPolicy :: Validator
validateCorrectPolicy p s =
let index1 = lBound p - 1
index2 = uBound p - 1
indices = elemIndices (letter p) s in
xor (index1 `elem` indices) (index2 `elem` indices)
let index1 = lBound p - 1
index2 = uBound p - 1
indices = elemIndices (letter p) s
in xor (index1 `elem` indices) (index2 `elem` indices)
day2 :: IO ()
day2 = do
rs <- lines <$> readFile "./input/day2"
putStr "[Day 2-1] # valid passwords: "
print . length . filter (==True) . fmap (validateLine validatePolicy) $ rs
print . length . filter (== True) . fmap (validateLine validatePolicy) $ rs
putStr "[Day 2-2] # valid passwords: "
print . length . filter (==True) . fmap (validateLine validateCorrectPolicy) $ rs
print . length . filter (== True) . fmap (validateLine validateCorrectPolicy) $ rs

Loading…
Cancel
Save