2018/Day2/Day2B.hs

26 lines
761 B
Haskell
Raw Normal View History

2018-12-02 15:52:36 +00:00
{-# LANGUAGE TupleSections #-}
module Day2B where
import Data.List (find)
import qualified Data.Sequence as S
import qualified Data.Foldable as F
dist' :: Int -> (S.Seq Char) -> String -> String -> (Int, String)
dist' n common (a:x) (b:y)
| a == b = dist' n (common S.|> a) x y
| otherwise = dist' (n + 1) common x y
dist' n common _ _ = (n, F.toList common)
dist :: String -> String -> (Int, String)
dist = dist' 0 S.empty
combinations :: [String] -> [(String, String)]
combinations (x:y:xs) = (x, y):(combinations (y:xs))
combinations [] = []
distances :: [(String, String)] -> String
distances xs = result
where Just(_, result) = find (\(x, _) -> x == 1) $ map (uncurry dist) xs
main :: IO ()
main = interact $ distances . combinations . lines