Skip to content

Instantly share code, notes, and snippets.

@NaokiKuzumi
Created February 26, 2013 01:25
Show Gist options
  • Save NaokiKuzumi/5034983 to your computer and use it in GitHub Desktop.
Save NaokiKuzumi/5034983 to your computer and use it in GitHub Desktop.
(defstruct node
(rireki nil :type list)
(state '(3 2 1 2) :type (and list (not null))))
(defun inner-solve (slist)
(if
(issolved (car slist))
(printrireki (car slist))
(inner-solve (nconc (cdr slist) (nextnodesforall (car slist) '(0 1 2 3))))))
(defun issolved (node)
(equal (node-state node) '(2 3 0 1)))
(defun printrireki (node)
(print (node-rireki node)))
(defun nextnodesforall (node list)
(iter (for a in list)
(collect
(make-node
:rireki (append (node-rireki node) (list a))
:state (nextstate (node-state node) a)))))
(defun nth1- (list n)
(if (= n 0)
(cons (1- (car list)) (cdr list))
(cons (car list) (nth1- (cdr list) (1- n)))))
(defun nextstate (state move)
(mapcar #'(lambda (x) (mod x 4)) (nth1- (mapcar #'1+ state) (mod (1+ move) 4))))
(defun solve (state)
(inner-solve (list (make-node :state state))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment