Created
February 15, 2014 03:54
-
-
Save funkotron/9014313 to your computer and use it in GitHub Desktop.
Revisions
-
funkotron created this gist
Feb 15, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ (ns jam.all-your-base (:require [clojure.math.numeric-tower :as math])) (def in (rest (clojure.string/split (slurp "A-large-practice.in") #"\n"))) (defn p [x y] (math/expt x y)) (defn next-index [x] (cond (= x 0) 1 (= x 1) 0 :else x)) (defn solve [q] (let [alphabet (into (sorted-set) (distinct q)) base (max 2 (count alphabet))] (loop [left (seq q) position 0 result (bigint 0) code {}] (if (nil? (first left)) result (let [x (first left) new-val (if (contains? code x) (code x) (next-index (count code)))] (recur (rest left) (inc position) (+ result (* (p base (dec (count left))) new-val)) (assoc code x new-val))))))) (defn write-solve [i x] (format "Case #%d: %d" (inc i) (biginteger (solve x)))) (spit "answerlarge.out2" (clojure.string/join "\n" (map-indexed write-solve in)))