Advertisement
yingpotter

FF13-2 Clock Puzzle Solver

Mar 21st, 2012
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. ----- the numbers on the clock are [3,2,1,1,2,1] and their orders are [0..5] correspondingly
  4. testdata1 :: [Int]
  5. testdata1 = [3,2,1,1,2,1]
  6.  
  7.  
  8. ----- convert the order of the elements to a list of possible next moves
  9. ----- 1 (order) -> [3,2,1,1,2,1] (list of numbers) -> [3,5] (list of orders)
  10. getSRoutine :: Int -> [Int] -> [Int]
  11. getSRoutine a ls = nub [fd, sd]
  12.                    where len = length ls
  13.                          fd = (a + ls!!a) `mod` len
  14.                          sd = (a - ls!!a) `mod` len
  15.  
  16. ----- a naive backtrack solver to show the orders of all possible routes
  17. ----- REMARK: the orders are of [0..], not [1..]
  18. tokSolver :: [Int] -> [[Int]]
  19. tokSolver [] = []
  20. tokSolver ls = concat [ tokSolver' len [[a]] | a <- [0..len-1] ]
  21.               where len = length ls
  22.                     tokSolver' :: Int -> [[Int]] -> [[Int]]
  23.                      tokSolver' 1 routes = routes
  24.                     tokSolver' len routes = tokSolver' (len-1) (concatMap nextMove routes)
  25.                                             where nextMove route = [ route ++ [s] | s <- filter (\x -> x `notElem` route) $ getSRoutine (last route) ls ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement