add some explanations

main
Jacopo De Simoi 11 months ago
parent ace56c526d
commit c0d8f3ef3c
  1. 25
      p10/p10.org
  2. 10
      p11/p11.org

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

@ -1,12 +1,11 @@
#+title: Solution for p11 #+title: Solution for p11
#+begin_src emacs-lisp #+begin_src emacs-lisp
(setq data '(0 27 5409930 828979 4471 3 68524 170)) (setq data '(125 17))
; (setq data '(125 17))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
| 0 | 27 | 5409930 | 828979 | 4471 | 3 | 68524 | 170 | | 125 | 17 |
Define the auxiliary functions Define the auxiliary functions
#+begin_src emacs-lisp #+begin_src emacs-lisp
@ -28,14 +27,15 @@ Define the auxiliary functions
#+RESULTS: #+RESULTS:
: blink : blink
And now compute And now compute. For the first step a simple implementation suffices
#+begin_src emacs-lisp #+begin_src emacs-lisp
(length (-last-item (--iterate (-mapcat #'blink it) data (1+ 25)))) (length (-last-item (--iterate (-mapcat #'blink it) data (1+ 25))))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 194482 : 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 #+begin_src emacs-lisp
(setq freq-data (-frequencies data)) (setq freq-data (-frequencies data))

Loading…
Cancel
Save