diff --git a/p10/p10.org b/p10/p10.org index 474ec27..f004b88 100644 --- a/p10/p10.org +++ b/p10/p10.org @@ -580,7 +580,7 @@ try to split into chunks (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 (let* ((soln-acc nil) (number-of-buttons (1- (length (car matrix)))) @@ -604,7 +604,7 @@ try to split into chunks (setq soln new-soln soln-chunks (append chunks-to-store soln-chunks)))) (let* ((row (nth current-row matrix)) - (a (car row)) + (a (mod (car row) prime)) (rrow (cdr row)) (i (--find-index (not (zerop it)) (-take last-used-button rrow)))) (if i @@ -612,24 +612,19 @@ try to split into chunks (-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))) + (corrected-a (mod (- a correction) prime))) + (-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)))) + (setq soln (--mapcat (-map (lambda (candidate) + (-replace-at button candidate it)) + (-iota prime)) soln) 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))))) (push soln soln-acc) (apply #'append soln-acc)))