[lib] Move some helpers to the library

main
Jacopo De Simoi 6 months ago
parent 936cff2c32
commit 651f171bae
  1. 19
      lib/lib-advent.org
  2. 26
      p20/p20.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

@ -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

Loading…
Cancel
Save