Format files

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

@ -4,7 +4,9 @@ import qualified Data.IntSet as DIS
import Data.Maybe (mapMaybe) import Data.Maybe (mapMaybe)
type Target = Int type Target = Int
type Expenses = [Int] type Expenses = [Int]
type Expenses' = DIS.IntSet type Expenses' = DIS.IntSet
day1 :: IO () day1 :: IO ()
@ -27,7 +29,7 @@ fixExpenseReport' :: Target -> Int -> Expenses' -> Maybe Int
fixExpenseReport' t 1 es = if DIS.member t es then Just t else Nothing fixExpenseReport' t 1 es = if DIS.member t es then Just t else Nothing
fixExpenseReport' t c es = fixExpenseReport' t c es =
let foo x = (* x) <$> fixExpenseReport' (t - x) (c -1) (DIS.delete x es) let foo x = (* x) <$> fixExpenseReport' (t - x) (c -1) (DIS.delete x es)
bar = mapMaybe foo (DIS.toList es) in bar = mapMaybe foo (DIS.toList es)
case bar of in case bar of
[] -> Nothing [] -> Nothing
(x : _) -> Just x (x : _) -> Just x

@ -2,18 +2,22 @@
module Day2 (day2) where module Day2 (day2) where
import qualified Data.Text as T
import qualified Data.Attoparsec.Text as P import qualified Data.Attoparsec.Text as P
import Data.List (elemIndices) import Data.List (elemIndices)
import qualified Data.Text as T
type Max = Int type Max = Int
type Min = Int type Min = Int
type Letter = Char type Letter = Char
type Validator = Policy -> String -> Bool type Validator = Policy -> String -> Bool
data Policy = Policy { lBound :: Min data Policy = Policy
, uBound :: Max { lBound :: Min,
, letter :: Letter uBound :: Max,
letter :: Letter
} }
parserPolicy :: P.Parser Policy parserPolicy :: P.Parser Policy
@ -28,8 +32,8 @@ parserPolicy = do
validatePolicy :: Validator validatePolicy :: Validator
validatePolicy p s = validatePolicy p s =
let l = length $ filter (== letter p) s in let l = length $ filter (== letter p) s
l >= lBound p && l <= uBound p in l >= lBound p && l <= uBound p
validateLine :: Validator -> String -> Bool validateLine :: Validator -> String -> Bool
validateLine v s = validateLine v s =
@ -46,9 +50,8 @@ validateCorrectPolicy :: Validator
validateCorrectPolicy p s = validateCorrectPolicy p s =
let index1 = lBound p - 1 let index1 = lBound p - 1
index2 = uBound p - 1 index2 = uBound p - 1
indices = elemIndices (letter p) s in indices = elemIndices (letter p) s
xor (index1 `elem` indices) (index2 `elem` indices) in xor (index1 `elem` indices) (index2 `elem` indices)
day2 :: IO () day2 :: IO ()
day2 = do day2 = do

Loading…
Cancel
Save