]> git.scottworley.com Git - planeteer/blobdiff - planeteer.go
Use pointers more and indexes less
[planeteer] / planeteer.go
index 31dc7d0a07a632e785ae8da32ccdc96b88fd7628..57f00ecc0780668ce6bf00f08ff8f121542c9da7 100644 (file)
@@ -31,16 +31,16 @@ type Commodity struct {
        CanSell   bool
        Limit     int
 }
-
+type Planet struct {
+       Name     string
+       BeaconOn bool
+       /* Use relative prices rather than absolute prices because you
+          can get relative prices without traveling to each planet. */
+       RelativePrices map [string] int
+}
 type planet_data struct {
        Commodities []Commodity
-       Planets []struct {
-               Name     string
-               BeaconOn bool
-               /* Use relative prices rather than absolute prices because you
-                  can get relative prices without traveling to each planet. */
-               RelativePrices map [string] int
-       }
+       Planets []Planet
 }
 
 func ReadData() (data planet_data) {
@@ -56,8 +56,61 @@ func ReadData() (data planet_data) {
        return
 }
 
+func TradeValue(from, to *Planet,
+                commodity *Commodity,
+                quantity int) int {
+       if !commodity.CanSell {
+               return 0
+       }
+       from_relative_price, from_available := from.RelativePrices[commodity.Name]
+       if !from_available {
+               return 0
+       }
+       to_relative_price, to_available := to.RelativePrices[commodity.Name]
+       if !to_available {
+               return 0
+       }
+
+       from_absolute_price := from_relative_price * commodity.BasePrice
+       to_absolute_price := to_relative_price * commodity.BasePrice
+       buy_price := from_absolute_price
+       sell_price := int(float64(to_absolute_price) * 0.9)
+       return (sell_price - buy_price) * quantity
+
+}
+
+func FindBestTrades(data planet_data) [][]*Commodity {
+       best := make([][]*Commodity, len(data.Planets))
+       for from_index := range data.Planets {
+               best[from_index] = make([]*Commodity, len(data.Planets))
+               for to_index := range data.Planets {
+                       best_gain := 0
+                       for commodity_index := range data.Commodities {
+                               gain := TradeValue(&data.Planets[from_index],
+                                                  &data.Planets[to_index],
+                                                  &data.Commodities[commodity_index],
+                                                  1)
+                               if gain > best_gain {
+                                       best[from_index][to_index] = &data.Commodities[commodity_index]
+                                       gain = best_gain
+                               }
+                       }
+               }
+       }
+       return best
+}
+
 func main() {
        flag.Parse()
        data := ReadData()
-       fmt.Printf("%v", data)
+       best_trades := FindBestTrades(data)
+       for from_index, from_planet := range data.Planets {
+               for to_index, to_planet := range data.Planets {
+                       best_trade := "(nothing)"
+                       if best_trades[from_index][to_index] != nil {
+                               best_trade = best_trades[from_index][to_index].Name
+                       }
+                       fmt.Printf("%s to %s: %s\n", from_planet.Name, to_planet.Name, best_trade)
+               }
+       }
 }