Created
December 14, 2015 04:39
-
-
Save akjetma/1c7ba3fcfe764998d9ca to your computer and use it in GitHub Desktop.
TripleByte question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns triplebyte.core) | |
(defn split-string | |
([s position] (split-string s (count s) position)) | |
([s length position] | |
[(subs s 0 position) | |
(subs s position length)])) | |
(defn twigs | |
[branch] | |
(let [length (count branch)] | |
(mapv | |
(partial split-string branch length) | |
(range 1 (inc length))))) | |
(defn bloom | |
[branch] | |
(mapv | |
(fn [[stem leaf]] | |
(if (empty? leaf) | |
[stem] | |
[stem (bloom leaf)])) | |
(twigs branch))) | |
(defn plot | |
([tree] (plot [] [] tree)) | |
([forest path tree] | |
(if tree | |
(reduce | |
(fn [forest [turn group]] | |
(plot forest (conj path turn) group)) | |
forest | |
tree) | |
(conj forest path)))) | |
(def op-name | |
{* "*" | |
/ "/" | |
+ "+" | |
- "-"}) | |
(defn make-context | |
[display value] | |
{:display display :value value}) | |
(defn init-context | |
[head] | |
(make-context | |
(str head) | |
head)) | |
(defn compute-context | |
[word context op] | |
(make-context | |
(str "(" (get op-name op) " " (:display context) " " word ")") | |
(op (:value context) word))) | |
(defn compute | |
[word context] | |
(mapv | |
(partial compute-context word context) | |
[* / + -])) | |
(defn check | |
[goal context] | |
(if (= goal (:value context)) | |
[context] | |
[])) | |
(defn operate | |
([goal sentence] | |
(let [head (bigint (first sentence)) | |
tail (rest sentence) | |
context (init-context head)] | |
(if (empty? tail) | |
(check goal context) | |
(operate goal tail context)))) | |
([goal sentence context] | |
(let [head (bigint (first sentence)) | |
tail (rest sentence) | |
cur (compute head context)] | |
(if (empty? tail) | |
(mapcat (partial check goal) cur) | |
(mapcat (partial operate goal tail) cur))))) | |
(defn match | |
[input goal] | |
(let [input (str input) | |
goal (bigint goal) | |
sentences (-> input bloom plot) | |
winners (mapcat (partial operate goal) sentences)] | |
(mapv :display winners))) | |
;; | |
;; (def pi "314159265358") | |
;; (def e 27182) | |
;; | |
;; #=> (clojure.pprint/pprint (match pi e)) | |
;; ["(+ (* (/ (* (- (* (- (* (* 3 1) 4) 1) 59) 2) 6) 5) 35) 8)" | |
;; "(+ (* (/ (* (- (* (- (* (/ 3 1) 4) 1) 59) 2) 6) 5) 35) 8)" | |
;; "(- (* (+ (* (- (* (+ (* 3 1) 4) 159) 26) 5) 3) 5) 8)" | |
;; "(- (* (+ (* (- (* (+ (/ 3 1) 4) 159) 26) 5) 3) 5) 8)" | |
;; "(- (* (+ (* (- (+ 3 1) 41) 5) 9265) 3) 58)" | |
;; "(+ (+ (+ (* (* 3 14) 15) 9) 26535) 8)" | |
;; "(- (- (* (+ (* (+ (+ 3 1415) 92) 6) 5) 3) 5) 8)" | |
;; "(- (* (+ (* (+ (* (+ (- (/ 31 4) 1) 5) 92) 6) 5) 3) 5) 8)" | |
;; "(- (* (- (+ (* (+ (* 314 1) 592) 6) 5) 3) 5) 8)" | |
;; "(- (* (- (+ (* (+ (/ 314 1) 592) 6) 5) 3) 5) 8)" | |
;; "(- (* (+ (+ (* (+ (- 314 1) 592) 6) 5) 3) 5) 8)"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment