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)]
|
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 [] = []
|
|
|
|
|
|
|
|
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
|