26 lines
761 B
Haskell
26 lines
761 B
Haskell
|
{-# 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
|