Created
February 15, 2014 03:54
-
-
Save funkotron/9014313 to your computer and use it in GitHub Desktop.
Solution to "All Your Base" Google Codejam contest in Clojure http://code.google.com/codejam/contest/189252/dashboard#s=p0
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 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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment