|
|
|
|
@ -18,7 +18,7 @@ Load the file into a list of lines |
|
|
|
|
|
|
|
|
|
#+RESULTS: |
|
|
|
|
|
|
|
|
|
and split into a list of list of chars |
|
|
|
|
and split into a list of list of heights, calculating height and width |
|
|
|
|
#+begin_src emacs-lisp |
|
|
|
|
(setq data-heights (-map (lambda (str) (--map (- (string-to-char it) ?0) (split-string str "\\|.+" t))) |
|
|
|
|
data) |
|
|
|
|
@ -29,16 +29,18 @@ and split into a list of list of chars |
|
|
|
|
#+RESULTS: |
|
|
|
|
: 41 |
|
|
|
|
|
|
|
|
|
here we define some helper functions |
|
|
|
|
#+begin_src emacs-lisp |
|
|
|
|
(defun acceptable-p (p) |
|
|
|
|
(let ((x (car p)) |
|
|
|
|
(y (cadr p))) |
|
|
|
|
(and (>= x 0) (>= y 0) (< x width) (< y height)) )) |
|
|
|
|
(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))) |
|
|
|
|
(-filter #'acceptable-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))) |
|
|
|
|
(-filter #'acceptable-p |
|
|
|
|
(-map (lambda (q) (list (+ x (car q)) (+ y (cadr q)))) '((+1 0) (-1 0) (0 1) (0 -1)))))) |
|
|
|
|
|
|
|
|
|
(defun height (p) |
|
|
|
|
(nth (car p) (nth (cadr p) data-heights))) |
|
|
|
|
@ -47,8 +49,7 @@ and split into a list of list of chars |
|
|
|
|
#+RESULTS: |
|
|
|
|
: height |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this yields the coordinates of the 0s |
|
|
|
|
This yields the coordinates of the trailheads |
|
|
|
|
#+begin_src emacs-lisp |
|
|
|
|
(setq trail-heads (-map #'cadr |
|
|
|
|
(--filter (eq (car it) 0) |
|
|
|
|
@ -253,6 +254,8 @@ this yields the coordinates of the 0s |
|
|
|
|
| 22 | 40 | |
|
|
|
|
| 23 | 40 | |
|
|
|
|
| 24 | 40 | |
|
|
|
|
|
|
|
|
|
these follow the trails to the top, calculating the two properties of each trailhead |
|
|
|
|
#+begin_src emacs-lisp |
|
|
|
|
(defun tails (p h) |
|
|
|
|
(when (eq (height p) h) |
|
|
|
|
|