main
Jacopo De Simoi 10 months ago
parent b9d94ed2ed
commit f36a916fcc
  1. 48
      p18/p18.org

@ -16,7 +16,7 @@ First parse with regex to make into a list
height 71
start-pos '(0 0)
end-pos (list (- width 1) (- height 1)))
(setq data (-take 1024 data))
(setq full-data data)
#+end_src
now declare the usual helper functions
#+begin_src emacs-lisp :results none
@ -42,26 +42,46 @@ First parse with regex to make into a list
(not (corrupted-p p))
(not (visited-p p))))
(setq distances '(((0 0) . 0)))
#+end_src
#+begin_src emacs-lisp
(setq new distances)
(defun step (new-distances)
(let ((radius (1+ (cdar new-distances)))
(sphere (-map #'car new-distances)))
(--map (cons it radius) (-filter #'admissible-p (-distinct (-mapcat (lambda (p) (--map (neighbour p it) ' ((1 0) (0 1) (-1 0) (0 -1))))
sphere))))))
(defun step (new-distances)
(let ((radius (1+ (cdar new-distances)))
(sphere (-map #'car new-distances)))
(--map (cons it radius) (-filter #'admissible-p (-distinct (-mapcat (lambda (p) (--map (neighbour p it) ' ((1 0) (0 1) (-1 0) (0 -1))))
sphere)))))
)
(while (setq nn (step new))
(setq distances (-concat nn distances)
new nn))
(defun out-p (n)
(setq data (-take n full-data)
distances '(((0 0) . 0)))
(setq new distances)
(while (setq nn (step new))
(setq distances (-concat nn distances)
new nn))
(assoc end-pos distances))
(nth (- (bisect 'out-p 1024 (length full-data)) 1) full-data)
(cdr (assoc end-pos distances))
#+end_src
#+RESULTS:
: 330
| 10 | 38 |
#+begin_src emacs-lisp
(defun bisect (pred begin end)
(if (eq (1+ begin) end) end
(let ((mid (/ (+ begin end) 2)))
(if (funcall pred mid) (bisect pred mid end)
(bisect pred begin mid))))
)
(defun ddd (x)
(< x 138))
(bisect 'ddd 10 1000)
#+end_src
#+RESULTS:
: 138

Loading…
Cancel
Save