Skip to content

Instantly share code, notes, and snippets.

@budu
Created August 27, 2011 05:06
Show Gist options
  • Select an option

  • Save budu/1175007 to your computer and use it in GitHub Desktop.

Select an option

Save budu/1175007 to your computer and use it in GitHub Desktop.
Snake my_turn_in puzzle implementation in Haskell
-- Snake my_turn_in puzzle implementation
--
-- The direction argument accept an Ordering value where GT is
-- :forward, LT is :backward and EQ is :same.
--
-- Problem definition can be found in:
-- https://github.com/budu/Puzzles
myTurnIn :: (Ord a, Integral a) => a -> a -> Ordering -> a -> a
myTurnIn _ _ _ 1 = 0
myTurnIn p1 p2 d s
| mc == 0 && d == EQ = 0
| rd == d || rd == EQ || d == EQ = dist - rdist
| otherwise = rdist
where mp = p1 - 1
mc = mp - p2 + 1
rd = 0 `compare` mc
rdist = abs mc
dist = 1 + 2 *
| mc < 0 || rd == EQ && d == GT = s - mp - 1
| otherwise = mp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment