Edited at

オフラインリアルタイムどう書く第5回の参考問題をArcで解く

More than 5 years have passed since last update.

気がついたら明日だったので、Arcで解いてみた。

問題はこちら

http://qiita.com/items/0ddde0164a745cd09c34


realtime_doukaku5.arc

(= opposite '(2 3 0 1))

(= pattern '((2 3 0 1) (1 0 3 2) (3 2 1 0)))
(= adjacent (list [if (< -1 (- _ 3)) (- _ 3)] ; up
[if (< (mod _ 3) 2) (++ _)] ; right
[if (< (+ _ 3) 9) (+ _ 3)] ; down
[if (< 0 (mod _ 3)) (-- _)])) ; left

(def run (lis idx side)
(let next ((pattern (lis idx)) side)
(cons idx
(aif ((adjacent next) idx)
(run lis it (opposite next))))))

(def to-alphabet (l) (upcase (string (map [coerce (+ _ 97) 'char] l))))
(def to-numlis (s) (read (+ "(" (string (intersperse #\ (coerce s 'cons))) ")")))
(def solve (s) (to-alphabet (run (to-numlis s) 1 0)))


以上が算出のコード

普通に書いたつもりだがなんかショートコードっぽくなるな…


realtime_doukaku_5_result.arc

(solve "101221102") ;-> "BEDGHIFEH"                                                                                                                                               

(solve "000000000") ;-> "BEH"
(solve "111111111") ;-> "BCF"
(solve "222222222") ;-> "BAD"
(solve "000211112") ;-> "BEFIHEDGH"
(solve "221011102") ;-> "BADGHIFEBCF"
(solve "201100112") ;-> "BEHIFCBADEF"
(solve "000111222") ;-> "BEFIH"
(solve "012012012") ;-> "BC"
(solve "201120111") ;-> "BEDABCFI"
(solve "220111122") ;-> "BADEHGD"
(solve "221011022") ;-> "BADG"
(solve "111000112") ;-> "BCFIHEBA"
(solve "001211001") ;-> "BEFI"
(solve "111222012") ;-> "BCFEHIF"
(solve "220111211") ;-> "BADEHI"
(solve "211212212") ;-> "BCFEBAD"
(solve "002112210") ;-> "BEFC"
(solve "001010221") ;-> "BEF"
(solve "100211002") ;-> "BEFIHG"
(solve "201212121") ;-> "BEFCBAD"

以上が検証コード

うまく動いている。