Commits

Miki Tebeka  committed 412137d

Running

  • Participants
  • Parent commits c0ed146

Comments (0)

Files changed (1)

File go/src/euler/euler.go

 import (
 	"flag"
 	"fmt"
+	"os"
+	"sort"
+	"strconv"
 )
 
 type Solution struct {
 	fn   func()
 }
 
-var solutions = []Solution{}
+type Solutions []*Solution
+func (s Solutions) Len() int { return len(s) }
+func (s Solutions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+func (s Solutions) Less(i, j int) bool {
+	ielem, _ := strconv.Atoi(s[i].name)
+	jelem, _ := strconv.Atoi(s[j].name)
+	return ielem < jelem
+}
+
+
+var solutions = Solutions{}
 
 func register(name string, fn func()) {
-	solutions = append(solutions, Solution{name, fn})
+	solutions = append(solutions, &Solution{name, fn})
+	sort.Sort(&solutions)
+}
+
+func run(name string) error {
+	for _, sol := range solutions {
+		if sol.name == name {
+			sol.fn()
+			return nil
+		}
+	}
+
+	return fmt.Errorf("%s not found", name)
 }
 
 
 		}
 	}
 
+	for _, name := range flag.Args() {
+		err := run(name)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "error: %s\n", err)
+			os.Exit(1)
+		}
+	}
 }