Text processing/Max licenses in use: Difference between revisions

Line 524:
=={{header|Clojure}}==
 
<lang clojure>(defn delta [entry]
"Determines if the number of licences out should be increased or decreased
according to an entry."
[entry]
(case (second (re-find #"\ (.*)\ @" entry))
"IN " -1
Line 533 ⟶ 530:
(throw (Exception. (str "Invalid entry:" entry)))))
 
(defn timet [entry]
"Extracts the time component of an entry."
[entry]
(second (re-find #"@\ (.*)\ f" entry)))
 
(let [data (processentries (clojure.string/split (slurp "mlijobs.txt") #"\n"))
(defn process
in-use (reductions + (map delta entries))
"Builds a map where keys are the number of licences out and the values are the
m (apply max (keys datain-use))]
times where we reached that number."
[ times (map #(nth (map t entries]) %)
(keep-indexed #(when (= m %2) %1) in-use))]
(first
(reduce
(fn [[m last] entry]
(let [new (+ (delta entry) last)]
[(update m new concat [(time entry)])
new]))
[{} 0] entries)))
 
(let [data (process (clojure.string/split (slurp "mlijobs.txt") #"\n"))
m (apply max (keys data))]
(println "Maximum simultaneous license use is" m "at the following times:")
(map println (data m)times))</lang>
 
<pre>