-
Notifications
You must be signed in to change notification settings - Fork 0
/
ballclock.go
78 lines (58 loc) · 2.27 KB
/
ballclock.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
/*
BallClock Main Package
Brady Clifford - July 26, 2015
Summary:
Simulation of a ball-clock that outputs the number of days which elapse before the clock completly cycles through and resets to its orginal state.
Program computes the time before repetition, which varies according to the total number of balls present.
-balls: must be between 27 and 127
-minutes: must be a positive integer between 0 to 3477600.
Max balls allowed are 127, takes 2415 days to cycle the clock, there are 3477600 minutes in 2415 days.
*/
package main
import (
"fmt"
"os"
"flag"
"BallClock/control"
)
// Static paramater defaults
const MAX_BALLS = 127
const MIN_BALLS = 27
const MAX_MINUTES_TO_RUN = 3477600
// Gets the paramaters from the command line
// Returns the number of balls to cycle and the number of minutes to run
// Used the flag package because it already handles command input and type validation
func parseCommandLine() (int, int) {
// If no ball count specified, defaults MINBALLS
ballCount := flag.Int("balls", 0, fmt.Sprintf("Number of balls to cycle through. Must be between %d and %d.", MIN_BALLS, MAX_BALLS)) // Make sure not an negative value
// If no minutes to Run, defaults to 0
minutesToRun := flag.Int("minutes", 0, "Number of minutes to run.") // Make sure not an negative value
flag.Parse()
// Ball count must be between the specific range
if *ballCount < MIN_BALLS || *ballCount > MAX_BALLS {
fmt.Printf("-ball must be between %v and %v. Use attribute -h for help.\n", MIN_BALLS, MAX_BALLS)
os.Exit(1)
}
// Ball count must be between the specific range
if *minutesToRun < 0 || *minutesToRun > MAX_MINUTES_TO_RUN {
fmt.Printf("-minutes must be between 0 and %v. Use attribute -h for help.\n", MAX_MINUTES_TO_RUN)
os.Exit(1)
}
return *ballCount, *minutesToRun
}
func main() {
// Get the paramaters from the command line
ballCount, minutesToRun := parseCommandLine();
// Run the clock cycle simulation
totalDays, err := control.Run(ballCount, minutesToRun)
var message string
// Determine which message to display
if err != nil {
message = fmt.Sprintf("%v", err)
} else if minutesToRun > 0 {
message = control.GetCurrentStateString()
} else {
message = fmt.Sprintf("%d balls cycle after %d days.", ballCount, totalDays)
}
fmt.Println(message)
}