Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Last active September 9, 2015 16:19
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 QuantumFractal/ea0dde30b31fcc1fa2ab to your computer and use it in GitHub Desktop.
Save QuantumFractal/ea0dde30b31fcc1fa2ab to your computer and use it in GitHub Desktop.
Google Code Jam Solutions
''' All Your Base
2009 1-C
thomas moll 2015
'''
import sys
filename = 'A-large-practice.in'
outfile = 'file.out'
def main():
with open(filename, 'r') as in_file:
lines = in_file.readlines()
with open(outfile, 'w') as out_file:
for x,line in enumerate(lines[1:]):
value = find_smallest(line.strip())
out_file.write("Case #"+str(x+1)+": "+str(value)+"\n")
def find_smallest(line):
values = {line[0]: 1}
for digit in line:
if digit not in values:
size = len(values)
if size == 1:
values[digit] = 0
else:
values[digit] = size
result = 0
base = max(len(values), 2)
for digit in line:
result *= base
result += values[digit]
return result
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment