[p10] chunkize

Machine 71 blows
master
Jacopo De Simoi 6 months ago
parent e051b0c63a
commit 907579fc12
  1. 257
      p10/p10.org

@ -4,7 +4,7 @@ This problem is pretty hard. I have not yet completely understood the
linear algebra behind it. linear algebra behind it.
#+begin_src emacs-lisp :results none #+begin_src emacs-lisp :results none
(with-temp-buffer (with-temp-buffer
(insert-file-contents "input") (insert-file-contents "input-test")
(advent/replace-multiple-regex-buffer (advent/replace-multiple-regex-buffer
'(("," . " ") '(("," . " ")
("^" . "(") ("^" . "(")
@ -198,11 +198,9 @@ These are some auxiliary functions to create and deal with matrices
| 44 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | | 44 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
the inductive hypothesis is that the function returns the solution with fewer steps to finish the game
but this is a bit silly, since it's really the matrix that governs the behaviour
#+begin_src emacs-lisp #+begin_src emacs-lisp
(setq solutions-tree nil) (setq solutions-tree nil)
(solve-well-ordered (-distinct (matrix-buttons (fix-machine (nth 4 machines))))) (solve-well-ordered (-distinct (matrix-buttons (fix-machine (nth 1 machines)))))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
@ -288,97 +286,120 @@ but this is a bit silly, since it's really the matrix that governs the behaviour
| 9 | 10 | 15 | 5 | 9 | 11 | 24 | 19 | 1 | 0 | | 9 | 10 | 15 | 5 | 9 | 11 | 24 | 19 | 1 | 0 |
| 7 | 11 | 17 | 2 | 9 | 12 | 25 | 19 | 0 | 0 | | 7 | 11 | 17 | 2 | 9 | 12 | 25 | 19 | 0 | 0 |
This is it.
It will take forever.
Hopefully, it won't blow the stack.
(memoize 'solve-well-ordered-recursively) #+begin_src emacs-lisp
(setq cache nil) (setq machine-no 0
min-presses nil)
(--each machines
(push (-min (-map '-sum (let ((matrix (matrix-buttons (fix-machine it))))
(solve-well-ordered-chunks matrix))))
min-presses)
(setq machine-no (1+ machine-no)))
(-sum min-presses)
#+end_src
#+RESULTS:
: 33
#+begin_src emacs-lisp #+begin_src emacs-lisp
(let ((matrix (matrix-buttons (fix-machine (nth 4 machines))))) (let ((matrix (matrix-buttons (fix-machine (nth 1 machines)))))
(solve-well-ordered-chunks matrix)) (solve-well-ordered-chunks matrix))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
| 7 | 11 | 17 | 14 | 9 | 0 | 25 | 7 | 0 | 12 | | 25 | 1 | 6 | 16 | 10 | 0 | 21 | 12 | 14 | 14 |
| 9 | 10 | 15 | 16 | 9 | 0 | 24 | 8 | 1 | 11 | | 24 | 1 | 7 | 16 | 10 | 1 | 20 | 12 | 14 | 14 |
| 7 | 11 | 17 | 13 | 9 | 1 | 25 | 8 | 0 | 11 | | 23 | 1 | 8 | 16 | 10 | 2 | 19 | 12 | 14 | 14 |
| 11 | 9 | 13 | 18 | 9 | 0 | 23 | 9 | 2 | 10 | | 22 | 1 | 9 | 16 | 10 | 3 | 18 | 12 | 14 | 14 |
| 9 | 10 | 15 | 15 | 9 | 1 | 24 | 9 | 1 | 10 | | 21 | 1 | 10 | 16 | 10 | 4 | 17 | 12 | 14 | 14 |
| 7 | 11 | 17 | 12 | 9 | 2 | 25 | 9 | 0 | 10 | | 20 | 1 | 11 | 16 | 10 | 5 | 16 | 12 | 14 | 14 |
| 13 | 8 | 11 | 20 | 9 | 0 | 22 | 10 | 3 | 9 | | 19 | 1 | 12 | 16 | 10 | 6 | 15 | 12 | 14 | 14 |
| 11 | 9 | 13 | 17 | 9 | 1 | 23 | 10 | 2 | 9 | | 18 | 1 | 13 | 16 | 10 | 7 | 14 | 12 | 14 | 14 |
| 9 | 10 | 15 | 14 | 9 | 2 | 24 | 10 | 1 | 9 | | 17 | 1 | 14 | 16 | 10 | 8 | 13 | 12 | 14 | 14 |
| 7 | 11 | 17 | 11 | 9 | 3 | 25 | 10 | 0 | 9 | | 16 | 1 | 15 | 16 | 10 | 9 | 12 | 12 | 14 | 14 |
| 15 | 7 | 9 | 22 | 9 | 0 | 21 | 11 | 4 | 8 | | 15 | 1 | 16 | 16 | 10 | 10 | 11 | 12 | 14 | 14 |
| 13 | 8 | 11 | 19 | 9 | 1 | 22 | 11 | 3 | 8 | | 14 | 1 | 17 | 16 | 10 | 11 | 10 | 12 | 14 | 14 |
| 11 | 9 | 13 | 16 | 9 | 2 | 23 | 11 | 2 | 8 | | 13 | 1 | 18 | 16 | 10 | 12 | 9 | 12 | 14 | 14 |
| 9 | 10 | 15 | 13 | 9 | 3 | 24 | 11 | 1 | 8 | | 12 | 1 | 19 | 16 | 10 | 13 | 8 | 12 | 14 | 14 |
| 7 | 11 | 17 | 10 | 9 | 4 | 25 | 11 | 0 | 8 | | 11 | 1 | 20 | 16 | 10 | 14 | 7 | 12 | 14 | 14 |
| 17 | 6 | 7 | 24 | 9 | 0 | 20 | 12 | 5 | 7 | | 10 | 1 | 21 | 16 | 10 | 15 | 6 | 12 | 14 | 14 |
| 15 | 7 | 9 | 21 | 9 | 1 | 21 | 12 | 4 | 7 | | 9 | 1 | 22 | 16 | 10 | 16 | 5 | 12 | 14 | 14 |
| 13 | 8 | 11 | 18 | 9 | 2 | 22 | 12 | 3 | 7 | | 8 | 1 | 23 | 16 | 10 | 17 | 4 | 12 | 14 | 14 |
| 11 | 9 | 13 | 15 | 9 | 3 | 23 | 12 | 2 | 7 | | 7 | 1 | 24 | 16 | 10 | 18 | 3 | 12 | 14 | 14 |
| 9 | 10 | 15 | 12 | 9 | 4 | 24 | 12 | 1 | 7 | | 6 | 1 | 25 | 16 | 10 | 19 | 2 | 12 | 14 | 14 |
| 7 | 11 | 17 | 9 | 9 | 5 | 25 | 12 | 0 | 7 | | 5 | 1 | 26 | 16 | 10 | 20 | 1 | 12 | 14 | 14 |
| 19 | 5 | 5 | 26 | 9 | 0 | 19 | 13 | 6 | 6 | | 4 | 1 | 27 | 16 | 10 | 21 | 0 | 12 | 14 | 14 |
| 17 | 6 | 7 | 23 | 9 | 1 | 20 | 13 | 5 | 6 | | 23 | 3 | 4 | 16 | 11 | 0 | 18 | 16 | 15 | 12 |
| 15 | 7 | 9 | 20 | 9 | 2 | 21 | 13 | 4 | 6 | | 22 | 3 | 5 | 16 | 11 | 1 | 17 | 16 | 15 | 12 |
| 13 | 8 | 11 | 17 | 9 | 3 | 22 | 13 | 3 | 6 | | 21 | 3 | 6 | 16 | 11 | 2 | 16 | 16 | 15 | 12 |
| 11 | 9 | 13 | 14 | 9 | 4 | 23 | 13 | 2 | 6 | | 20 | 3 | 7 | 16 | 11 | 3 | 15 | 16 | 15 | 12 |
| 9 | 10 | 15 | 11 | 9 | 5 | 24 | 13 | 1 | 6 | | 19 | 3 | 8 | 16 | 11 | 4 | 14 | 16 | 15 | 12 |
| 7 | 11 | 17 | 8 | 9 | 6 | 25 | 13 | 0 | 6 | | 18 | 3 | 9 | 16 | 11 | 5 | 13 | 16 | 15 | 12 |
| 21 | 4 | 3 | 28 | 9 | 0 | 18 | 14 | 7 | 5 | | 17 | 3 | 10 | 16 | 11 | 6 | 12 | 16 | 15 | 12 |
| 19 | 5 | 5 | 25 | 9 | 1 | 19 | 14 | 6 | 5 | | 16 | 3 | 11 | 16 | 11 | 7 | 11 | 16 | 15 | 12 |
| 17 | 6 | 7 | 22 | 9 | 2 | 20 | 14 | 5 | 5 | | 15 | 3 | 12 | 16 | 11 | 8 | 10 | 16 | 15 | 12 |
| 15 | 7 | 9 | 19 | 9 | 3 | 21 | 14 | 4 | 5 | | 14 | 3 | 13 | 16 | 11 | 9 | 9 | 16 | 15 | 12 |
| 13 | 8 | 11 | 16 | 9 | 4 | 22 | 14 | 3 | 5 | | 13 | 3 | 14 | 16 | 11 | 10 | 8 | 16 | 15 | 12 |
| 11 | 9 | 13 | 13 | 9 | 5 | 23 | 14 | 2 | 5 | | 12 | 3 | 15 | 16 | 11 | 11 | 7 | 16 | 15 | 12 |
| 9 | 10 | 15 | 10 | 9 | 6 | 24 | 14 | 1 | 5 | | 11 | 3 | 16 | 16 | 11 | 12 | 6 | 16 | 15 | 12 |
| 7 | 11 | 17 | 7 | 9 | 7 | 25 | 14 | 0 | 5 | | 10 | 3 | 17 | 16 | 11 | 13 | 5 | 16 | 15 | 12 |
| 23 | 3 | 1 | 30 | 9 | 0 | 17 | 15 | 8 | 4 | | 9 | 3 | 18 | 16 | 11 | 14 | 4 | 16 | 15 | 12 |
| 21 | 4 | 3 | 27 | 9 | 1 | 18 | 15 | 7 | 4 | | 8 | 3 | 19 | 16 | 11 | 15 | 3 | 16 | 15 | 12 |
| 19 | 5 | 5 | 24 | 9 | 2 | 19 | 15 | 6 | 4 | | 7 | 3 | 20 | 16 | 11 | 16 | 2 | 16 | 15 | 12 |
| 17 | 6 | 7 | 21 | 9 | 3 | 20 | 15 | 5 | 4 | | 6 | 3 | 21 | 16 | 11 | 17 | 1 | 16 | 15 | 12 |
| 15 | 7 | 9 | 18 | 9 | 4 | 21 | 15 | 4 | 4 | | 5 | 3 | 22 | 16 | 11 | 18 | 0 | 16 | 15 | 12 |
| 13 | 8 | 11 | 15 | 9 | 5 | 22 | 15 | 3 | 4 | | 21 | 5 | 2 | 16 | 12 | 0 | 15 | 20 | 16 | 10 |
| 11 | 9 | 13 | 12 | 9 | 6 | 23 | 15 | 2 | 4 | | 20 | 5 | 3 | 16 | 12 | 1 | 14 | 20 | 16 | 10 |
| 9 | 10 | 15 | 9 | 9 | 7 | 24 | 15 | 1 | 4 | | 19 | 5 | 4 | 16 | 12 | 2 | 13 | 20 | 16 | 10 |
| 7 | 11 | 17 | 6 | 9 | 8 | 25 | 15 | 0 | 4 | | 18 | 5 | 5 | 16 | 12 | 3 | 12 | 20 | 16 | 10 |
| 23 | 3 | 1 | 29 | 9 | 1 | 17 | 16 | 8 | 3 | | 17 | 5 | 6 | 16 | 12 | 4 | 11 | 20 | 16 | 10 |
| 21 | 4 | 3 | 26 | 9 | 2 | 18 | 16 | 7 | 3 | | 16 | 5 | 7 | 16 | 12 | 5 | 10 | 20 | 16 | 10 |
| 19 | 5 | 5 | 23 | 9 | 3 | 19 | 16 | 6 | 3 | | 15 | 5 | 8 | 16 | 12 | 6 | 9 | 20 | 16 | 10 |
| 17 | 6 | 7 | 20 | 9 | 4 | 20 | 16 | 5 | 3 | | 14 | 5 | 9 | 16 | 12 | 7 | 8 | 20 | 16 | 10 |
| 15 | 7 | 9 | 17 | 9 | 5 | 21 | 16 | 4 | 3 | | 13 | 5 | 10 | 16 | 12 | 8 | 7 | 20 | 16 | 10 |
| 13 | 8 | 11 | 14 | 9 | 6 | 22 | 16 | 3 | 3 | | 12 | 5 | 11 | 16 | 12 | 9 | 6 | 20 | 16 | 10 |
| 11 | 9 | 13 | 11 | 9 | 7 | 23 | 16 | 2 | 3 | | 11 | 5 | 12 | 16 | 12 | 10 | 5 | 20 | 16 | 10 |
| 9 | 10 | 15 | 8 | 9 | 8 | 24 | 16 | 1 | 3 | | 10 | 5 | 13 | 16 | 12 | 11 | 4 | 20 | 16 | 10 |
| 7 | 11 | 17 | 5 | 9 | 9 | 25 | 16 | 0 | 3 | | 9 | 5 | 14 | 16 | 12 | 12 | 3 | 20 | 16 | 10 |
| 23 | 3 | 1 | 28 | 9 | 2 | 17 | 17 | 8 | 2 | | 8 | 5 | 15 | 16 | 12 | 13 | 2 | 20 | 16 | 10 |
| 21 | 4 | 3 | 25 | 9 | 3 | 18 | 17 | 7 | 2 | | 7 | 5 | 16 | 16 | 12 | 14 | 1 | 20 | 16 | 10 |
| 19 | 5 | 5 | 22 | 9 | 4 | 19 | 17 | 6 | 2 | | 6 | 5 | 17 | 16 | 12 | 15 | 0 | 20 | 16 | 10 |
| 17 | 6 | 7 | 19 | 9 | 5 | 20 | 17 | 5 | 2 | | 19 | 7 | 0 | 16 | 13 | 0 | 12 | 24 | 17 | 8 |
| 15 | 7 | 9 | 16 | 9 | 6 | 21 | 17 | 4 | 2 | | 18 | 7 | 1 | 16 | 13 | 1 | 11 | 24 | 17 | 8 |
| 13 | 8 | 11 | 13 | 9 | 7 | 22 | 17 | 3 | 2 | | 17 | 7 | 2 | 16 | 13 | 2 | 10 | 24 | 17 | 8 |
| 11 | 9 | 13 | 10 | 9 | 8 | 23 | 17 | 2 | 2 | | 16 | 7 | 3 | 16 | 13 | 3 | 9 | 24 | 17 | 8 |
| 9 | 10 | 15 | 7 | 9 | 9 | 24 | 17 | 1 | 2 | | 15 | 7 | 4 | 16 | 13 | 4 | 8 | 24 | 17 | 8 |
| 7 | 11 | 17 | 4 | 9 | 10 | 25 | 17 | 0 | 2 | | 14 | 7 | 5 | 16 | 13 | 5 | 7 | 24 | 17 | 8 |
| 23 | 3 | 1 | 27 | 9 | 3 | 17 | 18 | 8 | 1 | | 13 | 7 | 6 | 16 | 13 | 6 | 6 | 24 | 17 | 8 |
| 21 | 4 | 3 | 24 | 9 | 4 | 18 | 18 | 7 | 1 | | 12 | 7 | 7 | 16 | 13 | 7 | 5 | 24 | 17 | 8 |
| 19 | 5 | 5 | 21 | 9 | 5 | 19 | 18 | 6 | 1 | | 11 | 7 | 8 | 16 | 13 | 8 | 4 | 24 | 17 | 8 |
| 17 | 6 | 7 | 18 | 9 | 6 | 20 | 18 | 5 | 1 | | 10 | 7 | 9 | 16 | 13 | 9 | 3 | 24 | 17 | 8 |
| 15 | 7 | 9 | 15 | 9 | 7 | 21 | 18 | 4 | 1 | | 9 | 7 | 10 | 16 | 13 | 10 | 2 | 24 | 17 | 8 |
| 13 | 8 | 11 | 12 | 9 | 8 | 22 | 18 | 3 | 1 | | 8 | 7 | 11 | 16 | 13 | 11 | 1 | 24 | 17 | 8 |
| 11 | 9 | 13 | 9 | 9 | 9 | 23 | 18 | 2 | 1 | | 7 | 7 | 12 | 16 | 13 | 12 | 0 | 24 | 17 | 8 |
| 9 | 10 | 15 | 6 | 9 | 10 | 24 | 18 | 1 | 1 | | 15 | 9 | 0 | 16 | 14 | 2 | 7 | 28 | 18 | 6 |
| 7 | 11 | 17 | 3 | 9 | 11 | 25 | 18 | 0 | 1 | | 14 | 9 | 1 | 16 | 14 | 3 | 6 | 28 | 18 | 6 |
| 23 | 3 | 1 | 26 | 9 | 4 | 17 | 19 | 8 | 0 | | 13 | 9 | 2 | 16 | 14 | 4 | 5 | 28 | 18 | 6 |
| 21 | 4 | 3 | 23 | 9 | 5 | 18 | 19 | 7 | 0 | | 12 | 9 | 3 | 16 | 14 | 5 | 4 | 28 | 18 | 6 |
| 19 | 5 | 5 | 20 | 9 | 6 | 19 | 19 | 6 | 0 | | 11 | 9 | 4 | 16 | 14 | 6 | 3 | 28 | 18 | 6 |
| 17 | 6 | 7 | 17 | 9 | 7 | 20 | 19 | 5 | 0 | | 10 | 9 | 5 | 16 | 14 | 7 | 2 | 28 | 18 | 6 |
| 15 | 7 | 9 | 14 | 9 | 8 | 21 | 19 | 4 | 0 | | 9 | 9 | 6 | 16 | 14 | 8 | 1 | 28 | 18 | 6 |
| 13 | 8 | 11 | 11 | 9 | 9 | 22 | 19 | 3 | 0 | | 8 | 9 | 7 | 16 | 14 | 9 | 0 | 28 | 18 | 6 |
| 11 | 9 | 13 | 8 | 9 | 10 | 23 | 19 | 2 | 0 | | 11 | 11 | 0 | 16 | 15 | 4 | 2 | 32 | 19 | 4 |
| 9 | 10 | 15 | 5 | 9 | 11 | 24 | 19 | 1 | 0 | | 10 | 11 | 1 | 16 | 15 | 5 | 1 | 32 | 19 | 4 |
| 7 | 11 | 17 | 2 | 9 | 12 | 25 | 19 | 0 | 0 | | 9 | 11 | 2 | 16 | 15 | 6 | 0 | 32 | 19 | 4 |
arst
#+RESULTS:
: 114
#+begin_src emacs-lisp #+begin_src emacs-lisp
; (-min (-map '-sum (solve-well-ordered (matrix-buttons (fix-machine (cadr machines)))))) ; (-min (-map '-sum (solve-well-ordered (matrix-buttons (fix-machine (cadr machines))))))
@ -482,27 +503,45 @@ This implementation works, but I need to make it recursive, so that I can memoiz
)) ))
soln)) soln))
#+end_src #+end_src
#+RESULTS:
: solve-well-ordered
try to split into chunks try to split into chunks
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun create-chunks (n list)
(let ((result nil))
(while list
(push (-take n list) result)
(setq list (-drop n list)))
result))
(create-chunks 3 '(a b c d e))
(defun solve-well-ordered-chunks (matrix) (defun solve-well-ordered-chunks (matrix)
;; we start from the last row ;; we start from the last row
(let* ((soln-acc nil) (let* ((soln-acc nil)
(number-of-buttons (1- (length (car matrix)))) (number-of-buttons (1- (length (car matrix))))
(soln (list (-repeat number-of-buttons 0))) (soln (list (-repeat number-of-buttons 0)))
(last-used-button number-of-buttons) (last-used-button number-of-buttons)
(current-row (1- (length matrix))) (current-row (1- (length matrix)))
(soln-chunks nil)) (soln-chunks nil))
(while (or (>= current-row 0) soln-chunks) (while (or (>= current-row 0) soln-chunks)
(message (format "%d %d - %d" last-used-button (length soln) (length soln-chunks))) (message (format "%d %d - %d" last-used-button (length soln) (length soln-chunks)))
(when (< current-row 0) (when (< current-row 0)
(let ((chunk (pop soln-chunks))) (let ((chunk (pop soln-chunks)))
(push soln soln-acc) (push soln soln-acc)
(setq current-row (pop chunk) (setq current-row (pop chunk)
last-used-button (pop chunk) last-used-button (pop chunk)
soln (pop chunk)))) soln (pop chunk))))
;; chunkize here ;; chunkize here
(when (> (length soln 10000)) ) (when (> (length soln) 50000)
(let* ((chunks (create-chunks 8000 soln))
(new-soln (car chunks))
(chunks-to-store (--map (list current-row last-used-button it) (cdr chunks))))
(setq soln new-soln
soln-chunks (append chunks-to-store soln-chunks))))
(let* ((row (nth current-row matrix)) (let* ((row (nth current-row matrix))
(a (car row)) (a (car row))
(rrow (cdr row)) (rrow (cdr row))
@ -522,9 +561,9 @@ try to split into chunks
;;otherwise, we create a number of solutions ;;otherwise, we create a number of solutions
(let* ((button (-last-item possible-indices))) (let* ((button (-last-item possible-indices)))
(setq soln (--mapcat (let* ((max-soln (-min (-non-nil (-map (lambda (row) (setq soln (--mapcat (let* ((max-soln (-min (-non-nil (-map (lambda (row)
(when (= 1 (nth button (cdr row))) (when (= 1 (nth button (cdr row)))
(- (car row) (advent/dot it (cdr row))))) (- (car row) (advent/dot it (cdr row)))))
matrix))))) matrix)))))
(unless (< max-soln 0) (unless (< max-soln 0)
(-map (lambda (candidate) (-replace-at button candidate it)) (-iota (1+ max-soln) max-soln -1)))) (-map (lambda (candidate) (-replace-at button candidate it)) (-iota (1+ max-soln) max-soln -1))))
soln) soln)
@ -536,7 +575,7 @@ try to split into chunks
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: minimal-pushes : solve-well-ordered-chunks
Try to do it recursively Try to do it recursively
#+begin_src emacs-lisp #+begin_src emacs-lisp
@ -570,14 +609,14 @@ Try to do it recursively
matrix))))) matrix)))))
(when (>= max-soln 0) (-iota (1+ max-soln))))))) (when (>= max-soln 0) (-iota (1+ max-soln)))))))
(-non-nil (-mapcat (lambda (a) (-non-nil (-mapcat (lambda (a)
(let* ((new-car (--map (if (= 1 (-last-item it)) (- (car it) a) (car it)) matrix))) (let* ((new-car (--map (if (= 1 (-last-item it)) (- (car it) a) (car it)) matrix)))
(when (--every (>= it 0) new-car) (when (--every (>= it 0) new-car)
(let* ((new-matrix (-map '-butlast (--map-indexed (cons (nth it-index new-car) (cdr it)) matrix))) ; remove one column (let* ((new-matrix (-map '-butlast (--map-indexed (cons (nth it-index new-car) (cdr it)) matrix))) ; remove one column
(next (solve-well-ordered-recursively new-matrix))) (next (solve-well-ordered-recursively new-matrix)))
(when next (if (listp next) (when next (if (listp next)
(--map (cons a it) next) (--map (cons a it) next)
(list (list a)))))))) (list (list a))))))))
possible-solutions))))))) possible-solutions)))))))
(--map (cons 'a it) '((1) (1 2) (1 3))) (--map (cons 'a it) '((1) (1 2) (1 3)))
#+end_src #+end_src

Loading…
Cancel
Save