rlm@0
|
1 (ns coderloop.lying
|
rlm@0
|
2 (:use [clojure.contrib
|
rlm@0
|
3 [duck-streams :only [read-lines]]
|
rlm@0
|
4 [seq :only [find-first]]
|
rlm@0
|
5 [str-utils :only [re-gsub]]])
|
rlm@0
|
6 (:use rlm.shell-inspect)
|
rlm@0
|
7 (:use rlm.map-utils)
|
rlm@0
|
8 (:use [clojure [string :only [split trim]]]))
|
rlm@0
|
9
|
rlm@0
|
10
|
rlm@0
|
11
|
rlm@0
|
12 (comment
|
rlm@0
|
13 6
|
rlm@0
|
14 JohnDoe - 1
|
rlm@0
|
15 MisterBlanko
|
rlm@0
|
16 JohnTitor - 2
|
rlm@0
|
17 DirkWhatever
|
rlm@0
|
18 BrunoJoeConner
|
rlm@0
|
19 DirkWhatever - 1
|
rlm@0
|
20 FooBar
|
rlm@0
|
21 BrunoJoeConner - 2
|
rlm@0
|
22 JohnTitor
|
rlm@0
|
23 MisterBlanko
|
rlm@0
|
24 MisterBlanko - 1
|
rlm@0
|
25 DirkWhatever
|
rlm@0
|
26 FooBar - 3
|
rlm@0
|
27 DirkWhatever
|
rlm@0
|
28 BrunoJoeConner
|
rlm@0
|
29 JohnDoe
|
rlm@0
|
30 )
|
rlm@0
|
31
|
rlm@0
|
32
|
rlm@0
|
33 (def witnesses
|
rlm@0
|
34 {:john-doe [:mister-blanco]
|
rlm@0
|
35 :john-tator [:dirk-whatever :bruno-joe-conner]
|
rlm@0
|
36 :dirk-whatever [:foo-bar]
|
rlm@0
|
37 :bruno-joe-conner [:john-tator :mister-blanco]
|
rlm@0
|
38 :mister-blanco [:dirk-whatever]
|
rlm@0
|
39 :foo-bar [:dirk-whatever :bruno-joe-conner :john-doe]})
|
rlm@0
|
40
|
rlm@0
|
41 (def t "/home/r/coderloop-test/test.txt")
|
rlm@0
|
42 (def a "/home/r/coderloop-test/cluedo-a.in")
|
rlm@0
|
43
|
rlm@0
|
44 (defn parse-file [f]
|
rlm@0
|
45 (let [lines (map clojure.string/trim (rest (read-lines f)))]
|
rlm@0
|
46 (map-keys
|
rlm@0
|
47 (comp (partial re-gsub #"\W*:\W*\d*\W*" "") first)
|
rlm@0
|
48 (apply hash-map
|
rlm@0
|
49 (partition-by #(re-matches #".*:.*" %) lines)))))
|
rlm@0
|
50
|
rlm@0
|
51
|
rlm@0
|
52 (defrecord verdict [good bad])
|
rlm@0
|
53
|
rlm@0
|
54 (def initial-verdict (verdict. #{} #{}))
|
rlm@0
|
55
|
rlm@0
|
56 (defn valid-verdict? [num verdict]
|
rlm@0
|
57 (and
|
rlm@0
|
58 (>= num (+ (count (:good verdict)) (count (:bad verdict))))
|
rlm@0
|
59 (not (reduce #(or %1 %2)
|
rlm@0
|
60 (map (:good verdict) (:bad verdict))))))
|
rlm@0
|
61
|
rlm@0
|
62 (defn expand-verdict [judgements [person accuse]]
|
rlm@0
|
63 (let [y (flatten
|
rlm@0
|
64 (for [record judgements]
|
rlm@0
|
65 [(verdict. (into (:good record) (vector person))
|
rlm@0
|
66 (into (:bad record) accuse))
|
rlm@0
|
67 (verdict. (into (:good record) accuse)
|
rlm@0
|
68 (into (:bad record) (vector person)))]))]
|
rlm@0
|
69 ;; (println (count y))
|
rlm@0
|
70 ;; (dorun (map println y))
|
rlm@0
|
71 y))
|
rlm@0
|
72
|
rlm@0
|
73 (defn determine-guilt [witnesses]
|
rlm@0
|
74 (let [valid? (partial valid-verdict? (count witnesses))]
|
rlm@0
|
75 (filter valid?
|
rlm@0
|
76 (reduce (comp
|
rlm@0
|
77 (partial filter valid?)
|
rlm@0
|
78 expand-verdict)
|
rlm@0
|
79 [initial-verdict] witnesses))))
|
rlm@0
|
80
|
rlm@0
|
81 (defn print-soln [witnesses]
|
rlm@0
|
82 (let [verdict (first (determine-guilt witnesses))
|
rlm@0
|
83 group-nums (sort [(count (:good verdict)) (count (:bad verdict))])]
|
rlm@0
|
84 (println (str (first group-nums) \: (second group-nums)))))
|
rlm@0
|
85
|
rlm@0
|
86
|
rlm@0
|
87 (if (command-line?)
|
rlm@0
|
88 (print-soln (parse-file (first *command-line-args*))))
|