Skip to content

Instantly share code, notes, and snippets.

@funkotron
Created February 15, 2014 03:54
Show Gist options
  • Select an option

  • Save funkotron/9014313 to your computer and use it in GitHub Desktop.

Select an option

Save funkotron/9014313 to your computer and use it in GitHub Desktop.

Revisions

  1. funkotron created this gist Feb 15, 2014.
    35 changes: 35 additions & 0 deletions all_your_base.clj
    Original 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)))