Compare commits
No commits in common. "70bf616bad80dcfa7762e510ce86eae799a8f240" and "3b0fde248fb3e5677f1a392b56566691eede88bc" have entirely different histories.
70bf616bad
...
3b0fde248f
3 changed files with 5 additions and 30 deletions
|
@ -1,16 +1,9 @@
|
|||
module AoC where
|
||||
|
||||
import Text.ParserCombinators.ReadP
|
||||
|
||||
(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
|
||||
f .: g = (f .) . g
|
||||
infixl 8 .:
|
||||
|
||||
oneCompleteResult :: ReadP a -> String -> Maybe a
|
||||
oneCompleteResult p s = case readP_to_S (p <* eof) s of
|
||||
[(x, "")] -> Just x
|
||||
_ -> Nothing
|
||||
|
||||
runAoC :: (Show r1, Show r2) => (String -> i) -> (i -> r1) -> (i -> r2) -> IO ()
|
||||
runAoC inputTransform part1 part2 = do
|
||||
contents <- inputTransform <$> getContents
|
||||
|
|
|
@ -89,6 +89,11 @@ parseFieldNames = spaceSeparated parseFieldName
|
|||
parsePassport :: ReadP [Field]
|
||||
parsePassport = spaceSeparated parseField
|
||||
|
||||
oneCompleteResult :: ReadP a -> String -> Maybe a
|
||||
oneCompleteResult p s = case readP_to_S (p <* eof) s of
|
||||
[(x, "")] -> Just x
|
||||
_ -> Nothing
|
||||
|
||||
splitOnEmptyLines :: String -> [String]
|
||||
splitOnEmptyLines = map (intercalate " ") . filter (not . any null) . groupBy ((==) `on` null) . lines
|
||||
|
||||
|
|
23
day5/day5.hs
23
day5/day5.hs
|
@ -1,23 +0,0 @@
|
|||
import AoC
|
||||
|
||||
import Data.List (sort)
|
||||
import Data.Maybe (fromJust)
|
||||
import Numeric (readInt)
|
||||
import Text.ParserCombinators.ReadP
|
||||
|
||||
binarify :: String -> Maybe Int
|
||||
binarify = oneCompleteResult . readS_to_P $ readInt 2 (`elem` "BFLR") digitValue
|
||||
where digitValue 'F' = 0
|
||||
digitValue 'B' = 1
|
||||
digitValue 'L' = 0
|
||||
digitValue 'R' = 1
|
||||
|
||||
findHole :: (Enum a, Eq a) => [a] -> Maybe a
|
||||
findHole (x:y:ys) | y == next = findHole $ y:ys
|
||||
| otherwise = Just next
|
||||
where next = succ x
|
||||
findHole _ = Nothing
|
||||
|
||||
main = runAoC (fmap (fromJust . binarify) <$> lines) part1 part2
|
||||
where part1 = foldr1 max
|
||||
part2 = fromJust . findHole . sort
|
Loading…
Reference in a new issue