You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

2.0 KiB

A collection of helper functions for Advent of Code

Input manipulation

  (require 'dash)
  (defun advent/replace-multiple-regex-buffer (regex-list)
    "Takes REGEX-LIST as an alist ( regex . replacement )
  and applies each substitiution to the current buffer"
    (save-excursion
      (-each regex-list (lambda (x)
                          (goto-char (point-min))
                          (replace-regexp (car x) (cdr x))))))

Maze processing

  (defun advent/split-string-into-char-list (str)
    "Split the string into a list of chars"
    (--map (string-to-char it) (split-string str "\\|.+" t)))
    (defun advent/char-at (p l)
      "return the char at position P in the 2D list l"
  (nth (car p) (nth (cadr p) l)))
  (defun advent/coordinates-of (e li)
    "This takes a 2D list (a list of lists) of chars LI and an element E
  and returns a list of positions in LI that contain the element E.
  "
    (-map #'cadr
          (--filter (eq (car it) e)
                    (-mapcat #'identity
                             (-map-indexed (lambda (y l)
                                             (-map-indexed (lambda (x el) (list el (list x y))) l))
                                           li)))))

2D geometry auxiliary functions

Here a vector is just a list with two components; the "dot" function is a bit of an overkill tbh

(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)))))