You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.8 KiB
33 lines
1.8 KiB
(with-temp-buffer |
|
;; enter original char matrix |
|
(insert-file-contents "input") |
|
(let* ((rows (-map 'string-to-list (split-string (buffer-substring-no-properties (point-min) (point-max))))) |
|
(rshifted-rows (-map-indexed (lambda (index el) (-concat (-repeat index ?-) el)) rows)) |
|
(lshifted-rows (reverse (-map-indexed (lambda (index el) (-concat (-repeat index ?-) el)) (reverse rows)))) |
|
(len (length rows)) |
|
(r-matches (with-temp-buffer |
|
(insert (mapconcat 'identity |
|
(--map (apply 'string it) (apply '-zip-lists-fill ?- rshifted-rows)) "\n")) |
|
(goto-char (point-min)) |
|
(-concat (-unfold (lambda (x) (if (search-forward "MAS" nil t) (list (point)) (goto-char (point-min)) nil)) nil) |
|
(-unfold (lambda (x) (when (search-forward "SAM" nil t) (list (point)))) nil)))) |
|
(l-matches (with-temp-buffer |
|
(insert (mapconcat 'identity |
|
(--map (apply 'string it) (apply '-zip-lists-fill ?- lshifted-rows)) "\n")) |
|
(goto-char (point-min)) |
|
(-concat (-unfold (lambda (x) (if (search-forward "MAS" nil t) (list (point)) (goto-char (point-min)) nil)) nil) |
|
(-unfold (lambda (x) (when (search-forward "SAM" nil t) (list (point)))) nil)))) |
|
(lr-matches (--map (rpoint-to-lpoint it len) r-matches))) |
|
(length (-intersection lr-matches l-matches)))) |
|
|
|
; how to map the l-shifted coordinates to the r-shifted coordinates? |
|
; The column is the same; row - column |
|
|
|
(defun rpoint-to-lpoint (p len) |
|
; the point is at the end of the match |
|
(let* ((pointa (- p 2)); position of the middle A |
|
(x (- (mod pointa (+ 1 len)) 1)) ; column index |
|
(yr (/ pointa (+ 1 len))) ; row index |
|
(yl (- (+ len yr ) x x 1)) ; transformed row index |
|
(lpointa (+ 1 x (* (+ len 1) yl)))) ; new position of the middle A |
|
(+ 2 lpointa)))
|
|
|