|
|
|
@ -2,7 +2,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
#+begin_src emacs-lisp :results none |
|
|
|
#+begin_src emacs-lisp :results none |
|
|
|
(with-temp-buffer |
|
|
|
(with-temp-buffer |
|
|
|
(insert-file-contents "input-test") |
|
|
|
(insert-file-contents "input") |
|
|
|
(advent/replace-multiple-regex-buffer |
|
|
|
(advent/replace-multiple-regex-buffer |
|
|
|
'(("," . " ") |
|
|
|
'(("," . " ") |
|
|
|
("^" . "(") |
|
|
|
("^" . "(") |
|
|
|
@ -174,56 +174,37 @@ is a linear algebra problem. Gauss elimination to the rescue |
|
|
|
;; now, this is correct, but we need a positive solution that has |
|
|
|
;; now, this is correct, but we need a positive solution that has |
|
|
|
;; fewest button presses possible. |
|
|
|
;; fewest button presses possible. |
|
|
|
|
|
|
|
|
|
|
|
(defun unshadowed-buttons (matrix) |
|
|
|
(defun rank (matrix) |
|
|
|
(let ((result)) |
|
|
|
(length (-non-nil (--map (--find-index (not (= 0 it)) (cdr it)) matrix)))) |
|
|
|
(--each (-iota (length matrix) (- (length matrix) 1) -1) |
|
|
|
|
|
|
|
(let* ((row (nth it matrix)) |
|
|
|
|
|
|
|
(i (--find-index (not (= 0 it)) (cdr row)))) |
|
|
|
|
|
|
|
(when i (push i result)))) |
|
|
|
|
|
|
|
result)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun shadowed-buttons (matrix) |
|
|
|
(defun matrix-appl (matrix vector) |
|
|
|
(-difference (-iota (length (cdar matrix))) (unshadowed-buttons matrix))) |
|
|
|
(--map (advent/dot it vector) matrix)) |
|
|
|
|
|
|
|
|
|
|
|
(defun shadowed-button-solution (i matrix) |
|
|
|
(defun solution-p (machine candidate) |
|
|
|
(solve-row-reduced (let ((transpose (apply '-zip-lists matrix))) |
|
|
|
(--every (= 0 it) (matrix-appl (matrix-buttons machine) (cons -1 candidate))) |
|
|
|
(apply '-zip-lists (cons (nth (1+ i) transpose) (cdr transpose)))))) |
|
|
|
) |
|
|
|
|
|
|
|
(setq current-machine nil) |
|
|
|
|
|
|
|
(defun solve--machine (machine) |
|
|
|
|
|
|
|
(setq current-machine machine) |
|
|
|
|
|
|
|
(let ((candidate (solve-row-reduced (row-reduce (matrix-buttons machine))))) |
|
|
|
|
|
|
|
(setq canca candidate) |
|
|
|
|
|
|
|
(and (--every (>= it 0) candidate) (solution-p machine candidate) candidate))) |
|
|
|
|
|
|
|
|
|
|
|
(defun solve-machine (machine) |
|
|
|
(defun solve-machine (machine) |
|
|
|
(let* ((reduced-mat (row-reduce (matrix-buttons machine))) |
|
|
|
(let* ((reduced-mat (row-reduce (matrix-buttons machine))) |
|
|
|
(candidate (solve-row-reduced reduced-mat))) |
|
|
|
(rank (rank reduced-mat)) |
|
|
|
(if (--every (<= 0 it) candidate) candidate |
|
|
|
(bunch (--map (cons (car machine) it) |
|
|
|
(let ((shadowed (shadowed-buttons reduced-mat))) |
|
|
|
(--filter 'identity |
|
|
|
;; try replacing the shadowed button with the previous one |
|
|
|
;(<= rank (length it)) |
|
|
|
(solve-machine |
|
|
|
(-powerset (cdr machine)))))) |
|
|
|
(swap-indices (car shadowed) (1+ (car shadowed)) machine)))))) |
|
|
|
(-min (-map '-sum (-non-nil (-map 'solve--machine bunch)))) |
|
|
|
|
|
|
|
|
|
|
|
(defun vector- (v1 v2) |
|
|
|
|
|
|
|
(--map (- (car it) (cdr it)) (-zip-pair v1 v2))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun push-button (i machine-matrix) |
|
|
|
|
|
|
|
(let ((tr (apply '-zip-lists machine))) |
|
|
|
|
|
|
|
(apply '-zip-lists (cons (vector- (car tr) (nth (1+ i) tr)) (cdr tr))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun machine-valid-p (machine) |
|
|
|
|
|
|
|
(arst) |
|
|
|
|
|
|
|
(--every (>= it 0) (-map #'car machine))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun solve-machine (machine) |
|
|
|
|
|
|
|
(let* ((reduced-mat (row-reduce (matrix-buttons machine))) |
|
|
|
|
|
|
|
(shadowed (shadowed-buttons reduced-mat)) |
|
|
|
|
|
|
|
(max-iter (-max (car machine)))) |
|
|
|
|
|
|
|
;; we create a bunch of machines pushing the shadowed buttons a number of times. |
|
|
|
|
|
|
|
max-iter |
|
|
|
|
|
|
|
)) |
|
|
|
)) |
|
|
|
|
|
|
|
|
|
|
|
(push-button 0 (car machines)) |
|
|
|
(-sum (-map 'solve-machine machines)) |
|
|
|
|
|
|
|
current-machine |
|
|
|
(car machines) |
|
|
|
(solve--machine machine) |
|
|
|
(-map 'solve-machine machines) |
|
|
|
canca |
|
|
|
#+end_src |
|
|
|
#+end_src |
|
|
|
|
|
|
|
|
|
|
|
#+RESULTS: |
|
|
|
#+RESULTS: |
|
|
|
| 5 | 1 | 3 | 0 | 1 | 0 | |
|
|
|
: 33 |
|
|
|
| 2 | 0 | 5 | 5 | 0 | | |
|
|
|
|
|
|
|
| 6 | 5 | -1 | 0 | | | |
|
|
|
|
|
|
|
|