Groovy web console

subscribe to the feed Subscribe
to this
site
Ricardo (via #groovywebconsole)
tweet this snippet Tweet
this
script

Ricardo

Published 11 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def ciudadPartida = "MVD"
def ciudadDestino = "AS"
def aeropuertos= ["SC", "BA","AS"] //santiago de chile, buenos aires, porto alegre, asuncion, rio de janeiro
def USE_DYNAMIC_PROGRAMMING = false
def cache =[:]
def callStack=[]
def pricesCheaper=[]

def priceCalculator= {aerop1 , aerop2 ->
callStack+="${aerop1}-${aerop2}"
if(aerop1 == "MVD"){
    if(aerop2=="SC"){
        return 186
    }
    if(aerop2=="BA"){
        return 187
    }
    if(aerop2=="AS"){
        return 353
    }
}
if(aerop1 == "SC"){
    if(aerop2=="AS"){
        return 5000
    }
}
if(aerop1 == "BA"){
    if(aerop2=="AS"){
        return 70
    }
}
}


def priceCalculatorWrapper={ aerop1,aerop2 ->
if(USE_DYNAMIC_PROGRAMMING){
    
    def val= cache["${aerop1}-${aerop2}"]
    if(val){
        return val
    }
    def returnValue= priceCalculator(aerop1 , aerop2)
    cache["${aerop1}-${aerop2}"]= returnValue
return returnValue
}
    return priceCalculator(aerop1 , aerop2)
}

for(aeropuerto in aeropuertos){
    def actualRoute ="${ciudadPartida}-${aeropuerto}"
    def flightPrice = priceCalculatorWrapper(ciudadPartida, aeropuerto)
    if(aeropuerto!= ciudadDestino){
        actualRoute+="-${ciudadDestino}"
        flightPrice+= priceCalculatorWrapper(aeropuerto, ciudadDestino)
    }
    if(flightPrice < priceCalculatorWrapper(ciudadPartida, ciudadDestino)){
        pricesCheaper +=[flightPlan: actualRoute, price: flightPrice]
    }
}
println "llamadas al proveedor: "+callStack.size() +" resumen precio mas barato: "+ pricesCheaper.sort{it.price}