(defun 8search (strings pattern)
(loop with x = (length (elt strings 0)) and y = (length strings)
with array = (make-array (list y x) :initial-contents strings)
for (dx dy) in '((-1 -1) (-1 0) (-1 1) (0 -1) (0 1) (1 1) (1 0) (1 -1)) do
(loop with m = (1- (length pattern))
for a from (max 0 (- (* dx m))) below (min x (- x (* dx m))) do
(loop for b from (max 0 (- (* dy m))) below (min y (- y (* dy m))) do
(loop for i from a by dx and j from b by dy and c across pattern
unless (char= c (aref array j i)) return nil finally
(format t "(~D, ~D), ~[~;右~:;左~]~[~;下~:;上~]~%" a b dx dy))))))
(8search #("リオウウリウ" "ウオリウオリ" "オリリオリウ" "リリオオウオ")
"ウオリ")
kozima
#4672()
[
Common Lisp
]
Rating1/1=1.00
「何処まで行数を減らせるか」に反応してつい書いてしまったので投稿。ついでに一行 80 文字で。あんまり無理はしてないつもりなんですが、可読性はこちらの方が低いかも……。
(defun 8search (strings pattern) (loop with x = (length (elt strings 0)) and y = (length strings) with array = (make-array (list y x) :initial-contents strings) for (dx dy) in '((-1 -1) (-1 0) (-1 1) (0 -1) (0 1) (1 1) (1 0) (1 -1)) do (loop with m = (1- (length pattern)) for a from (max 0 (- (* dx m))) below (min x (- x (* dx m))) do (loop for b from (max 0 (- (* dy m))) below (min y (- y (* dy m))) do (loop for i from a by dx and j from b by dy and c across pattern unless (char= c (aref array j i)) return nil finally (format t "(~D, ~D), ~[~;右~:;左~]~[~;下~:;上~]~%" a b dx dy)))))) (8search #("リオウウリウ" "ウオリウオリ" "オリリオリウ" "リリオオウオ") "ウオリ")Rating1/1=1.00-0+