Skip to content

Instantly share code, notes, and snippets.

@abedra
Created March 31, 2012 03:39
Show Gist options
  • Save abedra/2259030 to your computer and use it in GitHub Desktop.
Save abedra/2259030 to your computer and use it in GitHub Desktop.
Initial Trammel Experiment
(ns clojurebreaker.core
(:use [trammel.core :only (defconstrainedfn defcontract)])
(:require [clojure.data :as data]
[trammel.provide :as provide]
[trammel.factors :as factors]))
(defn valid-inputs? [& colls]
(and (every? vector? colls)
(every? true? (map (fn [coll]
(every? #(some #{%} [:r :g :b :y]) coll))
colls))))
(defn properly-structured?
[coll]
(and (contains? coll :exact) (<= 0 (:exact coll))
(contains? coll :unordered)) (<= 0 (:unordered coll)))
(defn bounded-by-number-of-pegs?
[secret guess score]
(<= 0 (+ (:exact score) (:unordered score)) (count secret)))
(defcontract exact-contract
"Ensures input to exact-matches is properly constructed.
Ensures the output is a number greater than zero."
[c1 c2]
[(valid-inputs? c1 c2) => number? factors/all-positive?])
(defcontract unordered-contract
"Ensures input to exact-matches is properly constructed.
Ensures the output is a map."
[c1 c2]
[(valid-inputs? c1 c2) => map?])
(defcontract scoring-contract
"Ensures input to exact-matches is properly constructed.
Ensures the output is a map containing the keys :exact and
:unordered and that the values of each are a number greater
than zero. Ensures that the score is not higher than the
number of pegs."
[c1 c2]
[(valid-inputs? c1 c2) =>
map?
(properly-structured? %)
(bounded-by-number-of-pegs? c1 c2 %)])
(defn exact-matches
"Given two collections, return the number of
positions where the collections contain equal
items."
[c1 c2]
(let [[_ _ matches] (data/diff c1 c2)]
(count (remove nil? matches))))
(defn unordered-matches
"Given two collections, return a map where each
key is an item in both collections, and each
value is the minumum number of occurrences"
[c1 c2]
(let [f1 (select-keys (frequencies c1) c2)
f2 (select-keys (frequencies c2) c1)]
(merge-with min f1 f2)))
(defn score
"Given two collections, returns a map containing the exact and
unordered matches between them"
[c1 c2]
(let [exact (exact-matches c1 c2)
unordered (apply +
(vals
(unordered-matches c1 c2)))]
{:exact exact :unordered (- unordered exact)}))
(provide/contracts [exact-matches "Constraints for exact matches" exact-contract]
[unordered-matches "Constraints for unordered matches" unordered-contract]
[score "Constraints for score" scoring-contract])
@fogus
Copy link

fogus commented Mar 31, 2012

Look at the use for trammel.provide/contracts at http://fogus.github.com/trammel/provide-contracts/index.html

@abedra
Copy link
Author

abedra commented Mar 31, 2012

Thanks! Here is an updated version with things properly separated. I also started looking into factors and need to clean some of this up and just use what trammel provides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment