]>
Commit | Line | Data |
---|---|---|
1 | /* Planeteer: Give trade route advice for Planets: The Exploration of Space | |
2 | * Copyright (C) 2011 Scott Worley <sworley@chkno.net> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU Affero General Public License as | |
6 | * published by the Free Software Foundation, either version 3 of the | |
7 | * License, or (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU Affero General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Affero General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | package main | |
19 | ||
20 | import "flag" | |
21 | import "fmt" | |
22 | import "json" | |
23 | import "os" | |
24 | import "strings" | |
25 | ||
26 | var funds = flag.Int("funds", 0, | |
27 | "Starting funds") | |
28 | ||
29 | var start = flag.String("start", "", | |
30 | "The planet to start at") | |
31 | ||
32 | var flight_plan_string = flag.String("flight_plan", "", | |
33 | "Your hidey-holes for the day, comma-separated.") | |
34 | ||
35 | var end_string = flag.String("end", "", | |
36 | "A comma-separated list of acceptable ending planets.") | |
37 | ||
38 | var planet_data_file = flag.String("planet_data_file", "planet-data", | |
39 | "The file to read planet data from") | |
40 | ||
41 | var fuel = flag.Int("fuel", 16, "Reactor units") | |
42 | ||
43 | var hold = flag.Int("hold", 300, "Size of your cargo hold") | |
44 | ||
45 | var start_edens = flag.Int("start_edens", 0, | |
46 | "How many Eden Warp Units are you starting with?") | |
47 | ||
48 | var end_edens = flag.Int("end_edens", 0, | |
49 | "How many Eden Warp Units would you like to keep (not use)?") | |
50 | ||
51 | var cloak = flag.Bool("cloak", false, | |
52 | "Make sure to end with a Device of Cloaking") | |
53 | ||
54 | var drones = flag.Int("drones", 0, "Buy this many Fighter Drones") | |
55 | ||
56 | var batteries = flag.Int("batteries", 0, "Buy this many Shield Batterys") | |
57 | ||
58 | var visit_string = flag.String("visit", "", | |
59 | "A comma-separated list of planets to make sure to visit") | |
60 | ||
61 | func visit() []string { | |
62 | if *visit_string == "" { | |
63 | return nil | |
64 | } | |
65 | return strings.Split(*visit_string, ",") | |
66 | } | |
67 | ||
68 | func flight_plan() []string { | |
69 | if *flight_plan_string == "" { | |
70 | return nil | |
71 | } | |
72 | return strings.Split(*flight_plan_string, ",") | |
73 | } | |
74 | ||
75 | func end() map[string]bool { | |
76 | if *end_string == "" { | |
77 | return nil | |
78 | } | |
79 | m := make(map[string]bool) | |
80 | for _, p := range strings.Split(*flight_plan_string, ",") { | |
81 | m[p] = true | |
82 | } | |
83 | return m | |
84 | } | |
85 | ||
86 | type Commodity struct { | |
87 | BasePrice int | |
88 | CanSell bool | |
89 | Limit int | |
90 | } | |
91 | type Planet struct { | |
92 | BeaconOn bool | |
93 | /* Use relative prices rather than absolute prices because you | |
94 | can get relative prices without traveling to each planet. */ | |
95 | RelativePrices map[string]int | |
96 | } | |
97 | type planet_data struct { | |
98 | Commodities map[string]Commodity | |
99 | Planets map[string]Planet | |
100 | p2i, c2i map[string]int // Generated; not read from file | |
101 | i2p, i2c []string // Generated; not read from file | |
102 | } | |
103 | ||
104 | func ReadData() (data planet_data) { | |
105 | f, err := os.Open(*planet_data_file) | |
106 | if err != nil { | |
107 | panic(err) | |
108 | } | |
109 | defer f.Close() | |
110 | err = json.NewDecoder(f).Decode(&data) | |
111 | if err != nil { | |
112 | panic(err) | |
113 | } | |
114 | return | |
115 | } | |
116 | ||
117 | /* This program operates by filling in a state table representing the best | |
118 | * possible trips you could make; the ones that makes you the most money. | |
119 | * This is feasible because we don't look at all the possible trips. | |
120 | * We define a list of things that are germane to this game and then only | |
121 | * consider the best outcome in each possible game state. | |
122 | * | |
123 | * Each cell in the table represents a state in the game. In each cell, | |
124 | * we track two things: 1. the most money you could possibly have while in | |
125 | * that state and 2. one possible way to get into that state with that | |
126 | * amount of money. | |
127 | * | |
128 | * A basic analysis can be done with a two-dimensional table: location and | |
129 | * fuel. planeteer-1.0 used this two-dimensional table. This version | |
130 | * adds features mostly by adding dimensions to this table. | |
131 | * | |
132 | * Note that the sizes of each dimension are data driven. Many dimensions | |
133 | * collapse to one possible value (ie, disappear) if the corresponding | |
134 | * feature is not enabled. | |
135 | * | |
136 | * The order of the dimensions in the list of constants below determines | |
137 | * their layout in RAM. The cargo-based 'dimensions' are not completely | |
138 | * independent -- some combinations are illegal and not used. They are | |
139 | * handled as three dimensions rather than one for simplicity. Placing | |
140 | * these dimensions first causes the unused cells in the table to be | |
141 | * grouped together in large blocks. This keeps them from polluting | |
142 | * cache lines, and if they are large enough, prevent the memory manager | |
143 | * from allocating pages for these areas at all. | |
144 | * | |
145 | * If the table gets too big to fit in RAM: | |
146 | * * Combine the Edens, Cloaks, and UnusedCargo dimensions. Of the | |
147 | * 24 combinations, only 15 are legal: a 38% savings. | |
148 | * * Reduce the size of the Fuel dimension to 3. We only ever look | |
149 | * backwards 2 units, so just rotate the logical values through | |
150 | * the same 3 physical addresses. This is good for an 82% savings. | |
151 | * * Reduce the size of the Edens dimension from 3 to 2, for the | |
152 | * same reasons as Fuel above. 33% savings. | |
153 | * * Buy more ram. (Just sayin'. It's cheaper than you think.) | |
154 | * | |
155 | */ | |
156 | ||
157 | // The official list of dimensions: | |
158 | const ( | |
159 | // Name Num Size Description | |
160 | Edens = iota // 1 3 # of Eden warp units (0 - 2 typically) | |
161 | Cloaks // 2 2 # of Devices of Cloaking (0 or 1) | |
162 | UnusedCargo // 3 4 # of unused cargo spaces (0 - 3 typically) | |
163 | Fuel // 4 17 Reactor power left (0 - 16) | |
164 | Location // 5 26 Location (which planet) | |
165 | Hold // 6 15 Cargo bay contents (a *Commodity or nil) | |
166 | NeedFighters // 7 2 Errand: Buy fighter drones (needed or not) | |
167 | NeedShields // 8 2 Errand: Buy shield batteries (needed or not) | |
168 | Visit // 9 2**N Visit: Stop by these N planets in the route | |
169 | ||
170 | NumDimensions | |
171 | ) | |
172 | ||
173 | func bint(b bool) int { | |
174 | if b { | |
175 | return 1 | |
176 | } | |
177 | return 0 | |
178 | } | |
179 | ||
180 | func DimensionSizes(data planet_data) []int { | |
181 | eden_capacity := data.Commodities["Eden Warp Units"].Limit | |
182 | if *start_edens > eden_capacity { | |
183 | eden_capacity = *start_edens | |
184 | } | |
185 | cloak_capacity := bint(*cloak) | |
186 | dims := make([]int, NumDimensions) | |
187 | dims[Edens] = eden_capacity + 1 | |
188 | dims[Cloaks] = cloak_capacity + 1 | |
189 | dims[UnusedCargo] = eden_capacity + cloak_capacity + 1 | |
190 | dims[Fuel] = *fuel + 1 | |
191 | dims[Location] = len(data.Planets) | |
192 | dims[Hold] = len(data.Commodities) + 1 | |
193 | dims[NeedFighters] = bint(*drones > 0) + 1 | |
194 | dims[NeedShields] = bint(*batteries > 0) + 1 | |
195 | dims[Visit] = 1 << uint(len(visit())) | |
196 | ||
197 | // Remind myself to add a line above when adding new dimensions | |
198 | for i, dim := range dims { | |
199 | if dim < 1 { | |
200 | panic(i) | |
201 | } | |
202 | } | |
203 | return dims | |
204 | } | |
205 | ||
206 | func StateTableSize(dims []int) int { | |
207 | product := 1 | |
208 | for _, size := range dims { | |
209 | product *= size | |
210 | } | |
211 | return product | |
212 | } | |
213 | ||
214 | type State struct { | |
215 | value, from int | |
216 | } | |
217 | ||
218 | func EncodeIndex(dims, addr []int) int { | |
219 | index := addr[0] | |
220 | if addr[0] > dims[0] { | |
221 | panic(0) | |
222 | } | |
223 | for i := 1; i < NumDimensions; i++ { | |
224 | if addr[i] > dims[i] { | |
225 | panic(i) | |
226 | } | |
227 | index = index*dims[i] + addr[i] | |
228 | } | |
229 | return index | |
230 | } | |
231 | ||
232 | func DecodeIndex(dims []int, index int) []int { | |
233 | addr := make([]int, NumDimensions) | |
234 | for i := NumDimensions - 1; i > 0; i-- { | |
235 | addr[i] = index % dims[i] | |
236 | index /= dims[i] | |
237 | } | |
238 | addr[0] = index | |
239 | return addr | |
240 | } | |
241 | ||
242 | func InitializeStateTable(data planet_data, dims []int) []State { | |
243 | table := make([]State, StateTableSize(dims)) | |
244 | ||
245 | addr := make([]int, NumDimensions) | |
246 | addr[Fuel] = *fuel | |
247 | addr[Edens] = *start_edens | |
248 | addr[Location] = data.p2i[*start] | |
249 | table[EncodeIndex(dims, addr)].value = *funds | |
250 | ||
251 | return table | |
252 | } | |
253 | ||
254 | /* These four fill procedures fill in the cell at address addr by | |
255 | * looking at all the possible ways to reach this cell and selecting | |
256 | * the best one. | |
257 | * | |
258 | * The other obvious implementation choice is to do this the other way | |
259 | * around -- for each cell, conditionally overwrite all the other cells | |
260 | * that are reachable *from* the considered cell. We choose gathering | |
261 | * reads over scattering writes to avoid having to take a bunch of locks. | |
262 | */ | |
263 | ||
264 | func UpdateCell(table []State, here, there, value_difference int) { | |
265 | possible_value := table[there].value + value_difference | |
266 | if table[there].value > 0 && possible_value > table[here].value { | |
267 | table[here].value = possible_value | |
268 | table[here].from = there | |
269 | } | |
270 | } | |
271 | ||
272 | func FillCellByArriving(data planet_data, dims []int, table []State, addr []int) { | |
273 | my_index := EncodeIndex(dims, addr) | |
274 | other := make([]int, NumDimensions) | |
275 | copy(other, addr) | |
276 | ||
277 | /* Travel here via a 2-fuel unit jump */ | |
278 | if addr[Fuel]+2 < dims[Fuel] { | |
279 | other[Fuel] = addr[Fuel] + 2 | |
280 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
281 | UpdateCell(table, my_index, EncodeIndex(dims, other), 0) | |
282 | } | |
283 | other[Location] = addr[Location] | |
284 | other[Fuel] = addr[Fuel] | |
285 | } | |
286 | ||
287 | /* Travel here via a hidey hole */ | |
288 | if addr[Fuel]+1 < dims[Fuel] { | |
289 | hole_index := (dims[Fuel] - 1) - (addr[Fuel] + 1) | |
290 | if hole_index < len(flight_plan()) && addr[Location] == data.p2i[flight_plan()[hole_index]] { | |
291 | other[Fuel] = addr[Fuel] + 1 | |
292 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
293 | UpdateCell(table, my_index, EncodeIndex(dims, other), 0) | |
294 | } | |
295 | other[Location] = addr[Location] | |
296 | other[Fuel] = addr[Fuel] | |
297 | } | |
298 | } | |
299 | ||
300 | /* Travel here via Eden Warp Unit */ | |
301 | for other[Edens] = addr[Edens] + 1; other[Edens] < dims[Edens]; other[Edens]++ { | |
302 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
303 | UpdateCell(table, my_index, EncodeIndex(dims, other), 0) | |
304 | } | |
305 | } | |
306 | other[Location] = addr[Location] | |
307 | other[Edens] = addr[Edens] | |
308 | } | |
309 | ||
310 | func FillCellBySelling(data planet_data, dims []int, table []State, addr []int) { | |
311 | if addr[Hold] > 0 { | |
312 | // Can't sell and still have cargo | |
313 | return | |
314 | } | |
315 | if addr[UnusedCargo] > 0 { | |
316 | // Can't sell everything and still have 'unused' holds | |
317 | return | |
318 | } | |
319 | my_index := EncodeIndex(dims, addr) | |
320 | other := make([]int, NumDimensions) | |
321 | copy(other, addr) | |
322 | planet := data.i2p[addr[Location]] | |
323 | for other[Hold] = 0; other[Hold] < dims[Hold]; other[Hold]++ { | |
324 | commodity := data.i2c[other[Hold]] | |
325 | if !data.Commodities[commodity].CanSell { | |
326 | // TODO: Dump cargo | |
327 | continue | |
328 | } | |
329 | relative_price, available := data.Planets[planet].RelativePrices[commodity] | |
330 | if !available { | |
331 | continue | |
332 | } | |
333 | base_price := data.Commodities[commodity].BasePrice | |
334 | absolute_price := float64(base_price) * float64(relative_price) / 100.0 | |
335 | sell_price := int(absolute_price * 0.9) | |
336 | ||
337 | for other[UnusedCargo] = 0; other[UnusedCargo] < dims[UnusedCargo]; other[UnusedCargo]++ { | |
338 | ||
339 | quantity := *hold - (other[UnusedCargo] + other[Cloaks] + other[Edens]) | |
340 | sale_value := quantity * sell_price | |
341 | UpdateCell(table, my_index, EncodeIndex(dims, other), sale_value) | |
342 | } | |
343 | } | |
344 | other[UnusedCargo] = addr[UnusedCargo] | |
345 | } | |
346 | ||
347 | func FillCellByBuying(data planet_data, dims []int, table []State, addr []int) { | |
348 | if addr[Hold] == 0 { | |
349 | // Can't buy and then have nothing | |
350 | return | |
351 | } | |
352 | my_index := EncodeIndex(dims, addr) | |
353 | other := make([]int, NumDimensions) | |
354 | copy(other, addr) | |
355 | planet := data.i2p[addr[Location]] | |
356 | commodity := data.i2c[addr[Hold]] | |
357 | if !data.Commodities[commodity].CanSell { | |
358 | return | |
359 | } | |
360 | relative_price, available := data.Planets[planet].RelativePrices[commodity] | |
361 | if !available { | |
362 | return | |
363 | } | |
364 | base_price := data.Commodities[commodity].BasePrice | |
365 | absolute_price := int(float64(base_price) * float64(relative_price) / 100.0) | |
366 | quantity := *hold - (addr[UnusedCargo] + addr[Cloaks] + addr[Edens]) | |
367 | total_price := quantity * absolute_price | |
368 | other[Hold] = 0 | |
369 | other[UnusedCargo] = 0 | |
370 | UpdateCell(table, my_index, EncodeIndex(dims, other), -total_price) | |
371 | other[UnusedCargo] = addr[UnusedCargo] | |
372 | other[Hold] = addr[Hold] | |
373 | } | |
374 | ||
375 | func FillCellByMisc(data planet_data, dims []int, table []State, addr []int) { | |
376 | my_index := EncodeIndex(dims, addr) | |
377 | other := make([]int, NumDimensions) | |
378 | copy(other, addr) | |
379 | /* Buy Eden warp units */ | |
380 | /* Buy a Device of Cloaking */ | |
381 | if addr[Cloaks] == 1 && addr[UnusedCargo] < dims[UnusedCargo]-1 { | |
382 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Device Of Cloakings"] | |
383 | if available { | |
384 | absolute_price := int(float64(data.Commodities["Device Of Cloakings"].BasePrice) * float64(relative_price) / 100.0) | |
385 | other[Cloaks] = 0 | |
386 | if other[Hold] != 0 { | |
387 | other[UnusedCargo] = addr[UnusedCargo] + 1 | |
388 | } | |
389 | UpdateCell(table, my_index, EncodeIndex(dims, other), -absolute_price) | |
390 | other[UnusedCargo] = addr[UnusedCargo] | |
391 | other[Cloaks] = addr[Cloaks] | |
392 | } | |
393 | } | |
394 | /* Silly: Dump a Device of Cloaking */ | |
395 | /* Buy Fighter Drones */ | |
396 | /* Buy Shield Batteries */ | |
397 | /* Visit this planet */ | |
398 | } | |
399 | ||
400 | func FillStateTable2Iteration(data planet_data, dims []int, table []State, | |
401 | addr []int, f func(planet_data, []int, []State, []int)) { | |
402 | /* TODO: Justify the safety of the combination of this dimension | |
403 | * iteration and the various phases f. */ | |
404 | for addr[Hold] = 0; addr[Hold] < dims[Hold]; addr[Hold]++ { | |
405 | for addr[Cloaks] = 0; addr[Cloaks] < dims[Cloaks]; addr[Cloaks]++ { | |
406 | for addr[UnusedCargo] = 0; addr[UnusedCargo] < dims[UnusedCargo]; addr[UnusedCargo]++ { | |
407 | for addr[NeedFighters] = 0; addr[NeedFighters] < dims[NeedFighters]; addr[NeedFighters]++ { | |
408 | for addr[NeedShields] = 0; addr[NeedShields] < dims[NeedShields]; addr[NeedShields]++ { | |
409 | for addr[Visit] = 0; addr[Visit] < dims[Visit]; addr[Visit]++ { | |
410 | f(data, dims, table, addr) | |
411 | } | |
412 | } | |
413 | } | |
414 | } | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | func FillStateTable2(data planet_data, dims []int, table []State, | |
420 | fuel_remaining, edens_remaining int, planet string, barrier chan<- bool) { | |
421 | addr := make([]int, len(dims)) | |
422 | addr[Edens] = edens_remaining | |
423 | addr[Fuel] = fuel_remaining | |
424 | addr[Location] = data.p2i[planet] | |
425 | FillStateTable2Iteration(data, dims, table, addr, FillCellByArriving) | |
426 | FillStateTable2Iteration(data, dims, table, addr, FillCellBySelling) | |
427 | FillStateTable2Iteration(data, dims, table, addr, FillCellByBuying) | |
428 | FillStateTable2Iteration(data, dims, table, addr, FillCellByMisc) | |
429 | barrier <- true | |
430 | } | |
431 | ||
432 | /* Filling the state table is a set of nested for loops NumDimensions deep. | |
433 | * We split this into two procedures: 1 and 2. #1 is the outer, slowest- | |
434 | * changing indexes. #1 fires off many calls to #2 that run in parallel. | |
435 | * The order of the nesting of the dimensions, the order of iteration within | |
436 | * each dimension, and where the 1 / 2 split is placed are carefully chosen | |
437 | * to make this arrangement safe. | |
438 | * | |
439 | * Outermost two layers: Go from high-energy states (lots of fuel, edens) to | |
440 | * low-energy state. These must be processed sequentially and in this order | |
441 | * because you travel through high-energy states to get to the low-energy | |
442 | * states. | |
443 | * | |
444 | * Third layer: Planet. This is a good layer to parallelize on. There's | |
445 | * high enough cardinality that we don't have to mess with parallelizing | |
446 | * multiple layers for good utilization (on 2011 machines). Each thread | |
447 | * works on one planet's states and need not synchronize with peer threads. | |
448 | */ | |
449 | func FillStateTable1(data planet_data, dims []int, table []State) { | |
450 | barrier := make(chan bool, len(data.Planets)) | |
451 | eden_capacity := data.Commodities["Eden Warp Units"].Limit | |
452 | work_units := (float64(*fuel) + 1) * (float64(eden_capacity) + 1) | |
453 | work_done := 0.0 | |
454 | for fuel_remaining := *fuel; fuel_remaining >= 0; fuel_remaining-- { | |
455 | for edens_remaining := eden_capacity; edens_remaining >= 0; edens_remaining-- { | |
456 | for planet := range data.Planets { | |
457 | go FillStateTable2(data, dims, table, fuel_remaining, | |
458 | edens_remaining, planet, barrier) | |
459 | } | |
460 | for _ = range data.Planets { | |
461 | <-barrier | |
462 | } | |
463 | work_done++ | |
464 | print(fmt.Sprintf("\r%3.0f%%", 100*work_done/work_units)) | |
465 | } | |
466 | } | |
467 | print("\n") | |
468 | } | |
469 | ||
470 | func FindBestState(data planet_data, dims []int, table []State) int { | |
471 | addr := make([]int, NumDimensions) | |
472 | addr[Edens] = *end_edens | |
473 | addr[Cloaks] = dims[Cloaks] - 1 | |
474 | addr[NeedFighters] = dims[NeedFighters] - 1 | |
475 | addr[NeedShields] = dims[NeedShields] - 1 | |
476 | addr[Visit] = dims[Visit] - 1 | |
477 | // Fuel, Hold, UnusedCargo left at 0 | |
478 | max_index := -1 | |
479 | max_value := 0 | |
480 | for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ { | |
481 | index := EncodeIndex(dims, addr) | |
482 | if table[index].value > max_value { | |
483 | max_value = table[index].value | |
484 | max_index = index | |
485 | } | |
486 | } | |
487 | return max_index | |
488 | } | |
489 | ||
490 | func Commas(n int) (s string) { | |
491 | r := n % 1000 | |
492 | n /= 1000 | |
493 | for n > 0 { | |
494 | s = fmt.Sprintf(",%03d", r) + s | |
495 | r = n % 1000 | |
496 | n /= 1000 | |
497 | } | |
498 | s = fmt.Sprint(r) + s | |
499 | return | |
500 | } | |
501 | ||
502 | func DescribePath(data planet_data, dims []int, table []State, start int) (description []string) { | |
503 | for index := start; index > 0 && table[index].from > 0; index = table[index].from { | |
504 | line := fmt.Sprintf("%13v", Commas(table[index].value)) | |
505 | addr := DecodeIndex(dims, index) | |
506 | prev := DecodeIndex(dims, table[index].from) | |
507 | if addr[Location] != prev[Location] { | |
508 | from := data.i2p[prev[Location]] | |
509 | to := data.i2p[addr[Location]] | |
510 | if addr[Fuel] != prev[Fuel] { | |
511 | line += fmt.Sprintf(" Jump from %v to %v (%v reactor units)", from, to, prev[Fuel]-addr[Fuel]) | |
512 | } else if addr[Edens] != prev[Edens] { | |
513 | line += fmt.Sprintf(" Eden warp from %v to %v", from, to) | |
514 | } else { | |
515 | panic("Traveling without fuel?") | |
516 | } | |
517 | } | |
518 | if addr[Hold] != prev[Hold] { | |
519 | if addr[Hold] == 0 { | |
520 | quantity := *hold - (prev[UnusedCargo] + prev[Edens] + prev[Cloaks]) | |
521 | line += fmt.Sprintf(" Sell %v %v", quantity, data.i2c[prev[Hold]]) | |
522 | } else if prev[Hold] == 0 { | |
523 | quantity := *hold - (addr[UnusedCargo] + addr[Edens] + addr[Cloaks]) | |
524 | line += fmt.Sprintf(" Buy %v %v", quantity, data.i2c[addr[Hold]]) | |
525 | } else { | |
526 | panic("Switched cargo?") | |
527 | } | |
528 | ||
529 | } | |
530 | if addr[Cloaks] == 1 && prev[Cloaks] == 0 { | |
531 | // TODO: Dump cloaks, convert from cargo? | |
532 | line += " Buy a Cloak" | |
533 | } | |
534 | description = append(description, line) | |
535 | } | |
536 | return | |
537 | } | |
538 | ||
539 | // (Example of a use case for generics in Go) | |
540 | func IndexPlanets(m *map[string]Planet, start_at int) (map[string]int, []string) { | |
541 | e2i := make(map[string]int, len(*m)+start_at) | |
542 | i2e := make([]string, len(*m)+start_at) | |
543 | i := start_at | |
544 | for e := range *m { | |
545 | e2i[e] = i | |
546 | i2e[i] = e | |
547 | i++ | |
548 | } | |
549 | return e2i, i2e | |
550 | } | |
551 | func IndexCommodities(m *map[string]Commodity, start_at int) (map[string]int, []string) { | |
552 | e2i := make(map[string]int, len(*m)+start_at) | |
553 | i2e := make([]string, len(*m)+start_at) | |
554 | i := start_at | |
555 | for e := range *m { | |
556 | e2i[e] = i | |
557 | i2e[i] = e | |
558 | i++ | |
559 | } | |
560 | return e2i, i2e | |
561 | } | |
562 | ||
563 | func main() { | |
564 | flag.Parse() | |
565 | data := ReadData() | |
566 | data.p2i, data.i2p = IndexPlanets(&data.Planets, 0) | |
567 | data.c2i, data.i2c = IndexCommodities(&data.Commodities, 1) | |
568 | dims := DimensionSizes(data) | |
569 | table := InitializeStateTable(data, dims) | |
570 | FillStateTable1(data, dims, table) | |
571 | best := FindBestState(data, dims, table) | |
572 | if best == -1 { | |
573 | print("Cannot acheive success criteria\n") | |
574 | } else { | |
575 | description := DescribePath(data, dims, table, best) | |
576 | for i := len(description) - 1; i >= 0; i-- { | |
577 | fmt.Println(description[i]) | |
578 | } | |
579 | } | |
580 | } |