From 1006f83446472f938a6475148cabacbd4a2adaf2 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 5 Dec 2020 10:21:57 +0100 Subject: [PATCH] 2020 day5/haskell: add solution --- 2020/day5/day5.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2020/day5/day5.hs diff --git a/2020/day5/day5.hs b/2020/day5/day5.hs new file mode 100644 index 0000000..2aa5dd2 --- /dev/null +++ b/2020/day5/day5.hs @@ -0,0 +1,23 @@ +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