From f36a916fcc22f1e2a80179b0ad89e6b3fec268f6 Mon Sep 17 00:00:00 2001 From: Jacopo De Simoi Date: Tue, 13 May 2025 18:26:24 -0400 Subject: [PATCH] p18 done --- p18/p18.org | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/p18/p18.org b/p18/p18.org index a53ff88..d970aa9 100644 --- a/p18/p18.org +++ b/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