import Data.Bool import Data.List main = getContents >>= putStrLn . bool "No" "Yes" . pazzle15 . map read . words pazzle15 ns = null $ snd $ until (\(x,_) -> x==16) move (s, ds) where (_,s) = maybe undefined id $ find ((==0) . fst) nis ds = filter (\(x,y) -> neighbor x y) nis nis = zip ns [1..16] move (x,ds) = case find ((==x) . fst) ds of Nothing -> (16, (0,0) : ds) Just (x,y) -> (y, delete (x,y) ds) neighbor x y = (qx==qy && abs (x-y) == 1) || (rx==ry && abs (x-y) == 4) where (qx,rx) = divMod (x-1) 4 (qy,ry) = divMod (y-1) 4