-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2017-18-duet.clj
More file actions
30 lines (26 loc) · 1.1 KB
/
Copy path2017-18-duet.clj
File metadata and controls
30 lines (26 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(defn parse [s]
(if-let [x (parse-long s)]
x (symbol s)))
(defn value [m v]
(if (symbol? v) (get (:reg m) v 0) v))
(def instructions
{'snd (fn [m x _] (update (assoc m :frq (value m x)) :pos inc))
'set (fn [m x y] (update (update m :reg assoc x (value m y)) :pos inc))
'add (fn [m x y] (update (update m :reg update x (fnil + 0) (value m y)) :pos inc))
'mul (fn [m x y] (update (update m :reg update x (fnil * 0) (value m y)) :pos inc))
'mod (fn [m x y] (update (update m :reg update x (fnil rem 0) (value m y)) :pos inc))
'rcv (fn [m _ _] (update m :pos inc))
'jgz (fn [m x y] (update m :pos + (if (> (value m x) 0) (value m y) 1)))})
(defn receive [xs]
(loop [m {:pos 0 :reg {} :frq nil}]
(let [[ins x y] (xs (:pos m))]
(if (= ins 'rcv)
(:frq m)
(recur ((instructions ins) m x y))))))
(let [in (line-seq (java.io.BufferedReader. *in*))
xf (comp
(map (fn [ln] (re-seq #"-?\w+" ln)))
(map (fn [xs] (into [(symbol (first xs))] (map parse) (rest xs)))))
xs (into [] xf in)]
(println "Part A:" (receive xs))
(println "Part B:" nil))