Advent of Code '23 - day 11

Table of Contents

An easier one again. For the second part it was very tempting to just insert all those extra columns, but calculating the offset based on the number of previously seen empty columns/rows is more efficient.

Input

Example

...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

Part 1

(defun aoc23/parse-input (string)
   (let* ((lines (string-split string "\n" t))
          (lines (aoc23/expand-galaxy lines))
          (lines (aoc23/rotate-map lines))
          (lines (aoc23/expand-galaxy lines))
          (lines (aoc23/rotate-map lines))
          (lines (aoc23/rotate-map lines))
          (lines (aoc23/rotate-map lines)))
     lines))

(defun aoc23/expand-galaxy (lines)
  (apply 'append (mapcar (lambda (line)
            (if (string-match-p "^\\.+$" line)
                (cons line (cons line nil))
              (cons line nil)))
          lines)))

(defun aoc23/rotate-map (lines &optional forward-rotation)
  (let* ((dw (length lines))
         (dh (length (car lines)))
         (src (string-join lines "\n"))
         (dst '()))
    (dotimes (dy dh)
      (let ((line '()))
        (dotimes (dx dw)
          (let ((sx dy)
                (sy dx))
            (setq line (append line (list (elt (elt lines sy) sx))))
            ))

        (setq dst (append dst (list (apply 'concat (mapcar 'char-to-string line)))))))
    dst))

(defun aoc23/find-galaxies (map)
  (let ((h (length map))
        (w (length (car map)))
        (galaxies '()))
    (dotimes (y h)
      (dotimes (x w)
        (unless (= ?. (elt (elt map y) x))
          (setq galaxies (append galaxies (list (cons x y)))))))
    galaxies))

(defun aoc23/distances (nodes)
  (let ((nodes nodes)
        (sum 0))
    (while nodes
      (let ((n (car nodes)))
        (dolist (m (cdr nodes))
          (setq sum (+ sum (+ (abs (- (car m) (car n)))
                              (abs (- (cdr m) (cdr n))))))
          )
        (setq nodes (cdr nodes))))
    sum))

(aoc23/distances 
 (aoc23/find-galaxies
  (aoc23/parse-input input)))

Part 2

'(defun aoc23/parse-input (string)
  (string-split string "\n" t))

(defun aoc23/expand-galaxy (lines)
  (apply 'append (mapcar (lambda (line)
                           (if (string-match-p "^\\.+$" line)
                               (cons line (cons line nil))
                             (cons line nil)))
                         lines)))

(defun aoc23/get-expansions (lines)
  (let ((columns '())
        (rows '())
        (map lines))
    (dotimes (i (length map))
      (when (string-match-p "^\\.+$" (elt map i))
        (setq rows (append rows (list i)))))
    (setq map (aoc23/rotate-map map))
    (dotimes (i (length map))
      (when (string-match-p "^\\.+$" (elt map i))
        (setq columns (append columns (list i)))))
    (list columns rows)))





(defun aoc23/find-galaxies (map)
  (let ((h (length map))
        (w (length (car map)))
        (galaxies '()))
    (dotimes (y h)
      (dotimes (x w)
        (unless (= ?. (elt (elt map y) x))
          (setq galaxies (append galaxies (list (cons x y)))))))
    galaxies))

(defun aoc23/getoffset (p exps)
  (+ p (* (length (seq-filter (lambda (q)
                                (< q p))
                              exps))
          (1- 1000000))))

(defun aoc23/distances (nodes map)
  (let* ((nodes nodes)
         (sum 0)
         (expa (aoc23/get-expansions map))
         (colexps (car expa))
         (rowexps (cadr expa)))
    (while nodes
      (let* ((n (car nodes))
             (n (cons (aoc23/getoffset (car n) colexps)
                      (aoc23/getoffset (cdr n) rowexps))))
        (dolist (m (cdr nodes))
          (let ((m (cons (aoc23/getoffset (car m) colexps)
                         (aoc23/getoffset (cdr m) rowexps))))

            (setq sum (+ sum (+ (abs (- (car m) (car n)))
                                (abs (- (cdr m) (cdr n))))))
            ))
        (setq nodes (cdr nodes))))
    sum))

(let ((map (aoc23/parse-input input)))
  (aoc23/distances 
   (aoc23/find-galaxies
    map)
   map))