[hs] Migrate 2019_04

This commit is contained in:
Joscha 2020-12-06 12:06:31 +00:00
parent c32cc9d47e
commit 53aacea987
3 changed files with 36 additions and 10 deletions

View file

@ -8,6 +8,7 @@ module Aoc.Parse
, untilSpace , untilSpace
, untilEol , untilEol
, lineChar , lineChar
, digit
) where ) where
import Data.Char import Data.Char
@ -39,3 +40,17 @@ untilEol = takeWhileP (Just "non-newline character") (/= '\n')
lineChar :: Parser Char lineChar :: Parser Char
lineChar = label "non-newline character" $ satisfy (/= '\n') lineChar = label "non-newline character" $ satisfy (/= '\n')
digit :: Num a => Parser a
digit = foldr1 (<|>)
[ 0 <$ char '0'
, 1 <$ char '1'
, 2 <$ char '2'
, 3 <$ char '3'
, 4 <$ char '4'
, 5 <$ char '5'
, 6 <$ char '6'
, 7 <$ char '7'
, 8 <$ char '8'
, 9 <$ char '9'
]

View file

@ -6,10 +6,12 @@ import Aoc.Day
import qualified Aoc.Y2019.D01 as D01 import qualified Aoc.Y2019.D01 as D01
import qualified Aoc.Y2019.D02 as D02 import qualified Aoc.Y2019.D02 as D02
import qualified Aoc.Y2019.D03 as D03 import qualified Aoc.Y2019.D03 as D03
import qualified Aoc.Y2019.D04 as D04
year :: Year year :: Year
year = Year 2019 year = Year 2019
[ ( 1, D01.day) [ ( 1, D01.day)
, ( 2, D02.day) , ( 2, D02.day)
, ( 3, D03.day) , ( 3, D03.day)
, ( 4, D04.day)
] ]

View file

@ -1,7 +1,10 @@
module Aoc.Y2019.A04 module Aoc.Y2019.D04
( solve201904 ( day
) where ) where
import Aoc.Day
import Aoc.Parse
type Passwd = (Int, Int, Int, Int, Int, Int) type Passwd = (Int, Int, Int, Int, Int, Int)
passwds :: [Passwd] passwds :: [Passwd]
@ -17,9 +20,6 @@ passwds = do
getRange :: Passwd -> Passwd -> [Passwd] getRange :: Passwd -> Passwd -> [Passwd]
getRange lowerBound upperBound = takeWhile (<=upperBound) $ dropWhile (<lowerBound) passwds getRange lowerBound upperBound = takeWhile (<=upperBound) $ dropWhile (<lowerBound) passwds
passwdsInRange :: [Passwd]
passwdsInRange = getRange (1,3,6,8,1,8) (6,8,5,9,7,9)
seqDigits :: Passwd -> Bool seqDigits :: Passwd -> Bool
seqDigits (a, b, c, d, e, f) = or $ zipWith (==) [a,b,c,d,e] [b,c,d,e,f] seqDigits (a, b, c, d, e, f) = or $ zipWith (==) [a,b,c,d,e] [b,c,d,e,f]
@ -30,10 +30,19 @@ sepSeqDigits (a, b, c, d, e, f) =
rightEdge = zipWith (/=) [b,c,d,e,f] [c,d,e,f,0] rightEdge = zipWith (/=) [b,c,d,e,f] [c,d,e,f,0]
in or $ zipWith3 (\x y z -> x && y && z) leftEdge pair rightEdge in or $ zipWith3 (\x y z -> x && y && z) leftEdge pair rightEdge
solve201904 :: FilePath -> IO () parser :: Parser (Passwd, Passwd)
solve201904 _ = do parser = (,) <$> (pPasswd <* char '-') <*> (pPasswd <* newline)
putStrLn ">> Part 1" where
print $ length $ filter seqDigits passwdsInRange pPasswd = (,,,,,) <$> digit <*> digit <*> digit <*> digit <*> digit <*> digit
solver :: (Passwd, Passwd) -> IO ()
solver (low, high) = do
putStrLn ">> Part 1"
print $ length $ filter seqDigits $ getRange low high
putStrLn ""
putStrLn ">> Part 2" putStrLn ">> Part 2"
print $ length $ filter sepSeqDigits passwdsInRange print $ length $ filter sepSeqDigits $ getRange low high
day :: Day
day = dayParse parser solver