Created
January 8, 2014 10:56
-
-
Save tkych/8315049 to your computer and use it in GitHub Desktop.
Rails on Tiles, http://qiita.com/Nabetani/items/0ddde0164a745cd09c34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; Last modified: 2014-01-08 19:55:42 tkych | |
;;==================================================================== | |
;; Rails on Tiles | |
;;==================================================================== | |
;; - [Rails on Tiles 〜 横へな 2012.11.9の参考問題](http://nabetani.sakura.ne.jp/hena/ord5railsontiles/) | |
;; - [オフラインリアルタイムどう書く第5回の参考問題](http://qiita.com/Nabetani/items/0ddde0164a745cd09c34) | |
;;-------------------------------------------------------------------- | |
;; Package | |
;;-------------------------------------------------------------------- | |
(in-package :cl-user) | |
(defpackage :rails-on-tiles (:use :cl)) | |
(in-package :rails-on-tiles) | |
;;-------------------------------------------------------------------- | |
;; Main | |
;;-------------------------------------------------------------------- | |
#| MEMO | |
* position | |
+-0-+ | |
| | | |
3 1 | |
| | | |
+-2-+ | |
* tile (e.g. 0th tile) | |
+---+ | |
| | | |
| A | | |
| | | |
+---+-a-+---+ | |
| | | | | | |
| D d-+-b B | | |
| | | | | | |
+---+-c-+---+ | |
| | | |
| C | | |
| | | |
+---+ | |
a -> C's 0 | |
b -> D's 1 | |
c -> A's 2 | |
d -> B's 3 | |
tile 0 := (0 1 2 3) | |
|# | |
(defun parse (input) | |
(map 'list (lambda (c) | |
(ecase c | |
(#\0 '(0 1 2 3)) | |
(#\1 '(3 2 1 0)) | |
(#\2 '(1 0 3 2)))) | |
input)) | |
(defun next-route (route pos) | |
(+ route | |
(ecase pos | |
(0 3) (1 -1) (2 -3) (3 1)))) | |
(defun main (input) | |
(with-output-to-string (s) | |
(loop :with tiles := (parse input) | |
:with tile := (nth 1 tiles) | |
:for pos := 0 :then (nth pos tile) | |
:for route := 1 :then (next-route route pos) | |
:until (or (< route 0) | |
(<= 9 route) | |
;; right limit | |
(and (= pos 1) | |
(or (= route 2) (= route 5))) | |
;; left limit | |
(and (= pos 3) | |
(or (= route 3) (= route 6)))) | |
:do (princ (char "ABCDEFGHI" route) s) | |
(setf tile (nth route tiles))))) | |
;;-------------------------------------------------------------------- | |
;; Tests | |
;;-------------------------------------------------------------------- | |
(defun =>? (got expected) | |
(assert (string= got expected))) | |
(progn | |
(=>? (main "101221102") "BEDGHIFEH") | |
(=>? (main "000000000") "BEH") | |
(=>? (main "111111111") "BCF") | |
(=>? (main "222222222") "BAD") | |
(=>? (main "000211112") "BEFIHEDGH") | |
(=>? (main "221011102") "BADGHIFEBCF") | |
(=>? (main "201100112") "BEHIFCBADEF") | |
(=>? (main "000111222") "BEFIH") | |
(=>? (main "012012012") "BC") | |
(=>? (main "201120111") "BEDABCFI") | |
(=>? (main "220111122") "BADEHGD") | |
(=>? (main "221011022") "BADG") | |
(=>? (main "111000112") "BCFIHEBA") | |
(=>? (main "001211001") "BEFI") | |
(=>? (main "111222012") "BCFEHIF") | |
(=>? (main "220111211") "BADEHI") | |
(=>? (main "211212212") "BCFEBAD") | |
(=>? (main "002112210") "BEFC") | |
(=>? (main "001010221") "BEF") | |
(=>? (main "100211002") "BEFIHG") | |
(=>? (main "201212121") "BEFCBAD") | |
) | |
;;==================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment