26 lines
791 B
Haskell
26 lines
791 B
Haskell
{-# LANGUAGE TupleSections #-}
|
|
module Day2B where
|
|
import Data.Maybe (mapMaybe)
|
|
import Data.Foldable (toList)
|
|
import qualified Data.Sequence as Seq
|
|
|
|
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
|
|
|
|
distance :: (String, String) -> Maybe String
|
|
distance (a, b) = distance' 0 Seq.empty a b
|
|
|
|
distances :: [(String, String)] -> String
|
|
distances xs = head $ mapMaybe distance xs
|
|
|
|
combinations :: [String] -> [(String, String)]
|
|
combinations (x:xs) = map (x,) xs ++ combinations xs
|
|
combinations [] = []
|
|
|
|
main :: IO ()
|
|
main = interact $ distances . combinations . lines
|