From c0d8f3ef3c8fdc6a5797a305e6f7a099bd0b61fd Mon Sep 17 00:00:00 2001 From: Jacopo De Simoi Date: Thu, 10 Apr 2025 07:46:23 -0400 Subject: [PATCH] add some explanations --- p10/p10.org | 25 ++++++++++++++----------- p11/p11.org | 10 +++++----- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/p10/p10.org b/p10/p10.org index 42b5191..e846529 100644 --- a/p10/p10.org +++ b/p10/p10.org @@ -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) diff --git a/p11/p11.org b/p11/p11.org index f51448e..aed21e5 100644 --- a/p11/p11.org +++ b/p11/p11.org @@ -1,12 +1,11 @@ #+title: Solution for p11 #+begin_src emacs-lisp - (setq data '(0 27 5409930 828979 4471 3 68524 170)) -; (setq data '(125 17)) + (setq data '(125 17)) #+end_src #+RESULTS: -| 0 | 27 | 5409930 | 828979 | 4471 | 3 | 68524 | 170 | +| 125 | 17 | Define the auxiliary functions #+begin_src emacs-lisp @@ -28,14 +27,15 @@ Define the auxiliary functions #+RESULTS: : blink -And now compute +And now compute. For the first step a simple implementation suffices #+begin_src emacs-lisp (length (-last-item (--iterate (-mapcat #'blink it) data (1+ 25)))) #+end_src #+RESULTS: : 194482 - + +Otherwise, we need to be smarter. We group Peebles in piles with the same engraving and blink only once for each group #+begin_src emacs-lisp (setq freq-data (-frequencies data))