[p7] cached-recursive approach

master
Jacopo De Simoi 6 months ago
parent dde6910392
commit afe6f6d253
  1. 27
      p7/p7.org

@ -68,3 +68,30 @@ the multiplicities of each beam
#+RESULTS: #+RESULTS:
: 1537373473728 : 1537373473728
now try part2 with a recursive approach. This should look better.
Of course it will blow in my face if I did not cache the results
(took 1.1s on Pixel 7)
#+begin_src emacs-lisp
(setq max-lisp-eval-depth 10000000) ; burn baby burn
(setq cache nil)
(defun cache-result (fun pos)
(let ((c (assoc pos cache)))
(if c (cdr c)
(let ((res (funcall fun pos)))
(push (cons pos res) cache)
res ))))
(advice-add 'timelines-at :around 'cache-result)
(defun timelines-at (pos)
(if (not (position-valid-p pos)) 1
(if (not (eq ?^ (advent/char-at pos data-char)))
(timelines-at (advent/neighbour pos '(0 1)))
(-sum (--map (timelines-at (advent/neighbour pos it))
'((1 0) (-1 0)))))))
(setq tachyons (car (advent/coordinates-of ?S data-char)))
(timelines-at tachyons)
#+end_src
#+RESULTS:
: 1537373473728

Loading…
Cancel
Save