Created
April 9, 2013 23:41
-
-
Save Denommus/5350411 to your computer and use it in GitHub Desktop.
Solution for the Code Jam's problem "All Your Base" (https://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
| #!/usr/bin/sbcl --script | |
| (defun count-unique (alist) | |
| (let ((hash (make-hash-table :test #'equal))) | |
| (map 'nil | |
| (lambda (i) | |
| (setf (gethash i hash) 1)) | |
| alist) | |
| (hash-table-count hash))) | |
| (defun determine-number (digits) | |
| (let ((hash (make-hash-table :test #'equal)) | |
| (current 0) | |
| (total 0) | |
| (size (length digits)) | |
| (base (count-unique digits))) | |
| (setf base (if (= base 1) 2 base)) | |
| (setf (gethash (elt digits 0) hash) 1) | |
| (loop for i from 0 below size | |
| for x = (- size 1) then (1- x) | |
| for d = (elt digits i) | |
| do | |
| (unless (gethash d hash) | |
| (setf (gethash d hash) current) | |
| (incf current) | |
| (when (= current 1) (incf current))) | |
| (incf total (* (gethash d hash) (expt base x)))) | |
| total)) | |
| (with-open-file (in (car (last *posix-argv*))) | |
| (let ((total-cases (read in))) | |
| (with-open-file (out "A.out" :direction :output :if-exists :supersede) | |
| (loop for i from 1 to total-cases | |
| do | |
| (let ((test-case (read-line in))) | |
| (format out "Case #~D: ~D~%" i (determine-number test-case))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment