Skip to content

Instantly share code, notes, and snippets.

@funkotron
Created February 15, 2014 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funkotron/9014313 to your computer and use it in GitHub Desktop.
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
(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