src/2021/days/day-07.lisp @ e41337e3b59b
Accessors
author |
Steve Losh <steve@stevelosh.com> |
date |
Wed, 15 Dec 2021 23:10:57 -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 --------------------------------------------------------------------