src/2021/days/day-07.lisp @ cec05589a84c
2021/14
| author | Steve Losh <steve@stevelosh.com> |
|---|---|
| date | Tue, 14 Dec 2021 20:19:00 -0500 |
| parents | b8ca529c9228 |
| children | (none) |
(advent:defpackage* :advent/2021/07) (in-package :advent/2021/07) (defun triangle (n) (/ (* n (1+ n)) 2)) (defun cost (crabs position &key modifier) (summation crabs :key (lambda (crab) (funcall modifier (abs (- crab position)))))) (defun find-best-cost (crabs &key cost-modifier) (multiple-value-bind (lo hi) (extrema #'< crabs) (iterate (for pos :from lo :to hi) (minimizing (cost crabs pos :modifier cost-modifier))))) (define-problem (2021 7) (data read-comma-separated-integers) (328187 91257582) (values (find-best-cost data :cost-modifier #'identity) (find-best-cost data :cost-modifier #'triangle))) #; Scratch --------------------------------------------------------------------