[hs] Solve 2020_02

This commit is contained in:
Joscha 2020-12-03 11:12:27 +00:00
parent 6ad7c5108d
commit 7183f49a6b
4 changed files with 57 additions and 2 deletions

44
hs/src/Aoc/Y2020/D02.hs Normal file
View file

@ -0,0 +1,44 @@
module Aoc.Y2020.D02
( day
) where
import qualified Data.Text as T
import Aoc.Day
import Aoc.Parse
data Line = Line
{ lMin :: Int
, lMax :: Int
, lChar :: Char
, lPw :: T.Text
}
parser :: Parser [Line]
parser = manyLines $ Line
<$> (decimal <* char '-')
<*> (decimal <* space)
<*> (anySingle <* char ':' <* space)
<*> untilEol
validCount :: Line -> Bool
validCount l = n >= lMin l && n <= lMax l
where
n = T.count (T.singleton $ lChar l) $ lPw l
validPositions :: Line -> Bool
validPositions l = (left == lChar l) /= (right == lChar l)
where
left = T.index (lPw l) (lMin l - 1)
right = T.index (lPw l) (lMax l - 1)
solver :: [Line] -> IO ()
solver ls = do
putStrLn ">> Part 1"
print $ length $ filter validCount ls
putStrLn ">> Part 2"
print $ length $ filter validPositions ls
day :: Day
day = dayParse "2020_02" parser solver