Skip to content

Instantly share code, notes, and snippets.

@kototama
Created April 11, 2012 13:33
Show Gist options
  • Save kototama/2359312 to your computer and use it in GitHub Desktop.
Save kototama/2359312 to your computer and use it in GitHub Desktop.
(ns seven.core
(:use clojure.math.combinatorics))
(defn rpn-eval
[e]
(loop [[arg1 arg2 op & nxt] e]
(if (nil? arg2)
arg1
(let [result (try (eval (list op arg1 arg2))
(catch Exception e nil))]
(if (nil? result)
nil
(recur (cons result nxt)))))))
(defn build-exprs
[]
(let [numbers [7 7 7 7 7 1]
ops '[- * - /]]
(mapcat (fn [currentops]
(permutations (concat numbers currentops)))
(selections ops (dec (count numbers))))))
(defn solve
[]
(let [exprs (build-exprs)]
;; (doseq [x exprs]
;; (printf "%s -> %s\n" (list x) (rpn-eval x)))
(first (filter (fn [expr] (= (rpn-eval expr) 100)) exprs))))
(defn -main
[]
(let [sol (solve)]
(printf "solution is: ")
(prn sol)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment