src/problems/conv.lisp @ cd3fc11e3298

CONV, SPEC
author Steve Losh <steve@stevelosh.com>
date Thu, 04 Aug 2022 22:30:18 -0400
parents (none)
children (none)
(defpackage :rosalind/conv (:use :cl :rosalind :losh :iterate))
(in-package :rosalind/conv)

(defparameter *input* "186.07931 287.12699 548.20532 580.18077 681.22845 706.27446 782.27613 968.35544 968.35544
101.04768 158.06914 202.09536 318.09979 419.14747 463.17369")

(defparameter *output* "3
85.03163")

(defun parse (stream)
  (_ (read-line stream)
    (str:split " " _)
    (let ((*read-default-float-format* 'rational))
      (mapcar #'read-from-string _))
    frequencies))

(defun minkowski-difference (s1 s2)
  (iterate
    (with-result result = (make-hash-table :size (* (hash-table-count s1)
                                                    (hash-table-count s2))))
    (for (k1 v1) :in-hashtable s1)
    (iterate (for (k2 v2) :in-hashtable s2)
             (incf (gethash (- k1 k2) result 0)
                   (* v1 v2)))))

(defun msmax (multiset)
  (iterate (for (k v) :in-hashtable multiset)
           (finding (cons k v) :maximizing v)))

(define-problem conv (data stream) *input* *output*
  (destructuring-bind (weight . n)
      (msmax (minkowski-difference (parse data) (parse data)))
    (format nil "~D~%~F" n weight)))