[p10] really

master
Jacopo De Simoi 6 months ago
parent 3c85a967c0
commit 2ac318c1a2
  1. 21
      p10/p10.org

@ -580,7 +580,7 @@ try to split into chunks
(create-chunks 3 '(a b c d e)) (create-chunks 3 '(a b c d e))
(defun solve-well-ordered-chunks (matrix) (defun solve-well-ordered-chunks-mod (matrix prime)
;; 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))))
@ -604,7 +604,7 @@ try to split into chunks
(setq soln new-soln (setq soln new-soln
soln-chunks (append chunks-to-store soln-chunks)))) soln-chunks (append chunks-to-store soln-chunks))))
(let* ((row (nth current-row matrix)) (let* ((row (nth current-row matrix))
(a (car row)) (a (mod (car row) prime))
(rrow (cdr row)) (rrow (cdr row))
(i (--find-index (not (zerop it)) (-take last-used-button rrow)))) (i (--find-index (not (zerop it)) (-take last-used-button rrow))))
(if i (if i
@ -612,24 +612,19 @@ try to split into chunks
(-iota (- last-used-button i) i)))) (-iota (- last-used-button i) i))))
(if (= 1 (length possible-indices)) ;no choices here, easy (if (= 1 (length possible-indices)) ;no choices here, easy
(setq soln (-non-nil (--map (let* ((correction (advent/dot it rrow)) (setq soln (-non-nil (--map (let* ((correction (advent/dot it rrow))
(corrected-a (- a correction))) (corrected-a (mod (- a correction) prime)))
(unless (< corrected-a 0) (-replace-at i corrected-a it))
(-replace-at i corrected-a it)))
soln)) soln))
last-used-button i last-used-button i
current-row (1- current-row)) ; this needs to change current-row (1- current-row)) ; this needs to change
;;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 (-map (lambda (candidate)
(when (= 1 (nth button (cdr row))) (-replace-at button candidate it))
(- (car row) (advent/dot it (cdr row))))) (-iota prime))
matrix)))))
(unless (< max-soln 0)
(-map (lambda (candidate) (-replace-at button candidate it)) (-iota (1+ max-soln) max-soln -1))))
soln) soln)
last-used-button button)))) last-used-button button))))
(setq soln (--filter (= a (advent/dot it rrow)) soln) (setq soln (--filter (= (mod a prime) (mod (advent/dot it rrow) prime)) soln)
current-row (1- current-row))))) current-row (1- current-row)))))
(push soln soln-acc) (push soln soln-acc)
(apply #'append soln-acc))) (apply #'append soln-acc)))

Loading…
Cancel
Save