src/2021/days/day-07.lisp @ 2848a4548adf

2023/01 and 2022/01

Also start porting my test data to the new account, since Twitter imploded and
apparently it's impossible for a website to store a goddamn username and
password in The Year of Our Lord 2023 so everyone just outsources auth all
the time, ugh.
author Steve Losh <steve@stevelosh.com>
date Fri, 01 Dec 2023 11:05:43 -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 --------------------------------------------------------------------