-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
64 lines (57 loc) · 1.4 KB
/
common.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
type Ingredient struct {
Name string
Capacity int
Durability int
Flavor int
Texture int
Calories int
}
type Pair struct {
Ingredient Ingredient
Spoons int
}
func parse() []Ingredient {
ingredients := make([]Ingredient, 0)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" {
var i Ingredient
n, _ := fmt.Sscanf(line, "%s capacity %d, durability %d, flavor %d, texture %d, calories %d", &i.Name, &i.Capacity, &i.Durability, &i.Flavor, &i.Texture, &i.Calories)
if n != 6 {
log.Fatal("parse failed")
}
i.Name = strings.TrimRight(i.Name, ":")
ingredients = append(ingredients, i)
}
}
return ingredients
}
// Returns a read-only channel of all combinations of 100 spoons.
// It will be closed after it ends.
func Generate(ingredients []Ingredient) <-chan []Pair {
ch := make(chan []Pair)
go func() {
generator(ingredients, 100, nil, ch)
close(ch)
}()
return ch
}
// Recursive function to find all cases
func generator(ingredients []Ingredient, spoonLeft int, accumulator []Pair, ch chan<- []Pair) {
if len(ingredients) == 1 {
ch <- append(accumulator, Pair{ingredients[0], spoonLeft})
return
}
for a := 0; a <= spoonLeft; a++ {
generator(ingredients[1:], spoonLeft-a, append(accumulator, Pair{ingredients[0], a}), ch)
}
}