Skip to content

Instantly share code, notes, and snippets.

@tkych
Created January 6, 2014 11:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tkych/8281271 to your computer and use it in GitHub Desktop.
;;;; Last modified: 2014-01-06 20:05:00 tkych
;;====================================================================
;; サイコロを転がす
;;====================================================================
;; - [サイコロを転がす 〜 横へな 2013.7.6](http://nabetani.sakura.ne.jp/hena/ord12rotdice/)
;; - [第12回オフラインリアルタイムどう書くの問題](http://qiita.com/Nabetani/items/f3cca410428f90333e28)
;;--------------------------------------------------------------------
;; Package
;;--------------------------------------------------------------------
(in-package :cl-user)
(defpackage :rolling-dice (:use :cl))
(in-package :rolling-dice)
;;--------------------------------------------------------------------
;; Main
;;--------------------------------------------------------------------
(defun back (side) (- 7 side))
(defun main (input)
(with-output-to-string (s)
(princ 1 s) ; はじまりはいつも1
(loop :for dir :across input
:with top := 1 :and north := 2 :and east := 4
:do (ecase dir
(#\N (psetf top (back north)
north top))
(#\E (psetf top (back east)
east top))
(#\S (psetf top north
north (back top)))
(#\W (psetf top east
east (back top))))
(princ top s))))
;;--------------------------------------------------------------------
;; Tests
;;--------------------------------------------------------------------
(defun =>? (got expected)
(assert (string= got expected)))
(progn
(=>? (main "NNESWWS") "15635624")
(=>? (main "EEEE") "13641")
(=>? (main "WWWW") "14631")
(=>? (main "SSSS") "12651")
(=>? (main "NNNN") "15621")
(=>? (main "EENN") "13651")
(=>? (main "WWNN") "14651")
(=>? (main "SSNN") "12621")
(=>? (main "NENNN") "153641")
(=>? (main "NWNNN") "154631")
(=>? (main "SWWWSNEEEN") "12453635421")
(=>? (main "SENWSWSNSWE") "123123656545")
(=>? (main "SSSWNNNE") "126546315")
(=>? (main "SWNWSSSWWE") "12415423646")
(=>? (main "ENNWWS") "1354135")
(=>? (main "ESWNNW") "1321365")
(=>? (main "NWSSE") "154135")
(=>? (main "SWNWEWSEEN") "12415154135")
(=>? (main "EWNWEEEEWN") "13154532426")
(=>? (main "WNEWEWWWSNW") "145151562421")
(=>? (main "NNEE") "15631")
(=>? (main "EEEEWNWSW") "1364145642")
(=>? (main "SENNWWES") "123142321")
(=>? (main "SWWWSNSNESWW") "1245363635631")
(=>? (main "WESSENSE") "141263231")
(=>? (main "SWNSSESESSS") "124146231562")
(=>? (main "ENS") "1353")
(=>? (main "WNN") "1453")
(=>? (main "SSEENEEEN") "1263124536")
(=>? (main "NWSNNNW") "15414632")
(=>? (main "ESSSSSWW") "132453215")
(=>? (main "ESE") "1326")
(=>? (main "SNWNWWNSSSS") "121456232453")
(=>? (main "SWEESEN") "12423653")
(=>? (main "NEEWNSSWWW") "15323631562")
(=>? (main "WSEW") "14212")
(=>? (main "SWSNNNSNWE") "12464131353")
(=>? (main "ENWEWSEEW") "1351513545")
(=>? (main "WSEWN") "142124")
(=>? (main "EWNEESEWE") "1315321414")
(=>? (main "NESEEN") "1531263")
(=>? (main "WSW") "1426")
(=>? (main "ENEWE") "135656")
)
;;====================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment