breadth-first

main
Jacopo De Simoi 11 months ago
parent 9978ef9367
commit 57eb0fbcf9
  1. 39
      p16/p16.org

@ -4,7 +4,7 @@ Load map
#+begin_src emacs-lisp :results none
(require 'dash)
(with-temp-buffer
(insert-file-contents "input")
(insert-file-contents "input-test")
(goto-char (point-min))
(replace-regexp "^\\(#.*#\\)$" "\"\\1\"")
(goto-char (point-min))
@ -131,6 +131,7 @@ In the version below, we record dead-ends and partial solutions
(new-score (-min-by #'>nil (-map (lambda (newdir)
(explore (neighbour-maybe p dir newdir) newdir dir
(+ score (if (equal newdir dir) 1 1000))
(-distinct (cons p past))))
acceptable-dirs))))
(when (not (eq new-score 'loop)) (push (cons (list p dir) (subtract-nil new-score score)) book))
@ -141,6 +142,40 @@ In the version below, we record dead-ends and partial solutions
#+end_src
#+RESULTS:
: 11048
: 276920
The above does not work for some reasons I do not fully understand.
Let me try with a different approach. Now we start from a point and a
direction; we do one round of explorations and end up with a list of
starting points, directions and accumulated scores.
#+begin_src emacs-lisp
(setq start-vector (list start '(1 0) 0)) ; starting point, direction and score
(defun forward-dirs (dir)
(cons dir (--filter (= (dot it dir) 0) '((0 1) (0 -1) (-1 0) (1 0)))))
(defun explore (start-vect &optional stop)
(let* ((p (car start-vect))
(dir (cadr start-vect))
(score (caddr start-vect))
(acceptable-dirs (--filter (not (eq (thing-at (neighbour p it)) 'wall)) (forward-dirs dir))))
(if (and (> (length acceptable-dirs) 1) stop) (list start-vect)
(push p book)
(-mapcat (lambda (newdir) (explore (list (neighbour p newdir)
newdir
(+ score (if (equal newdir dir) 1 1000)))
t ))
acceptable-dirs))))
(setq book nil)
(explore start-vector)
#+end_src
#+RESULTS:
| (1 11) | (0 -1) | 1001 |
(print-map)

Loading…
Cancel
Save