Can you connect the dots?
Haskell

import Data.List.Split
import qualified Data.Sequence as Q
import Data.List
import Control.Monad

data A a = S a | E a | P a | X deriving Eq

sc = foldr1 (Q.&gt;&lt;)
sp b x y p = Q.update y (Q.update x p $ b `Q.index` y) b
dt b c | S c `elem` sc b = E c
       | otherwise = S c
ad b [x, y, c] = sp b x y (dt b c)

ep b [x, y, c] = do
  let nps = filter ob [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]
      ns = map gp nps
  [b | E c `elem` ns] ++ do
    (x', y') &lt;- filter ((== X) . gp) nps
    ep (sp b x' y' (P c)) [x', y', c]
  where ob (u, v) = 0 &lt;= u &amp;&amp; u &lt; length (b `Q.index` 0) &amp;&amp; 0 &lt;= v &amp;&amp; v &lt; length b
        gp (u, v) = b `Q.index` v `Q.index` u

rd i = let [c, r] : ps = (map read . splitOn ",") &lt;$&gt; words i :: [[Int]]
           e = Q.replicate r $ Q.replicate c X
           cs = map last ps
           ss = nubBy (\[_,_,c1] [_,_,c2] -&gt; c1 == c2) ps
           b = foldl ad e ps
           bs = foldM ep b ss
       in if even (length cs) &amp;&amp; length ss == length cs `div` 2 &amp;&amp;
             (all (\[j,k] -&gt; j==k) . chunksOf 2 . sort $ cs) &amp;&amp;
             any (null . Q.elemIndicesL X . sc) bs
           then 1
           else 0

main = rd &lt;$&gt; getContents &gt;&gt;= print


Key


sc: Seq concat
sp: set position
dt: dot type (ie, start or end of line)
ad: add dot
ep: extend path
rd: run dots (primary pure algorithm)