temp for part 2

main
Jacopo De Simoi 6 months ago
parent ac514c1cbc
commit 6dc925d565
  1. 17
      p21/p21.org

@ -87,6 +87,8 @@ go from p to q without getting out of the boundary
#+RESULTS:
: path-allowed-p
The following of course only works for part 1; with part 2 the stack
would blow in my face since we would have too many options to check.
#+begin_src emacs-lisp
(defun directions-for-string (s keypad-data)
(--map (connecting-paths it keypad-data)
@ -99,6 +101,7 @@ go from p to q without getting out of the boundary
(-reduce (lambda (a b)
(if (> (length a) (length b)) b a))
l))
(defun shortest-path (s)
(let* ((first-directional-keypad
(directions-for-string s numeric-keypad-data))
@ -147,3 +150,17 @@ go from p to q without getting out of the boundary
#+RESULTS:
: 222670
The new idea would be to run things bottom-down. For a given pair of
chars at a given depth, what is the optimal way to put things
together? It should be true that the optimal way to assemble things is
the shortest concatenation of the optimal way at the previous depth
So we start from all possible combinations of keypad pairs
#+begin_src emacs-lisp
(setq direction-pairs (let ((directions (advent/split-string-into-char-list "<>^vA")))
(-mapcat (lambda (x) (--map (cons x it) directions)) directions)))
(--map (connecting-paths (char-pairs-to-coordinate-pairs it dir-keypad-data) dir-keypad-data) direction-pairs)
#+end_src

Loading…
Cancel
Save