-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (91 loc) · 2.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"github.com/jfallis/collatz/pkg/collatz/extension/bruteforce"
"github.com/jfallis/collatz/pkg/collatz/extension"
"github.com/jfallis/collatz/pkg/collatz"
)
const argCount = 2
func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.Attr{}
}
return a
},
})))
var (
begin, end, size string
steps, logging, printAll bool
)
bruteforceFlags := flag.NewFlagSet("bruteforce", flag.ExitOnError)
bruteforceFlags.StringVar(&begin, "begin", "0", "define the start number")
bruteforceFlags.StringVar(&end, "end", "", "define the end number")
bruteforceFlags.StringVar(&size, "size", extension.CPUBatchSize(), "set the batch size")
bruteforceFlags.BoolVar(&printAll, "print-all", false, "print all steps, recommend logging to be enabled")
bruteforceFlags.BoolVar(&logging, "logging", true, "enable or disable logging")
bruteforceFlags.BoolVar(&steps, "steps", true, "enable or disable step collection, disable for performance improvement")
var number string
seedFlags := flag.NewFlagSet("seed", flag.ExitOnError)
seedFlags.StringVar(&number, "number", "", "define seed number")
seedFlags.BoolVar(&steps, "steps", true, "enable or disable step collection")
if len(os.Args) < argCount {
printUsage(nil)
return
}
switch os.Args[1] {
case "bruteforce":
if err := bruteforceFlags.Parse(os.Args[argCount:]); err != nil || end == "" {
printUsage(bruteforceFlags)
return
}
ctx := context.Background()
if _, err := bruteforce.Run(ctx, bruteforce.Request{
Start: begin,
End: end,
BatchSize: size,
Logging: logging,
EnableStep: steps,
PrintAll: printAll,
}); err != nil {
slog.Error(err.Error())
}
case "seed":
if err := seedFlags.Parse(os.Args[argCount:]); err != nil || number == "" {
printUsage(seedFlags)
return
}
collatzConjecture(number, steps)
}
}
func printUsage(flagSet *flag.FlagSet) {
fmt.Println("Collatz Conjecture")
fmt.Println("The Collatz Conjecture is the simplest math problem no one can solve " +
"- it is easy enough for almost anyone to understand but notoriously difficult to solve.")
fmt.Printf("\nUsage:\n")
fmt.Println(" collatz seed -number=9663")
fmt.Println(" collatz bruteforce -start=0 -end=100000")
if flagSet != nil {
fmt.Printf("\nOptions:\n")
flagSet.PrintDefaults()
}
fmt.Printf("\nHelp:\n")
fmt.Println(" collatz seed [--help | -h]")
fmt.Println(" collatz bruteforce [--help | -h]")
}
func collatzConjecture(n string, steps bool) {
c := collatz.New(n)
if err := c.Calculate(steps); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
slog.Info(c.String())
if steps {
slog.Info(fmt.Sprintf("Collatz sequence: %s", c.Steps().String()))
}
}