[p10] this should do

master
Jacopo De Simoi 3 months ago
parent 907579fc12
commit 5d62adcd23
  1. 255
      p10/p10.org

@ -4,7 +4,7 @@ This problem is pretty hard. I have not yet completely understood the
linear algebra behind it.
#+begin_src emacs-lisp :results none
(with-temp-buffer
(insert-file-contents "input-test")
(insert-file-contents "input")
(advent/replace-multiple-regex-buffer
'(("," . " ")
("^" . "(")
@ -183,109 +183,140 @@ These are some auxiliary functions to create and deal with matrices
(matrix-buttons (cadr machines))
(-distinct
(matrix-buttons (fix-machine (nth 4 machines)))
(matrix-buttons (fix-machine (nth 71 machines)))
)
#+end_src
#+RESULTS:
| 77 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 |
| 83 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| 59 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| 61 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 1 |
| 84 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 50 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
| 21 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 44 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| 242 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 1 |
| 116 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
| 282 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 1 |
| 295 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 305 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 |
| 110 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
| 116 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
| 76 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
| 83 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
| 78 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
#+begin_src emacs-lisp
(setq solutions-tree nil)
(solve-well-ordered (-distinct (matrix-buttons (fix-machine (nth 1 machines)))))
(solve-well-ordered-chunks (-distinct (matrix-buttons (fix-machine (nth 2 machines))))
)
#+end_src
#+RESULTS:
| 7 | 11 | 17 | 14 | 9 | 0 | 25 | 7 | 0 | 12 |
| 20 | 9 | 17 | 0 | 4 | 0 | 20 | 19 |
#+begin_src emacs-lisp
(setq rainbow (-annotate (lambda (n) (--map (mod n it) '(2 3 5 7))) (-iota (* 2 3 5 7))))
(let* ((machine (nth 4 machines))
(matrix (-distinct (matrix-buttons (fix-machine machine))))
(solmod (--map (solve-well-ordered-chunks-mod matrix it) '(2 3 5 7)))
(solns nil)
(numcand (apply '* (-map 'length solmod)))
(count 0))
;; Oh Programming Gods, have mercy of me for I have sinned
(-each (car solmod)
(lambda (a)
(-each (cadr solmod)
(lambda (b)
(-each (caddr solmod)
(lambda (c)
(-each (cadddr solmod)
(lambda (d)
(let ((cand (--map (cdr (assoc it rainbow)) (-zip-lists a b c d))))
(message (format "Verifying %d / %d - found %d" (setq count (1+ count)) numcand (length solns)))
(when (test-soln matrix cand) (push cand solns)))))))))))
solns)
#+end_src
#+RESULTS:
| 17 | 6 | 7 | 22 | 9 | 2 | 20 | 14 | 5 | 5 |
| 9 | 10 | 15 | 16 | 9 | 0 | 24 | 8 | 1 | 11 |
| 7 | 11 | 17 | 13 | 9 | 1 | 25 | 8 | 0 | 11 |
| 11 | 9 | 13 | 18 | 9 | 0 | 23 | 9 | 2 | 10 |
| 9 | 10 | 15 | 15 | 9 | 1 | 24 | 9 | 1 | 10 |
| 7 | 11 | 17 | 12 | 9 | 2 | 25 | 9 | 0 | 10 |
| 13 | 8 | 11 | 20 | 9 | 0 | 22 | 10 | 3 | 9 |
| 11 | 9 | 13 | 17 | 9 | 1 | 23 | 10 | 2 | 9 |
| 9 | 10 | 15 | 14 | 9 | 2 | 24 | 10 | 1 | 9 |
| 7 | 11 | 17 | 11 | 9 | 3 | 25 | 10 | 0 | 9 |
| 15 | 7 | 9 | 22 | 9 | 0 | 21 | 11 | 4 | 8 |
| 13 | 8 | 11 | 19 | 9 | 1 | 22 | 11 | 3 | 8 |
| 11 | 9 | 13 | 16 | 9 | 2 | 23 | 11 | 2 | 8 |
| 9 | 10 | 15 | 13 | 9 | 3 | 24 | 11 | 1 | 8 |
| 7 | 11 | 17 | 10 | 9 | 4 | 25 | 11 | 0 | 8 |
| 17 | 6 | 7 | 24 | 9 | 0 | 20 | 12 | 5 | 7 |
| 15 | 7 | 9 | 21 | 9 | 1 | 21 | 12 | 4 | 7 |
| 13 | 8 | 11 | 18 | 9 | 2 | 22 | 12 | 3 | 7 |
| 11 | 9 | 13 | 15 | 9 | 3 | 23 | 12 | 2 | 7 |
| 9 | 10 | 15 | 12 | 9 | 4 | 24 | 12 | 1 | 7 |
| 7 | 11 | 17 | 9 | 9 | 5 | 25 | 12 | 0 | 7 |
| 19 | 5 | 5 | 26 | 9 | 0 | 19 | 13 | 6 | 6 |
| 17 | 6 | 7 | 23 | 9 | 1 | 20 | 13 | 5 | 6 |
| 15 | 7 | 9 | 20 | 9 | 2 | 21 | 13 | 4 | 6 |
| 13 | 8 | 11 | 17 | 9 | 3 | 22 | 13 | 3 | 6 |
| 11 | 9 | 13 | 14 | 9 | 4 | 23 | 13 | 2 | 6 |
| 9 | 10 | 15 | 11 | 9 | 5 | 24 | 13 | 1 | 6 |
| 7 | 11 | 17 | 8 | 9 | 6 | 25 | 13 | 0 | 6 |
| 21 | 4 | 3 | 28 | 9 | 0 | 18 | 14 | 7 | 5 |
| 19 | 5 | 5 | 25 | 9 | 1 | 19 | 14 | 6 | 5 |
| 17 | 6 | 7 | 22 | 9 | 2 | 20 | 14 | 5 | 5 |
| 15 | 7 | 9 | 19 | 9 | 3 | 21 | 14 | 4 | 5 |
| 9 | 10 | 15 | 10 | 9 | 6 | 24 | 14 | 1 | 5 |
| 13 | 8 | 11 | 16 | 9 | 4 | 22 | 14 | 3 | 5 |
| 17 | 6 | 7 | 24 | 9 | 0 | 20 | 12 | 5 | 7 |
| 17 | 6 | 7 | 18 | 9 | 6 | 20 | 18 | 5 | 1 |
| 9 | 10 | 15 | 12 | 9 | 4 | 24 | 12 | 1 | 7 |
| 21 | 4 | 3 | 24 | 9 | 4 | 18 | 18 | 7 | 1 |
| 9 | 10 | 15 | 6 | 9 | 10 | 24 | 18 | 1 | 1 |
| 13 | 8 | 11 | 18 | 9 | 2 | 22 | 12 | 3 | 7 |
| 13 | 8 | 11 | 12 | 9 | 8 | 22 | 18 | 3 | 1 |
| 17 | 6 | 7 | 20 | 9 | 4 | 20 | 16 | 5 | 3 |
| 9 | 10 | 15 | 14 | 9 | 2 | 24 | 10 | 1 | 9 |
| 21 | 4 | 3 | 26 | 9 | 2 | 18 | 16 | 7 | 3 |
| 9 | 10 | 15 | 8 | 9 | 8 | 24 | 16 | 1 | 3 |
| 13 | 8 | 11 | 20 | 9 | 0 | 22 | 10 | 3 | 9 |
| 13 | 8 | 11 | 14 | 9 | 6 | 22 | 16 | 3 | 3 |
| 11 | 9 | 13 | 13 | 9 | 5 | 23 | 14 | 2 | 5 |
| 9 | 10 | 15 | 10 | 9 | 6 | 24 | 14 | 1 | 5 |
| 15 | 7 | 9 | 19 | 9 | 3 | 21 | 14 | 4 | 5 |
| 7 | 11 | 17 | 13 | 9 | 1 | 25 | 8 | 0 | 11 |
| 19 | 5 | 5 | 25 | 9 | 1 | 19 | 14 | 6 | 5 |
| 7 | 11 | 17 | 7 | 9 | 7 | 25 | 14 | 0 | 5 |
| 23 | 3 | 1 | 30 | 9 | 0 | 17 | 15 | 8 | 4 |
| 21 | 4 | 3 | 27 | 9 | 1 | 18 | 15 | 7 | 4 |
| 19 | 5 | 5 | 24 | 9 | 2 | 19 | 15 | 6 | 4 |
| 17 | 6 | 7 | 21 | 9 | 3 | 20 | 15 | 5 | 4 |
| 15 | 7 | 9 | 18 | 9 | 4 | 21 | 15 | 4 | 4 |
| 13 | 8 | 11 | 15 | 9 | 5 | 22 | 15 | 3 | 4 |
| 11 | 9 | 13 | 12 | 9 | 6 | 23 | 15 | 2 | 4 |
| 9 | 10 | 15 | 9 | 9 | 7 | 24 | 15 | 1 | 4 |
| 7 | 11 | 17 | 6 | 9 | 8 | 25 | 15 | 0 | 4 |
| 11 | 9 | 13 | 15 | 9 | 3 | 23 | 12 | 2 | 7 |
| 23 | 3 | 1 | 27 | 9 | 3 | 17 | 18 | 8 | 1 |
| 11 | 9 | 13 | 9 | 9 | 9 | 23 | 18 | 2 | 1 |
| 15 | 7 | 9 | 21 | 9 | 1 | 21 | 12 | 4 | 7 |
| 15 | 7 | 9 | 15 | 9 | 7 | 21 | 18 | 4 | 1 |
| 7 | 11 | 17 | 9 | 9 | 5 | 25 | 12 | 0 | 7 |
| 19 | 5 | 5 | 21 | 9 | 5 | 19 | 18 | 6 | 1 |
| 7 | 11 | 17 | 3 | 9 | 11 | 25 | 18 | 0 | 1 |
| 11 | 9 | 13 | 17 | 9 | 1 | 23 | 10 | 2 | 9 |
| 23 | 3 | 1 | 29 | 9 | 1 | 17 | 16 | 8 | 3 |
| 21 | 4 | 3 | 26 | 9 | 2 | 18 | 16 | 7 | 3 |
| 19 | 5 | 5 | 23 | 9 | 3 | 19 | 16 | 6 | 3 |
| 17 | 6 | 7 | 20 | 9 | 4 | 20 | 16 | 5 | 3 |
| 15 | 7 | 9 | 17 | 9 | 5 | 21 | 16 | 4 | 3 |
| 13 | 8 | 11 | 14 | 9 | 6 | 22 | 16 | 3 | 3 |
| 11 | 9 | 13 | 11 | 9 | 7 | 23 | 16 | 2 | 3 |
| 9 | 10 | 15 | 8 | 9 | 8 | 24 | 16 | 1 | 3 |
| 15 | 7 | 9 | 17 | 9 | 5 | 21 | 16 | 4 | 3 |
| 7 | 11 | 17 | 11 | 9 | 3 | 25 | 10 | 0 | 9 |
| 19 | 5 | 5 | 23 | 9 | 3 | 19 | 16 | 6 | 3 |
| 7 | 11 | 17 | 5 | 9 | 9 | 25 | 16 | 0 | 3 |
| 23 | 3 | 1 | 28 | 9 | 2 | 17 | 17 | 8 | 2 |
| 21 | 4 | 3 | 25 | 9 | 3 | 18 | 17 | 7 | 2 |
| 19 | 5 | 5 | 22 | 9 | 4 | 19 | 17 | 6 | 2 |
| 17 | 6 | 7 | 19 | 9 | 5 | 20 | 17 | 5 | 2 |
| 15 | 7 | 9 | 16 | 9 | 6 | 21 | 17 | 4 | 2 |
| 9 | 10 | 15 | 13 | 9 | 3 | 24 | 11 | 1 | 8 |
| 21 | 4 | 3 | 25 | 9 | 3 | 18 | 17 | 7 | 2 |
| 9 | 10 | 15 | 7 | 9 | 9 | 24 | 17 | 1 | 2 |
| 13 | 8 | 11 | 19 | 9 | 1 | 22 | 11 | 3 | 8 |
| 13 | 8 | 11 | 13 | 9 | 7 | 22 | 17 | 3 | 2 |
| 17 | 6 | 7 | 21 | 9 | 3 | 20 | 15 | 5 | 4 |
| 21 | 4 | 3 | 27 | 9 | 1 | 18 | 15 | 7 | 4 |
| 9 | 10 | 15 | 9 | 9 | 7 | 24 | 15 | 1 | 4 |
| 9 | 10 | 15 | 15 | 9 | 1 | 24 | 9 | 1 | 10 |
| 13 | 8 | 11 | 15 | 9 | 5 | 22 | 15 | 3 | 4 |
| 17 | 6 | 7 | 23 | 9 | 1 | 20 | 13 | 5 | 6 |
| 17 | 6 | 7 | 17 | 9 | 7 | 20 | 19 | 5 | 0 |
| 9 | 10 | 15 | 11 | 9 | 5 | 24 | 13 | 1 | 6 |
| 21 | 4 | 3 | 23 | 9 | 5 | 18 | 19 | 7 | 0 |
| 9 | 10 | 15 | 5 | 9 | 11 | 24 | 19 | 1 | 0 |
| 13 | 8 | 11 | 17 | 9 | 3 | 22 | 13 | 3 | 6 |
| 13 | 8 | 11 | 11 | 9 | 9 | 22 | 19 | 3 | 0 |
| 11 | 9 | 13 | 16 | 9 | 2 | 23 | 11 | 2 | 8 |
| 23 | 3 | 1 | 28 | 9 | 2 | 17 | 17 | 8 | 2 |
| 11 | 9 | 13 | 10 | 9 | 8 | 23 | 17 | 2 | 2 |
| 9 | 10 | 15 | 7 | 9 | 9 | 24 | 17 | 1 | 2 |
| 15 | 7 | 9 | 22 | 9 | 0 | 21 | 11 | 4 | 8 |
| 15 | 7 | 9 | 16 | 9 | 6 | 21 | 17 | 4 | 2 |
| 7 | 11 | 17 | 10 | 9 | 4 | 25 | 11 | 0 | 8 |
| 19 | 5 | 5 | 22 | 9 | 4 | 19 | 17 | 6 | 2 |
| 7 | 11 | 17 | 4 | 9 | 10 | 25 | 17 | 0 | 2 |
| 23 | 3 | 1 | 27 | 9 | 3 | 17 | 18 | 8 | 1 |
| 21 | 4 | 3 | 24 | 9 | 4 | 18 | 18 | 7 | 1 |
| 19 | 5 | 5 | 21 | 9 | 5 | 19 | 18 | 6 | 1 |
| 17 | 6 | 7 | 18 | 9 | 6 | 20 | 18 | 5 | 1 |
| 15 | 7 | 9 | 15 | 9 | 7 | 21 | 18 | 4 | 1 |
| 13 | 8 | 11 | 12 | 9 | 8 | 22 | 18 | 3 | 1 |
| 11 | 9 | 13 | 9 | 9 | 9 | 23 | 18 | 2 | 1 |
| 9 | 10 | 15 | 6 | 9 | 10 | 24 | 18 | 1 | 1 |
| 7 | 11 | 17 | 3 | 9 | 11 | 25 | 18 | 0 | 1 |
| 23 | 3 | 1 | 30 | 9 | 0 | 17 | 15 | 8 | 4 |
| 11 | 9 | 13 | 12 | 9 | 6 | 23 | 15 | 2 | 4 |
| 11 | 9 | 13 | 18 | 9 | 0 | 23 | 9 | 2 | 10 |
| 15 | 7 | 9 | 18 | 9 | 4 | 21 | 15 | 4 | 4 |
| 19 | 5 | 5 | 24 | 9 | 2 | 19 | 15 | 6 | 4 |
| 7 | 11 | 17 | 6 | 9 | 8 | 25 | 15 | 0 | 4 |
| 7 | 11 | 17 | 12 | 9 | 2 | 25 | 9 | 0 | 10 |
| 11 | 9 | 13 | 14 | 9 | 4 | 23 | 13 | 2 | 6 |
| 23 | 3 | 1 | 26 | 9 | 4 | 17 | 19 | 8 | 0 |
| 21 | 4 | 3 | 23 | 9 | 5 | 18 | 19 | 7 | 0 |
| 19 | 5 | 5 | 20 | 9 | 6 | 19 | 19 | 6 | 0 |
| 17 | 6 | 7 | 17 | 9 | 7 | 20 | 19 | 5 | 0 |
| 15 | 7 | 9 | 14 | 9 | 8 | 21 | 19 | 4 | 0 |
| 13 | 8 | 11 | 11 | 9 | 9 | 22 | 19 | 3 | 0 |
| 11 | 9 | 13 | 8 | 9 | 10 | 23 | 19 | 2 | 0 |
| 9 | 10 | 15 | 5 | 9 | 11 | 24 | 19 | 1 | 0 |
| 15 | 7 | 9 | 20 | 9 | 2 | 21 | 13 | 4 | 6 |
| 15 | 7 | 9 | 14 | 9 | 8 | 21 | 19 | 4 | 0 |
| 7 | 11 | 17 | 14 | 9 | 0 | 25 | 7 | 0 | 12 |
| 19 | 5 | 5 | 26 | 9 | 0 | 19 | 13 | 6 | 6 |
| 7 | 11 | 17 | 8 | 9 | 6 | 25 | 13 | 0 | 6 |
| 19 | 5 | 5 | 20 | 9 | 6 | 19 | 19 | 6 | 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.
@ -460,7 +491,7 @@ possibility
This implementation works, but I need to make it recursive, so that I can memoize
#+begin_src emacs-lisp
(defun test-soln (matrix soln)
(-map (lambda (row) (advent/dot (cdr row) soln)) matrix))
(equal (-map 'car matrix) (-map (lambda (row) (advent/dot (cdr row) soln)) matrix)))
(defun solve-well-ordered (matrix)
;; we start from the last row
@ -574,6 +605,72 @@ try to split into chunks
(apply #'append soln-acc)))
#+end_src
#+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)
;; we start from the last row
(let* ((soln-acc nil)
(number-of-buttons (1- (length (car matrix))))
(soln (list (-repeat number-of-buttons 0)))
(last-used-button number-of-buttons)
(current-row (1- (length matrix)))
(soln-chunks nil))
(while (or (>= current-row 0) soln-chunks)
(message (format "%d %d - %d" last-used-button (length soln) (length soln-chunks)))
(when (< current-row 0)
(let ((chunk (pop soln-chunks)))
(push soln soln-acc)
(setq current-row (pop chunk)
last-used-button (pop chunk)
soln (pop chunk))))
;; chunkize here
(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))
(a (car row))
(rrow (cdr row))
(i (--find-index (not (zerop it)) (-take last-used-button rrow))))
(if i
(let ((possible-indices (--filter (not (zerop (nth it rrow)))
(-iota (- last-used-button i) i))))
(if (= 1 (length possible-indices)) ;no choices here, easy
(setq soln (-non-nil (--map (let* ((correction (advent/dot it rrow))
(corrected-a (- a correction)))
(unless (< corrected-a 0)
(-replace-at i corrected-a it)))
soln))
last-used-button i
current-row (1- current-row)) ; this needs to change
;;otherwise, we create a number of solutions
(let* ((button (-last-item possible-indices)))
(setq soln (--mapcat (let* ((max-soln (-min (-non-nil (-map (lambda (row)
(when (= 1 (nth button (cdr row)))
(- (car row) (advent/dot it (cdr row)))))
matrix)))))
(unless (< max-soln 0)
(-map (lambda (candidate) (-replace-at button candidate it)) (-iota (1+ max-soln) max-soln -1))))
soln)
last-used-button button))))
(setq soln (--filter (= a (advent/dot it rrow)) soln)
current-row (1- current-row)))))
(push soln soln-acc)
(apply #'append soln-acc)))
#+end_src
#+RESULTS:
: solve-well-ordered-chunks

Loading…
Cancel
Save