+}
+
+func FillStateTable2(data planet_data, dims []int, table []State,
+fuel_remaining, edens_remaining int, planet string, barrier chan<- bool) {
+ addr := make([]int, len(dims))
+ addr[Edens] = edens_remaining
+ addr[Fuel] = fuel_remaining
+ addr[Location] = data.p2i[planet]
+ FillStateTable2Iteration(data, dims, table, addr, FillCellByArriving)
+ FillStateTable2Iteration(data, dims, table, addr, FillCellBySelling)
+ FillStateTable2Iteration(data, dims, table, addr, FillCellByBuying)
+ FillStateTable2Iteration(data, dims, table, addr, FillCellByMisc)
+ barrier <- true
+}
+
+/* Filling the state table is a set of nested for loops NumDimensions deep.
+ * We split this into two procedures: 1 and 2. #1 is the outer, slowest-
+ * changing indexes. #1 fires off many calls to #2 that run in parallel.
+ * The order of the nesting of the dimensions, the order of iteration within
+ * each dimension, and where the 1 / 2 split is placed are carefully chosen
+ * to make this arrangement safe.
+ *
+ * Outermost two layers: Go from high-energy states (lots of fuel, edens) to
+ * low-energy state. These must be processed sequentially and in this order
+ * because you travel through high-energy states to get to the low-energy
+ * states.
+ *
+ * Third layer: Planet. This is a good layer to parallelize on. There's
+ * high enough cardinality that we don't have to mess with parallelizing
+ * multiple layers for good utilization (on 2011 machines). Each thread
+ * works on one planet's states and need not synchronize with peer threads.
+ */
+func FillStateTable1(data planet_data, dims []int, table []State) {
+ barrier := make(chan bool, len(data.Planets))
+ eden_capacity := data.Commodities["Eden Warp Units"].Limit
+ work_units := (float64(*fuel) + 1) * (float64(eden_capacity) + 1)
+ work_done := 0.0
+ for fuel_remaining := *fuel; fuel_remaining >= 0; fuel_remaining-- {
+ for edens_remaining := eden_capacity; edens_remaining >= 0; edens_remaining-- {
+ for planet := range data.Planets {
+ go FillStateTable2(data, dims, table, fuel_remaining,
+ edens_remaining, planet, barrier)
+ }
+ for _ = range data.Planets {
+ <-barrier
+ }
+ work_done++
+ print(fmt.Sprintf("\r%3.0f%%", 100*work_done/work_units))
+ }
+ }
+ print("\n")
+}
+
+func FindBestState(data planet_data, dims []int, table []State) int {
+ addr := make([]int, NumDimensions)
+ addr[Edens] = *end_edens
+ addr[Cloaks] = dims[Cloaks] - 1
+ addr[NeedFighters] = dims[NeedFighters] - 1
+ addr[NeedShields] = dims[NeedShields] - 1
+ addr[Visit] = dims[Visit] - 1
+ // Fuel, Hold, UnusedCargo left at 0
+ max_index := -1
+ max_value := 0
+ for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ {
+ index := EncodeIndex(dims, addr)
+ if table[index].value > max_value {
+ max_value = table[index].value
+ max_index = index
+ }
+ }
+ return max_index
+}
+
+func Commas(n int) (s string) {
+ r := n % 1000
+ n /= 1000
+ for n > 0 {
+ s = fmt.Sprintf(",%03d", r) + s
+ r = n % 1000
+ n /= 1000
+ }
+ s = fmt.Sprint(r) + s
+ return
+}
+
+func DescribePath(data planet_data, dims []int, table []State, start int) (description []string) {
+ for index := start; index > 0 && table[index].from > 0; index = table[index].from {
+ line := fmt.Sprintf("%13v", Commas(table[index].value))
+ addr := DecodeIndex(dims, index)
+ prev := DecodeIndex(dims, table[index].from)
+ if addr[Location] != prev[Location] {
+ from := data.i2p[prev[Location]]
+ to := data.i2p[addr[Location]]
+ if addr[Fuel] != prev[Fuel] {
+ line += fmt.Sprintf(" Jump from %v to %v (%v reactor units)", from, to, prev[Fuel]-addr[Fuel])
+ } else if addr[Edens] != prev[Edens] {
+ line += fmt.Sprintf(" Eden warp from %v to %v", from, to)
+ } else {
+ panic("Traveling without fuel?")
+ }
+ }
+ if addr[Hold] != prev[Hold] {
+ if addr[Hold] == 0 {
+ quantity := *hold - (prev[UnusedCargo] + prev[Edens] + prev[Cloaks])
+ line += fmt.Sprintf(" Sell %v %v", quantity, data.i2c[prev[Hold]])
+ } else if prev[Hold] == 0 {
+ quantity := *hold - (addr[UnusedCargo] + addr[Edens] + addr[Cloaks])
+ line += fmt.Sprintf(" Buy %v %v", quantity, data.i2c[addr[Hold]])
+ } else {
+ panic("Switched cargo?")
+ }
+
+ }
+ description = append(description, line)
+ }
+ return