comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:307a81e46071
1 (ns coderloop.lying
2 (:use [clojure.contrib
3 [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]]]))
9
10
11
12 (comment
13 6
14 JohnDoe - 1
15 MisterBlanko
16 JohnTitor - 2
17 DirkWhatever
18 BrunoJoeConner
19 DirkWhatever - 1
20 FooBar
21 BrunoJoeConner - 2
22 JohnTitor
23 MisterBlanko
24 MisterBlanko - 1
25 DirkWhatever
26 FooBar - 3
27 DirkWhatever
28 BrunoJoeConner
29 JohnDoe
30 )
31
32
33 (def witnesses
34 {: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]})
40
41 (def t "/home/r/coderloop-test/test.txt")
42 (def a "/home/r/coderloop-test/cluedo-a.in")
43
44 (defn parse-file [f]
45 (let [lines (map clojure.string/trim (rest (read-lines f)))]
46 (map-keys
47 (comp (partial re-gsub #"\W*:\W*\d*\W*" "") first)
48 (apply hash-map
49 (partition-by #(re-matches #".*:.*" %) lines)))))
50
51
52 (defrecord verdict [good bad])
53
54 (def initial-verdict (verdict. #{} #{}))
55
56 (defn valid-verdict? [num verdict]
57 (and
58 (>= num (+ (count (:good verdict)) (count (:bad verdict))))
59 (not (reduce #(or %1 %2)
60 (map (:good verdict) (:bad verdict))))))
61
62 (defn expand-verdict [judgements [person accuse]]
63 (let [y (flatten
64 (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))
72
73 (defn determine-guilt [witnesses]
74 (let [valid? (partial valid-verdict? (count witnesses))]
75 (filter valid?
76 (reduce (comp
77 (partial filter valid?)
78 expand-verdict)
79 [initial-verdict] witnesses))))
80
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)))))
85
86
87 (if (command-line?)
88 (print-soln (parse-file (first *command-line-args*))))