|
|
|
|
@ -4,7 +4,7 @@ Load the file into a list of lines |
|
|
|
|
#+begin_src emacs-lisp |
|
|
|
|
(require 'dash) |
|
|
|
|
(with-temp-buffer |
|
|
|
|
(insert-file-contents "input-test") |
|
|
|
|
(insert-file-contents "input") |
|
|
|
|
(goto-char (point-min)) |
|
|
|
|
(replace-regexp "^" "\"") |
|
|
|
|
(goto-char (point-min)) |
|
|
|
|
@ -27,22 +27,63 @@ and split into a list of list of chars |
|
|
|
|
#+end_src |
|
|
|
|
|
|
|
|
|
#+RESULTS: |
|
|
|
|
: 10 |
|
|
|
|
: 140 |
|
|
|
|
|
|
|
|
|
#+begin_src emacs-lisp |
|
|
|
|
(defun acceptable-p (p) |
|
|
|
|
(let ((x (car p)) |
|
|
|
|
(y (cadr p))) |
|
|
|
|
(and (>= x 0) (>= y 0) (< x width) (< y height)) )) |
|
|
|
|
#+begin_src emacs-lisp :results silent |
|
|
|
|
(defun acceptable-p (p) |
|
|
|
|
(let ((x (car p)) |
|
|
|
|
(y (cadr p))) |
|
|
|
|
(and (>= x 0) (>= y 0) (< x width) (< y height)) )) |
|
|
|
|
|
|
|
|
|
(defun neighbours (p) |
|
|
|
|
(let ((x (car p)) |
|
|
|
|
(y (cadr p))) |
|
|
|
|
(-map (lambda (q) (list (+ x (car q)) (+ y (cadr q)))) '((+1 0) (-1 0) (0 1) (0 -1))))) |
|
|
|
|
(defun neighbours (p) |
|
|
|
|
(let ((x (car p)) |
|
|
|
|
(y (cadr p))) |
|
|
|
|
(-map (lambda (q) (list (+ x (car q)) (+ y (cadr q)))) '((+1 0) (-1 0) (0 1) (0 -1))))) |
|
|
|
|
|
|
|
|
|
(defun acceptable-neighbours (p) |
|
|
|
|
(-filter #'acceptable-p (neighbours p))) |
|
|
|
|
(-filter #'acceptable-p (neighbours p))) |
|
|
|
|
|
|
|
|
|
(defun plant (p) |
|
|
|
|
(if (not (acceptable-p p)) ?. |
|
|
|
|
(nth (car p) (nth (cadr p) data-chars)))) |
|
|
|
|
#+end_src |
|
|
|
|
|
|
|
|
|
collect all plants type and return a list of list of positions |
|
|
|
|
|
|
|
|
|
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)) |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
(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 height (p) |
|
|
|
|
(nth (car p) (nth (cadr p) data-heights))) |
|
|
|
|
(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))) |
|
|
|
|
#+end_src |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(plant ' (0 9)) |
|
|
|
|
|