2018/Day02/Day2B.hs

26 lines
791 B
Haskell
Raw Normal View History

2018-12-02 15:52:36 +00:00
{-# LANGUAGE TupleSections #-}
module Day2B where
import Data.Maybe (mapMaybe)
import Data.Foldable (toList)
import qualified Data.Sequence as Seq
2018-12-02 15:52:36 +00:00
distance' :: Int -> (Seq.Seq Char) -> String -> String -> Maybe String
distance' n common (a:x) (b:y)
| a == b = distance' n (common Seq.|> a) x y
| otherwise = distance' (n + 1) common x y
distance' 1 common "" "" = Just(toList common)
distance' _ _ _ _ = Nothing
2018-12-02 15:52:36 +00:00
distance :: (String, String) -> Maybe String
distance (a, b) = distance' 0 Seq.empty a b
distances :: [(String, String)] -> String
distances xs = head $ mapMaybe distance xs
2018-12-02 15:52:36 +00:00
combinations :: [String] -> [(String, String)]
2018-12-02 20:15:38 +00:00
combinations (x:xs) = map (x,) xs ++ combinations xs
2018-12-02 15:52:36 +00:00
combinations [] = []
main :: IO ()
main = interact $ distances . combinations . lines