From 325ec93a06337ab8821682406b197b97705b920d Mon Sep 17 00:00:00 2001 From: Jacopo De Simoi Date: Thu, 4 Dec 2025 12:20:30 -0500 Subject: [PATCH] [p4] Part 1 and 2 I am unhappy with the coding style. --- p4/p4.org | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 p4/p4.org diff --git a/p4/p4.org b/p4/p4.org new file mode 100644 index 0000000..9e5b081 --- /dev/null +++ b/p4/p4.org @@ -0,0 +1,76 @@ +#+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