Commit
+38 -17 +/-3 browse
1 | diff --git a/graph.go b/graph.go |
2 | index de31843..213139e 100644 |
3 | --- a/graph.go |
4 | +++ b/graph.go |
5 | @@ -7,14 +7,6 @@ import ( |
6 | "gonum.org/v1/gonum/graph/simple" |
7 | ) |
8 | |
9 | - func SumWeights(route Route, weighters ...Weighter) float64 { |
10 | - var values []float64 |
11 | - for _, weighter := range weighters { |
12 | - values = append(values, weighter(route)) |
13 | - } |
14 | - return floats.Sum(values) |
15 | - } |
16 | - |
17 | // Weighter evaluates the weight of a route |
18 | // based on some criteria |
19 | type Weighter func(Route) float64 |
20 | @@ -33,7 +25,11 @@ func Load(airports AirportMap, routes []Route, weighters ...Weighter) graph.Grap |
21 | g.AddNode(airport) |
22 | } |
23 | for _, route := range routes { |
24 | - g.SetWeightedEdge(SetWeight(route, SumWeights(route, weighters...))) |
25 | + var values []float64 |
26 | + for _, weighter := range weighters { |
27 | + values = append(values, weighter(route)) |
28 | + } |
29 | + g.SetWeightedEdge(SetWeight(route, floats.Sum(values))) |
30 | } |
31 | return g |
32 | } |
33 | diff --git a/main.go b/main.go |
34 | index 97e9c1b..aed1ac7 100644 |
35 | --- a/main.go |
36 | +++ b/main.go |
37 | @@ -8,7 +8,7 @@ import ( |
38 | ) |
39 | |
40 | func main() { |
41 | - app := cli.App("flights", "✈️ ✈️ ✈️ ✈️ ✈️ ") |
42 | + app := cli.App("flights", " ✈️ Find The Best Possible Flight ✈️ ") |
43 | |
44 | app.Command("airports", "list all possible airports", func(cmd *cli.Cmd) { |
45 | cmd.Action = func() { |
46 | @@ -27,6 +27,37 @@ func main() { |
47 | } |
48 | }) |
49 | |
50 | + app.Command("furthest", "show the furthest distance you can travel", func(cmd *cli.Cmd) { |
51 | + var ( |
52 | + departureCode = cmd.StringArg("DEPARTURE", "", "starting airport") |
53 | + ) |
54 | + cmd.Action = func() { |
55 | + airports := Airports() |
56 | + start := airports.Airport(*departureCode) |
57 | + end := start |
58 | + for _, other := range airports { |
59 | + if GetDistance(start, other) > GetDistance(start, end) { |
60 | + end = other |
61 | + } |
62 | + } |
63 | + fmt.Println(Itinerary{weight: GetDistance(start, end), stops: []Airport{start, end}}) |
64 | + } |
65 | + }) |
66 | + |
67 | + app.Command("nearby", "show airports nearby", func(cmd *cli.Cmd) { |
68 | + var ( |
69 | + threshold = cmd.IntOpt("t threshold", 500, "distance from starting airport") |
70 | + departureCode = cmd.StringArg("DEPARTURE", "", "starting airport") |
71 | + ) |
72 | + cmd.Action = func() { |
73 | + airports := Airports() |
74 | + start := airports.Airport(*departureCode) |
75 | + for _, end := range NearBy(float64(*threshold), start, airports) { |
76 | + fmt.Println(Itinerary{weight: GetDistance(start, end), stops: []Airport{start, end}}) |
77 | + } |
78 | + } |
79 | + }) |
80 | + |
81 | app.Command("route", "find the best possible routes", func(cmd *cli.Cmd) { |
82 | cmd.Spec = "[OPTIONS] DEPARTURE ARRIVAL" |
83 | var ( |
84 | @@ -36,12 +67,6 @@ func main() { |
85 | ) |
86 | cmd.Action = func() { |
87 | airports := Airports() |
88 | - if !airports.HasCode(*arrivalCode) { |
89 | - maybe(fmt.Errorf("bad arrival code: %s", *arrivalCode)) |
90 | - } |
91 | - if !airports.HasCode(*departureCode) { |
92 | - maybe(fmt.Errorf("bad departure code: %s", *departureCode)) |
93 | - } |
94 | //shortest, weight := Load(airports.Airport(*departureCode), airports.Airport(*arrivalCode), opts) |
95 | fmt.Println(Find( |
96 | airports.Airport(*departureCode), |
97 | diff --git a/util.go b/util.go |
98 | index ba3c08b..d2bd768 100644 |
99 | --- a/util.go |
100 | +++ b/util.go |
101 | @@ -26,7 +26,7 @@ func GetDistance(start, dest Airport) float64 { |
102 | return Haversine([2]float64{start.Latitude, start.Longitude}, [2]float64{dest.Latitude, dest.Longitude}) |
103 | } |
104 | |
105 | - func NearBy(threshold float64, start Airport, airports map[string]Airport) []Airport { |
106 | + func NearBy(threshold float64, start Airport, airports AirportMap) []Airport { |
107 | nearby := []Airport{} |
108 | for _, dest := range airports { |
109 | if GetDistance(start, dest) <= threshold { |