Skip to content

Instantly share code, notes, and snippets.

@tkych
Created December 3, 2013 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkych/7768613 to your computer and use it in GitHub Desktop.
Save tkych/7768613 to your computer and use it in GitHub Desktop.
ハニカム歩き ver.2, kei_qさんのエレガントな解答を参考に書き直しました。
;;;; Last modified: 2013-12-03 21:44:27 tkych
;; kei_qさんのエレガントな解答を参考に書き直しました。
;;====================================================================
;; ハニカム歩き ver.2
;;====================================================================
;; - [ハニカム歩き 〜 横へな 2013.5.10](http://nabetani.sakura.ne.jp/hena/ord10haniwa/)
;; - [第10回オフラインリアルタイムどう書くの問題](http://qiita.com/Nabetani/items/55641767510c2f9f235f)
;; - [kei_qさんのエレガントな解答: オフラインどう書く第10回解答例(Haskell)](http://qiita.com/kei_q/items/efe8c0f30ca2cb5eaf79)
;;--------------------------------------------------------------------
(in-package :cl-user)
(defpackage :honeycomb (:use :cl))
(in-package :honeycomb)
;;--------------------------------------------------------------------
;; Main
;;--------------------------------------------------------------------
(defparameter *honeycomb* #(" "
" TUVW "
" kHIJX "
" jSBCKY "
" iRGADLZ "
" hQFEMa "
" gPONb "
" fedc "
" "))
;; 01
;; 5A2
;; 43
(defparameter *dirs* #((-1 0) (-1 1) (0 1) (1 0) (1 -1) (0 -1)))
(defparameter *sentinel* #\Space)
(defun goto (pos step)
(let ((next-pos (mapcar #'+ pos (svref *dirs* step))))
(destructuring-bind (row col) next-pos
(let ((next-state (char (svref *honeycomb* row) col)))
(values next-state next-pos)))))
(defun main (input)
(with-output-to-string (s)
(princ #\A s)
(loop :for step :across input
:with pos := '(4 4)
:with state := #\A
:do (multiple-value-bind
(next-state next-pos) (goto pos (digit-char-p step))
(if (char= next-state *sentinel*)
(princ #\! s)
(progn
(princ next-state s)
(setf state next-state
pos next-pos)))))))
;;--------------------------------------------------------------------
;; Tests
;;--------------------------------------------------------------------
(defun =>? (got expected)
(assert (string= got expected)))
(progn
(=>? (main "135004") "ACDABHS")
(=>? (main "1") "AC")
(=>? (main "33333120") "AENc!!b!M")
(=>? (main "0") "AB")
(=>? (main "2") "AD")
(=>? (main "3") "AE")
(=>? (main "4") "AF")
(=>? (main "5") "AG")
(=>? (main "4532120") "AFQPOEMD")
(=>? (main "051455") "ABSHSj!")
(=>? (main "23334551") "ADMb!cdeO")
(=>? (main "22033251") "ADLKLa!ML")
(=>? (main "50511302122") "AGSjkTHTU!VW")
(=>? (main "000051") "ABHT!!!")
(=>? (main "1310105") "ACDKJW!V")
(=>? (main "50002103140") "AGSk!HU!IVIU")
(=>? (main "3112045") "AEDKYXKC")
(=>? (main "02021245535") "ABCIJW!JIHBS")
(=>? (main "014204") "ABIBCIB")
(=>? (main "255230") "ADAGAEA")
(=>? (main "443501") "AFPefgQ")
(=>? (main "022321") "ABCKLZ!")
(=>? (main "554452") "AGRh!!Q")
(=>? (main "051024") "ABSHTUH")
(=>? (main "524002") "AGAFGSB")
(=>? (main "54002441132") "AGQRjSRhRSGA")
(=>? (main "11010554312") "ACJV!!UTkSHI")
(=>? (main "23405300554") "ADMNEFOFGRi!")
(=>? (main "555353201") "AGRih!gPQG")
(=>? (main "22424105") "ADLMabaLD")
(=>? (main "11340202125") "ACJKDCKJX!!J")
(=>? (main "4524451") "AFQFPf!P")
(=>? (main "44434234050") "AFPf!!e!!Pgh")
(=>? (main "00554040132") "ABHk!j!i!jRG")
(=>? (main "3440403") "AEOePfgf")
(=>? (main "111130") "ACJW!XW")
(=>? (main "21133343125") "ADKXYZ!a!Z!L")
(=>? (main "353511") "AEFOPFA")
(=>? (main "22204115220") "ADLZYLY!KY!X")
(=>? (main "03013541") "ABABICBGB")
(=>? (main "101344") "ACIVJCA")
(=>? (main "2432541") "ADENbNdN")
(=>? (main "45332242015") "AFQPedc!!NME")
(=>? (main "215453") "ADKCAGF")
(=>? (main "45540523454") "AFQh!i!RQg!!")
(=>? (main "42434302545") "AFEOd!!ONOef")
)
;;====================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment