#+title: Solution to p4 Yay, a maze. Cool. No, not cool. #+begin_src emacs-lisp :results none (with-temp-buffer (insert-file-contents "input") (advent/replace-multiple-regex-buffer '(("^\\(.*\\)$" . "\"\\1\" "))) (goto-char (point-min)) (insert "(setq data '(") (goto-char (point-max)) (insert "))") (eval-buffer)) (setq data-char (-map #'advent/split-string-into-char-list data) width (length (car data-char)) height (length data-char)) #+end_src This is for part 1; easy #+begin_src emacs-lisp (defun position-valid-p (pos) (and (and (>= (car pos) 0) (< (car pos) width)) (and (>= (cadr pos) 0) (< (cadr pos) height)))) (defun roll-at-p (pos data-char) (when (position-valid-p pos) (eq ?@ (advent/char-at pos data-char)))) (setq adjacent '((-1 -1) (-1 0) (-1 1) (0 1) (0 -1) (1 -1) (1 0) (1 1))) (defun rolls-adjacent (pos data-char) (length (-non-nil (--map (roll-at-p it data-char) (--map (advent/neighbour pos it) adjacent))))) (length (--filter (< it 4) (--map (rolls-adjacent it data-char) (advent/coordinates-of ?@ data-char)))) #+end_src #+RESULTS: : 1464 Now for part two, we create a new data-char at each removal step I am not particularly happy style-wise, but that is it for today #+begin_src emacs-lisp (defun tensor (a b) (-map (lambda (x) (--map (list it x) a)) b)) (setq initial-roll-number (length (advent/coordinates-of ?@ data-char)) current-roll-number (1+ initial-roll-number)) (while (> current-roll-number (setq current-roll-number (length (advent/coordinates-of ?@ data-char)))) (setq data-char (-map (lambda (row) (--map (if (and (= ?@ (advent/char-at it data-char)) (<= 4 (rolls-adjacent it data-char))) ?@ ?.) row)) (tensor (-iterate #'1+ 0 width) (-iterate #'1+ 0 height))))) (- initial-roll-number current-roll-number) #+end_src #+RESULTS: : 8409