0a5694fe577c

MMCH
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 24 Jan 2020 22:42:19 -0500
parents 888f5c30c949
children e3aefcbf364c
branches/tags (none)
files src/problems/mmch.lisp src/utils.lisp

Changes

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/problems/mmch.lisp	Fri Jan 24 22:42:19 2020 -0500
@@ -0,0 +1,36 @@
+(defpackage :rosalind/mmch (:use :cl :rosalind :losh :iterate))
+(in-package :rosalind/mmch)
+
+(defparameter *input* ">Rosalind_92
+AUGCUUC")
+
+(defparameter *output* "6")
+
+;; We can attack this similarly to PMCH.
+;;
+;; Consider the A/U graph, and assume without loss of generality that the number
+;; of As is less than or equal to the number of Us.  For each A, we choose
+;; a U to pair it with like in PMCM, but this time we don't have enough A's to
+;; get the full factorial.  Instead we get U(U-1)(U-2)…(U-(A-1)) which we can
+;; just multiply out.
+;;
+;; … why did I get a dynamic programming achievement on Rosalind for solving
+;; this problem?
+
+(defun pairings (a b)
+  (when (< b a)
+    (rotatef a b))
+  (iterate (repeat a)
+           (for n :downfrom b)
+           (multiplying n)))
+
+(define-problem mmch (data stream) *input* *output*
+  (let* ((bases (nth-value 1 (u:read-fasta data)))
+         (freqs (frequencies bases)))
+    (* (pairings (gethash #\A freqs)
+                 (gethash #\U freqs))
+       (pairings (gethash #\C freqs)
+                 (gethash #\G freqs)))))
+
+#; Scratch --------------------------------------------------------------------
+(problem-mmch)
--- a/src/utils.lisp	Mon Jan 20 14:19:26 2020 -0500
+++ b/src/utils.lisp	Fri Jan 24 22:42:19 2020 -0500
@@ -530,7 +530,10 @@
 
 (defun solve% (problem)
   (with-open-file (input (problem-data-path problem))
-    (pbcopy (aesthetic-string (funcall problem input)))))
+    (let ((result (aesthetic-string (funcall problem input))))
+      (pbcopy result)
+      (format t "Copied the following to clipboard:~%~A~%" result)
+      (values))))
 
 (defmacro solve (name)
   (assert (symbolp name) ()