hi,
no one has posted the solution from my beloved language groovy, so i
add it :)
///////////////////////////////////////////
/////// GROOVY STYLE ALIEN NUMBERS SOLUTION
def digit_value_decimal(number, digit_position, source_lang){
return source_lang.indexOf(number.toList().reverse().get
(digit_position))
}
def analysis(number,digit_position,digit_base, source_lang){
if(digit_position == (number as String).length())
return 0
return (digit_base.power(digit_position) * digit_value_decimal
(number, digit_position, source_lang)
//recursion iteration, work on next digit position
+ analysis(number, digit_position + 1, digit_base,
source_lang))
}
def synthesis(digits, number, base, iteration){
//recursion hook -> we worked on all numbers
if(number == 0)
return digits
digits << (number % base.power(iteration + 1)) / base.power
(iteration)
number -= number % base.power(iteration + 1)
//recursion iteration
return synthesis(digits,number,base,iteration + 1)
}
def lang_representation(digits,lang){
def target=[]
for(i in 0..<digits.size())
target << lang[digits[i] as Integer]
return target
}
def process(filepath){
StringBuffer b=new StringBuffer()
def lines=new File(filepath).readLines()
counter=1
lines.subList(1,lines.size()).each{
items=it.split(" ");
def source_lang = items[1].trim()
def number = analysis(items[0].trim() as String, 0,
source_lang.toList().size(), source_lang)
def target_lang = items[2].trim()
def digits = synthesis([], number, target_lang.toList().size(),
0).reverse()
assert number == analysis(lang_representation(digits,
target_lang.toList()).join(""), 0, target_lang.toList().size(),
target_lang)
b.append("Case #${counter}: ")
b.append(lang_representation(digits, target_lang.toList()).join
(""))
b.append("\n")
counter++
}
return b.toString()
}
new File("resultSmall.txt").write(process("A-small-practice.in"))
new File("resultLarge.txt").write(process("A-large-practice.in"))
/////// END
//////////////////////////////////
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---