|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.buddy.wtf/challenges/buckets-of-fun/lib/bucket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// App holds context for the application
|
|
|
|
type App struct {
|
|
|
|
Target uint64
|
|
|
|
Buckets bucket.Buckets
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns an instance of the app
|
|
|
|
func New(args ...string) (*App, error) {
|
|
|
|
if len(args) < 2 {
|
|
|
|
msg := fmt.Sprintf("usage error: requires at least 2 arguments, received %d", len(args))
|
|
|
|
return nil, errors.New(msg)
|
|
|
|
}
|
|
|
|
target, err := strconv.ParseUint(args[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("target must be a positive integer: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
app := App{
|
|
|
|
Target: target,
|
|
|
|
Buckets: bucket.Buckets{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arg := range args[1:] {
|
|
|
|
capacity, err := strconv.ParseUint(arg, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("bucket capacity must be a positive integer: " + err.Error())
|
|
|
|
}
|
|
|
|
app.Buckets = append(app.Buckets, bucket.Bucket{Capacity: capacity})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the application
|
|
|
|
func (a *App) Run() error {
|
|
|
|
fmt.Printf("target: %d\n", a.Target)
|
|
|
|
fmt.Printf("buckets: %s\n", a.Buckets.String())
|
|
|
|
for _, solution := range a.Buckets.FindTarget((a.Target)) {
|
|
|
|
fmt.Println(solution)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|