-- http://www.codewars.com/kata/53223653a191940f2b000877
module Graph where

import qualified Data.IntMap.Strict as IM
import qualified Data.IntSet as IS
import Data.Char
import Control.Arrow
import Data.Foldable
import Data.Monoid
import Prelude hiding (any)

type Node = Char
type Arc  = (Node, Node)

solveGraph :: Node -> Node -> [Arc] -> Bool
solveGraph s' e' arcs' = any (e `IS.member`) steps' where
  s = ord s'
  e = ord e'
  arcs = IM.fromListWith IS.union $ map (ord *** IS.singleton . ord) arcs'
  step xs = xs <> foldMap id (IM.filterWithKey (\k _ -> k `IS.member` xs) arcs)
  steps = iterate step (IS.singleton s)
  steps' = map snd $ takeWhile (uncurry (/=)) $ zip steps (tail steps)
