diff --git a/p12/p12.org b/p12/p12.org index 2a481cc..c4c30c0 100644 --- a/p12/p12.org +++ b/p12/p12.org @@ -49,41 +49,64 @@ and split into a list of list of chars #+end_src collect all plants type and return a list of list of positions +#+begin_src emacs-lisp :results none + (setq plants (-distinct (-mapcat #'identity data-chars))) + + (setq groups (-map (lambda (pl) + (-map #'cadr + (--filter (eq (car it) pl) + (-mapcat #'identity + (-map-indexed (lambda (y l) (-map-indexed (lambda (x el) (list el (list x y))) l)) data-chars))))) + plants)) +#+end_src + now we need to split each group into regions -ACC is a list of regions. The function -#+begin_src emacs-lisp :results none - (defun agglomerate (acc el) - (let ((nbhs (acceptable-neighbours el))) - (cons (-mapcat #'identity (cons (list el) (--filter (-intersection it nbhs) acc))) - (--filter (not (-intersection it nbhs)) acc)) + ACC is a list of regions. The function + #+begin_src emacs-lisp :results none + (defun agglomerate (acc el) + (let ((nbhs (acceptable-neighbours el))) + (cons (-mapcat #'identity (cons (list el) (--filter (-intersection it nbhs) acc))) + (--filter (not (-intersection it nbhs)) acc)) + ) ) - ) - (setq regions (--mapcat (-reduce-from #'agglomerate nil it) groups)) + (setq regions (--mapcat (-reduce-from #'agglomerate nil it) groups)) #+end_src #+begin_src emacs-lisp - (defun count-fence (el) - (let ((i (plant el)) - (pos el)) - (length (--filter (not (eq i it)) (-map #'plant (neighbours pos)))))) + (defun count-fence (el) + (let ((i (plant el)) + (pos el)) + (length (--filter (not (eq i it)) (-map #'plant (neighbours pos)))))) + + (defun vertex-p (el dir) + (let* ((x (car el)) + (y (cadr el)) + (a (car dir)) + (b (cadr dir)) + (opposite (list (+ x a) (+ y b))) + (adjh (list (+ x a) y)) + (adjv (list x (+ y b)))) + (or (and (not (eq (plant el) (plant adjh))) + (not (eq (plant el) (plant adjv)))) + (and (eq (plant el) (plant adjh)) + (eq (plant el) (plant adjv)) + (not (eq (plant el) (plant opposite)))))) + ) + (defun count-vertices (el) + (length (-non-nil (--map (vertex-p el it) '((1 1) (1 -1) (-1 1) (-1 -1))))) + ) - (defun count-vertices (el) - - ) (-reduce #'+ (--map (* (car it) (cdr it)) (-zip-pair (-map #'length regions) (-map (lambda (region) (-reduce #'+ (-map #'count-fence region))) regions)))) -#+end_src - -#+RESULTS: -: 1451030 - -#+begin_src emacs-lisp :no-results - (setq areas (-frequencies (-mapcat #'identity data-chars))) + (-reduce #'+ (--map (* (car it) (cdr it)) + (-zip-pair (-map #'length regions) + (-map (lambda (region) (-reduce #'+ (-map #'count-vertices region))) regions)))) #+end_src +#+RESULTS: +: 859494 -(plant ' (0 9))