LoginSignup
0
0

More than 5 years have passed since last update.

オフラインリアルタイムどう書く第四回の参考問題 解答(Arc)

Last updated at Posted at 2012-10-17

ちょっと面白そうな勉強会 ( http://atnd.org/events/32979 ) を見つけたので、
試しに直近の参考問題をArcで解いてみた。
問題はこちら http://qiita.com/items/9c514267214d3917edf2

realtime_doukaku4.arc
(= x 5 n (- (* x x) 1))

(def get-tbl bans
  (let o (map [rem nil `(,(if (< (mod _ x) (- x 1)) (+ _ 1)) ;right                                                                                                                                                 
                         ,(if (< (/ _ x) (- x 1)) (+ _ x)) ; bottom                                                                                                                                                 
                         ,(if (> (mod _ x) 0) (- _ 1)) ;left                                                                                                                                                        
                         ,(if (>= (/ _ x) 1) (- _ x)))] ; about                                                                                                                                                     
              (range 0 n))
    (aif (car bans)
         (each x it (= (o (x 0)) (rem (x 1) (o (x 0)))
                       (o (x 1)) (rem (x 0) (o (x 1)))))) o))

(def count (i h tbl)
  (if (is i n) 1
      (apply + (mappend [if (no (mem _ h)) `(,(count _ (cons i h) tbl))]
                        (tbl i)))))

(def solve (xs)
  (let ban-syms (read (+ "(" xs ")"))
    (let bans (map [map [- (coerce _ 'int) 97]
                        (coerce (coerce _ 'string) 'cons)] ban-syms)
      (count 0 nil (get-tbl bans)))))

以上が算出のコード
(回答を出してる中では最短に近いのではないか。自然に書いてこうなる。)

realtime_doukaku_4_result.arc
(solve "") ;; -> 8512                                                                                                                                                                                               
(solve "af") ;; -> 4256                                                                                                                                                                                             
(solve "xy") ;; -> 4256                                                                                                                                                                                             
(solve "pq qr rs st di in ns sx") ;; -> 184                                                                                                                                                                         
(solve "af pq qr rs st di in ns sx") ;; -> 92                                                                                                                                                                       
(solve "bg ch di ij no st") ;; -> 185                                                                                                                                                                               
(solve "bc af ch di no kp mr ns ot pu rs") ;; -> 16                                                                                                                                                                 
(solve "ab af") ;; -> 0                                                                                                                                                                                             
(solve "ty xy") ;; -> 0                                                                                                                                                                                             
(solve "bg ch ej gh lm lq mr ot rs sx") ;; -> 11                                                                                                                                                                    
(solve "ty ch hi mn kp mr rs sx") ;; -> 18                                                                                                                                                                          
(solve "xy ch hi mn kp mr rs sx") ;; -> 32                                                                                                                                                                          
(solve "ch hi mn kp mr rs sx") ;; -> 50                                                                                                                                                                             
(solve "ab cd uv wx") ;; -> 621                                                                                                                                                                                     
(solve "gh mn st lq qr") ;; -> 685                                                                                                                                                                                  
(solve "fg gl lm mr rs") ;; -> 171 

以上が検証コード
うまく動いている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0