You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
#+title: Solution for p11 |
|
|
|
#+begin_src emacs-lisp |
|
(setq data '(125 17)) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
| 125 | 17 | |
|
|
|
Define the auxiliary functions |
|
#+begin_src emacs-lisp |
|
(defun number-digits (x) |
|
(1+ (floor (log x 10)))) |
|
|
|
(defun split (x) |
|
(let* ((n (number-digits x)) |
|
(div (expt 10 (/ n 2)))) |
|
(--map (funcall it x div) '(/ %)))) |
|
|
|
(defun blink (x) |
|
(cond |
|
((eq x 0) '(1)) |
|
((eq (% (number-digits x) 2) 0) (split x)) |
|
(t (list (* 2024 x))))) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
: blink |
|
|
|
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)) |
|
|
|
(defun regroup (l) |
|
(-map (lambda (el) (cons el (-reduce #'+ (-map #'cdr (--filter (eq el (car it)) l))))) (-distinct (-map #'car l)))) |
|
|
|
(--reduce-from (+ acc (cdr it)) 0 (-last-item (--iterate (regroup (-mapcat (lambda (a) |
|
(--map (cons it (cdr a)) |
|
(blink (car a)))) it)) |
|
freq-data (1+ 75)))) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
: 232454623677743
|
|
|