Created
April 11, 2012 13:33
-
-
Save kototama/2359312 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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