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.1 KiB

Solution to p4

Yay, a maze. Cool. No, not cool.

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

This is for part 1; easy

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

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