From 651f171baefafa40d38c25eb22e4c306f4962179 Mon Sep 17 00:00:00 2001 From: Jacopo De Simoi Date: Thu, 11 Sep 2025 12:27:39 -0400 Subject: [PATCH] [lib] Move some helpers to the library --- lib/lib-advent.org | 19 +++++++++++++++++++ p20/p20.org | 26 ++++++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/lib-advent.org b/lib/lib-advent.org index e4cd225..3ee51f1 100644 --- a/lib/lib-advent.org +++ b/lib/lib-advent.org @@ -32,3 +32,22 @@ (-map-indexed (lambda (x el) (list el (list x y))) l)) li))))) #+end_src +* 2D geometry auxiliary functions + Here a vector is just a list with two components; the "dot" + function is a bit of an overkill tbh + + #+begin_src emacs-lisp :results none + (defun advent/dot (a b) + "Return the dot product of the two vectors" + (-reduce #'+ (-map (lambda (x) (* (car x) (cdr x))) (-zip-pair a b)))) + + (defun advent/neighbour (p dir) + "Returns the neighbour to P in the direction DIR" + (list (+ (car p) (car dir)) + (+ (cadr p) (cadr dir)))) + + (defun advent/taxicab-distance (p q) + "Returns the taxicab distance from P to Q" + (+ (abs (- (car p) (car q))) + (abs (- (cadr p) (cadr q))))) +#+end_src diff --git a/p20/p20.org b/p20/p20.org index 968c2d3..635635c 100644 --- a/p20/p20.org +++ b/p20/p20.org @@ -34,23 +34,9 @@ Load map (car (advent/coordinates-of ?S data-chars))) #+end_src -Define some aux functions +Let the stack burn #+begin_src emacs-lisp :results none (setq max-lisp-eval-depth 1000000) ; burn baby burn - - (defun dot (a b) - "Return the dot product of the two vectors" - (-reduce #'+ (-map (lambda (x) (* (car x) (cdr x))) (-zip-pair a b)))) - - (defun neighbour (p dir) - "Returns the neighbour to P in the direction DIR" - (list (+ (car p) (car dir)) - (+ (cadr p) (cadr dir)))) - - (defun taxicab-distance (p q) - "Returns the taxicab distance from P to Q" - (+ (abs (- (car p) (car q))) - (abs (- (cadr p) (cadr q))))) #+end_src First, we explore the maze and record the path from START to END @@ -59,10 +45,10 @@ First, we explore the maze and record the path from START to END "Explore the maze starting at position P and in the direction DIR. Returns a list of positions traversed END to START" (if (eq (thing-at p) 'end) (cons p past) - (let* ((forward-dirs (--filter (>= (dot it dir) 0) + (let* ((forward-dirs (--filter (>= (advent/dot it dir) 0) '((0 1) (0 -1) (-1 0) (1 0)))) - (new-dir (car (--filter (not (eq (thing-at (neighbour p it)) 'wall)) forward-dirs)))) - (explore (neighbour p new-dir) new-dir (cons p past))))) + (new-dir (car (--filter (not (eq (thing-at (advent/neighbour p it)) 'wall)) forward-dirs)))) + (explore (advent/neighbour p new-dir) new-dir (cons p past))))) (setq distance-alist (-map-indexed (lambda (x y) (cons y x)) (explore (starting-pos) '(0 0)))) @@ -77,7 +63,7 @@ Now look for gaps: (--filter (>= it 100) (--map (- it 2) (-flatten (-non-nil (--map (-non-nil - (-map (lambda (dir) (subtract-nil (cdr (assoc (neighbour (car it) dir) distance-alist)) (cdr it))) + (-map (lambda (dir) (subtract-nil (cdr (assoc (advent/neighbour (car it) dir) distance-alist)) (cdr it))) '((0 2) (0 -2) (2 0) (-2 0)))) distance-alist)))))) @@ -95,7 +81,7 @@ then compute the time saved by using the cheat (-flatten (-map (lambda (x) (--map (- (cdar it) (cdr x) (cdr it)) (--filter (<= (cdr it) 20) - (--map (cons it (taxicab-distance (car it) (car x))) distance-alist)))) + (--map (cons it (advent/taxicab-distance (car it) (car x))) distance-alist)))) distance-alist)))) #+end_src