]> git.scottworley.com Git - planeteer/blobdiff - planeteer.go
Index commodities by their name.
[planeteer] / planeteer.go
index fd697beab4216deaf35127a5c161f875b57f2f36..bc47c3d0320aeecde0d612f3f331ef5fa46353b6 100644 (file)
@@ -25,23 +25,21 @@ import "fmt"
 var datafile = flag.String("planet_data_file", "planet-data",
        "The file to read planet data from")
 
+type Commodity struct {
+       BasePrice int
+       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 []struct {
-               Name      string
-               BasePrice int
-               CanSell   bool
-               Limit     int
-       }
-       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 []struct {
-                       Name  string
-                       Value int
-               }
-       }
+       Commodities map [string] Commodity
+       Planets []Planet
 }
 
 func ReadData() (data planet_data) {
@@ -57,8 +55,75 @@ func ReadData() (data planet_data) {
        return
 }
 
+/* What is the value of hauling 'commodity' from 'from' to 'to'?
+ * Take into account the available funds and the available cargo space. */
+func TradeValue(data planet_data,
+                from, to *Planet,
+                commodity string,
+                initial_funds, max_quantity int) int {
+       if !data.Commodities[commodity].CanSell {
+               return 0
+       }
+       from_relative_price, from_available := from.RelativePrices[commodity]
+       if !from_available {
+               return 0
+       }
+       to_relative_price, to_available := to.RelativePrices[commodity]
+       if !to_available {
+               return 0
+       }
+
+       base_price := data.Commodities[commodity].BasePrice
+       from_absolute_price := from_relative_price * base_price
+       to_absolute_price := to_relative_price * base_price
+       buy_price := from_absolute_price
+       sell_price := int(float64(to_absolute_price) * 0.9)
+       var can_afford int = initial_funds / buy_price
+       quantity := can_afford
+       if quantity > max_quantity {
+               quantity = max_quantity
+       }
+       return (sell_price - buy_price) * max_quantity
+}
+
+func FindBestTrades(data planet_data) [][]string {
+       best := make([][]string, len(data.Planets))
+       for from_index, from_planet := range data.Planets {
+               best[from_index] = make([]string, len(data.Planets))
+               for to_index, to_planet := range data.Planets {
+                       best_gain := 0
+                       price_list := from_planet.RelativePrices
+                       if len(to_planet.RelativePrices) < len(from_planet.RelativePrices) {
+                               price_list = to_planet.RelativePrices
+                       }
+                       for commodity := range price_list {
+                               gain := TradeValue(data,
+                                                  &from_planet,
+                                                  &to_planet,
+                                                  commodity,
+                                                  10000000,
+                                                  1)
+                               if gain > best_gain {
+                                       best[from_index][to_index] = commodity
+                                       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] != "" {
+                               best_trade = best_trades[from_index][to_index]
+                       }
+                       fmt.Printf("%s to %s: %s\n", from_planet.Name, to_planet.Name, best_trade)
+               }
+       }
 }