Mercurial > coderloop
view src/lying.clj @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
line wrap: on
line source
1 (ns coderloop.lying2 (:use [clojure.contrib3 [duck-streams :only [read-lines]]4 [seq :only [find-first]]5 [str-utils :only [re-gsub]]])6 (:use rlm.shell-inspect)7 (:use rlm.map-utils)8 (:use [clojure [string :only [split trim]]]))12 (comment13 614 JohnDoe - 115 MisterBlanko16 JohnTitor - 217 DirkWhatever18 BrunoJoeConner19 DirkWhatever - 120 FooBar21 BrunoJoeConner - 222 JohnTitor23 MisterBlanko24 MisterBlanko - 125 DirkWhatever26 FooBar - 327 DirkWhatever28 BrunoJoeConner29 JohnDoe30 )33 (def witnesses34 {:john-doe [:mister-blanco]35 :john-tator [:dirk-whatever :bruno-joe-conner]36 :dirk-whatever [:foo-bar]37 :bruno-joe-conner [:john-tator :mister-blanco]38 :mister-blanco [:dirk-whatever]39 :foo-bar [:dirk-whatever :bruno-joe-conner :john-doe]})41 (def t "/home/r/coderloop-test/test.txt")42 (def a "/home/r/coderloop-test/cluedo-a.in")44 (defn parse-file [f]45 (let [lines (map clojure.string/trim (rest (read-lines f)))]46 (map-keys47 (comp (partial re-gsub #"\W*:\W*\d*\W*" "") first)48 (apply hash-map49 (partition-by #(re-matches #".*:.*" %) lines)))))52 (defrecord verdict [good bad])54 (def initial-verdict (verdict. #{} #{}))56 (defn valid-verdict? [num verdict]57 (and58 (>= num (+ (count (:good verdict)) (count (:bad verdict))))59 (not (reduce #(or %1 %2)60 (map (:good verdict) (:bad verdict))))))62 (defn expand-verdict [judgements [person accuse]]63 (let [y (flatten64 (for [record judgements]65 [(verdict. (into (:good record) (vector person))66 (into (:bad record) accuse))67 (verdict. (into (:good record) accuse)68 (into (:bad record) (vector person)))]))]69 ;; (println (count y))70 ;; (dorun (map println y))71 y))73 (defn determine-guilt [witnesses]74 (let [valid? (partial valid-verdict? (count witnesses))]75 (filter valid?76 (reduce (comp77 (partial filter valid?)78 expand-verdict)79 [initial-verdict] witnesses))))81 (defn print-soln [witnesses]82 (let [verdict (first (determine-guilt witnesses))83 group-nums (sort [(count (:good verdict)) (count (:bad verdict))])]84 (println (str (first group-nums) \: (second group-nums)))))87 (if (command-line?)88 (print-soln (parse-file (first *command-line-args*))))