2020-02 in haskell
This commit is contained in:
parent
96efc40e53
commit
662b77da67
4 changed files with 1122 additions and 0 deletions
3
2020/02-Password_Philosophy/example
Normal file
3
2020/02-Password_Philosophy/example
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
1-3 a: abcde
|
||||||
|
1-3 b: cdefg
|
||||||
|
2-9 c: ccccccccc
|
59
2020/02-Password_Philosophy/first.hs
Normal file
59
2020/02-Password_Philosophy/first.hs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
-- requires cabal install --lib megaparsec
|
||||||
|
module Main (main) where
|
||||||
|
|
||||||
|
import Control.Monad (void, when)
|
||||||
|
import Data.Void
|
||||||
|
import Text.Megaparsec
|
||||||
|
import Text.Megaparsec.Char
|
||||||
|
import System.Exit (die)
|
||||||
|
|
||||||
|
exampleExpectedOutput = 2
|
||||||
|
|
||||||
|
data Rule = Rule { lower :: Int
|
||||||
|
, higher :: Int
|
||||||
|
, elt :: Char
|
||||||
|
, pass :: String
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
type Parser = Parsec Void String
|
||||||
|
|
||||||
|
parseRule :: Parser Rule
|
||||||
|
parseRule = do
|
||||||
|
l <- try (some digitChar)
|
||||||
|
void (char '-')
|
||||||
|
h <- try (some digitChar)
|
||||||
|
void (char ' ')
|
||||||
|
e <- anySingle
|
||||||
|
void (string ": ")
|
||||||
|
pass <- (many letterChar)
|
||||||
|
void (char '\n')
|
||||||
|
return Rule { lower = read l, higher = read h, elt = e, pass = pass }
|
||||||
|
|
||||||
|
parseRules :: Parser [Rule]
|
||||||
|
parseRules = do
|
||||||
|
rules <- many parseRule
|
||||||
|
eof
|
||||||
|
return rules
|
||||||
|
|
||||||
|
parseInput :: String -> IO [Rule]
|
||||||
|
parseInput filename = do
|
||||||
|
input <- readFile filename
|
||||||
|
case runParser parseRules filename input of
|
||||||
|
Left bundle -> die $ errorBundlePretty bundle
|
||||||
|
Right rules -> return rules
|
||||||
|
|
||||||
|
validateRule :: Rule -> Int
|
||||||
|
validateRule Rule{lower=l, higher=h, elt=e, pass=pass} = if (n >= l && n <= h) then 1 else 0
|
||||||
|
where
|
||||||
|
n = length $ filter (== e) pass
|
||||||
|
|
||||||
|
compute :: [Rule] -> Int
|
||||||
|
compute = sum . map validateRule
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
example <- parseInput "example"
|
||||||
|
let exampleOutput = compute example
|
||||||
|
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||||
|
input <- parseInput "input"
|
||||||
|
print $ compute input
|
1000
2020/02-Password_Philosophy/input
Normal file
1000
2020/02-Password_Philosophy/input
Normal file
File diff suppressed because it is too large
Load diff
60
2020/02-Password_Philosophy/second.hs
Normal file
60
2020/02-Password_Philosophy/second.hs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
-- requires cabal install --lib megaparsec
|
||||||
|
module Main (main) where
|
||||||
|
|
||||||
|
import Control.Monad (void, when)
|
||||||
|
import Data.Void
|
||||||
|
import Text.Megaparsec
|
||||||
|
import Text.Megaparsec.Char
|
||||||
|
import System.Exit (die)
|
||||||
|
|
||||||
|
exampleExpectedOutput = 1
|
||||||
|
|
||||||
|
data Rule = Rule { lower :: Int
|
||||||
|
, higher :: Int
|
||||||
|
, elt :: Char
|
||||||
|
, pass :: String
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
type Parser = Parsec Void String
|
||||||
|
|
||||||
|
parseRule :: Parser Rule
|
||||||
|
parseRule = do
|
||||||
|
l <- try (some digitChar)
|
||||||
|
void (char '-')
|
||||||
|
h <- try (some digitChar)
|
||||||
|
void (char ' ')
|
||||||
|
e <- anySingle
|
||||||
|
void (string ": ")
|
||||||
|
pass <- (many letterChar)
|
||||||
|
void (char '\n')
|
||||||
|
return Rule { lower = (read l) - 1, higher = (read h) - 1, elt = e, pass = pass }
|
||||||
|
|
||||||
|
parseRules :: Parser [Rule]
|
||||||
|
parseRules = do
|
||||||
|
rules <- many parseRule
|
||||||
|
eof
|
||||||
|
return rules
|
||||||
|
|
||||||
|
parseInput :: String -> IO [Rule]
|
||||||
|
parseInput filename = do
|
||||||
|
input <- readFile filename
|
||||||
|
case runParser parseRules filename input of
|
||||||
|
Left bundle -> die $ errorBundlePretty bundle
|
||||||
|
Right rules -> return rules
|
||||||
|
|
||||||
|
validateRule :: Rule -> Int
|
||||||
|
validateRule Rule{lower=l, higher=h, elt=e, pass=pass} = if (a /= b) then 1 else 0
|
||||||
|
where
|
||||||
|
a = e == pass !! l
|
||||||
|
b = e == pass !! h
|
||||||
|
|
||||||
|
compute :: [Rule] -> Int
|
||||||
|
compute = sum . map validateRule
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
example <- parseInput "example"
|
||||||
|
let exampleOutput = compute example
|
||||||
|
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||||
|
input <- parseInput "input"
|
||||||
|
print $ compute input
|
Loading…
Add table
Reference in a new issue