Better indent

main
Jacopo De Simoi 11 months ago
parent 7ec2d09720
commit a6a5175329
  1. 83
      p16/p16.org

@ -146,49 +146,48 @@ score. Hence at the end we look (recursively) in the discard pile and
put the ones that end on the best paths with the same score in the put the ones that end on the best paths with the same score in the
best pile. best pile.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(setq start-vector (list start '(1 0) 0)) ; starting point, direction and score (setq start-vector (list start '(1 0) 0)) ; starting point, direction and score
(defun next-dirs (dir) (defun next-dirs (dir)
(--filter (>= (dot it dir) 0) '((0 1) (0 -1) (-1 0) (1 0)))) (--filter (>= (dot it dir) 0) '((0 1) (0 -1) (-1 0) (1 0))))
(defun acceptable-p (p dir) (defun acceptable-p (p dir)
(not (eq (thing-at (neighbour p dir)) 'wall))) (not (eq (thing-at (neighbour p dir)) 'wall)))
(defun sort-vecs (a b) (defun sort-vecs (a b)
(< (caddar a) (caddar b))) (< (caddar a) (caddar b)))
(defun delta-score (olddir newdir) (defun delta-score (olddir newdir)
(if (equal olddir newdir) 1 1001)) (if (equal olddir newdir) 1 1001))
(defun explore (start-vects) (defun explore (start-vects)
(let ((start-vect (car start-vects))) (let ((start-vect (car start-vects)))
(if (eq (thing-at (car start-vect)) 'end) (progn (push start-vects scores) nil) (if (eq (thing-at (car start-vect)) 'end) (progn (push start-vects scores) nil)
(let* ((p (car start-vect)) (let* ((p (car start-vect))
(dir (cadr start-vect)) (dir (cadr start-vect))
(score (caddr start-vect)) (score (caddr start-vect))
(acceptable-next-dirs (--filter (acceptable-p p it) (next-dirs dir)))) (acceptable-next-dirs (--filter (acceptable-p p it) (next-dirs dir))))
(if (-contains-p book (list p dir)) (progn (push start-vects discard) nil) (if (-contains-p book (list p dir)) (progn (push start-vects discard) nil)
(push (list p dir) book) (push (list p dir) book)
(--map (cons (list (neighbour p it) it (+ score (delta-score it dir))) start-vects) acceptable-next-dirs)))))) (--map (cons (list (neighbour p it) it (+ score (delta-score it dir))) start-vects) acceptable-next-dirs))))))
(setq book nil (setq book nil
scores nil scores nil
discard nil) discard nil)
(setq start-list (list (list start-vector))) (setq start-list (list (list start-vector)))
(while (setq start-list (-concat (explore (car start-list)) (cdr start-list))) (while (setq start-list (-concat (explore (car start-list)) (cdr start-list)))
(setq start-list (-sort #'sort-vecs start-list))) (setq start-list (-sort #'sort-vecs start-list)))
(let ((best (list (cadr scores))) (let ((best (list (cadr scores)))
(repechage t)) (repechage t))
(while repechage (while repechage
(setq repechage (-filter (lambda (candidate) (setq repechage (-filter (lambda (candidate)
(--any-p (-contains-p it (car candidate)) best)) (--any-p (-contains-p it (car candidate)) best))
discard)) discard))
(setq best (-union repechage best) (setq best (-union repechage best)
discard (-difference discard repechage))) discard (-difference discard repechage)))
(length (-distinct (--mapcat (-map #'car it) best)))) (length (-distinct (--mapcat (-map #'car it) best))))
#+end_src #+end_src
#+results: #+results:
: 456 : 456

Loading…
Cancel
Save