-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
125 lines (103 loc) · 2.67 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"time"
)
type (
Bean int
GroundBean int
Water int
HotWater int
Coffee int
)
const (
GramBeans Bean = 1
GramGroundBeans GroundBean = 1
MilliLiterWater Water = 1
MilliLiterHotWater HotWater = 1
CupsCoffee Coffee = 1
)
func (w Water) String() string {
return fmt.Sprintf("%d[ml] water", int(w))
}
func (hw HotWater) String() string {
return fmt.Sprintf("%d[ml] hot water", int(hw))
}
func (b Bean) String() string {
return fmt.Sprintf("%d[g] beans", int(b))
}
func (gb GroundBean) String() string {
return fmt.Sprintf("%d[g] ground beans", int(gb))
}
func (cups Coffee) String() string {
return fmt.Sprintf("%d cup(s) coffee", int(cups))
}
// 1カップのコーヒーを淹れるのに必要な水の量
func (cups Coffee) Water() Water {
return Water(180*cups) / MilliLiterWater
}
// 1カップのコーヒーを淹れるのに必要なお湯の量
func (cups Coffee) HotWater() HotWater {
return HotWater(180*cups) / MilliLiterHotWater
}
// 1カップのコーヒーを淹れるのに必要な豆の量
func (cups Coffee) Beans() Bean {
return Bean(20*cups) / GramBeans
}
// 1カップのコーヒーを淹れるのに必要な粉の量
func (cups Coffee) GroundBeans() GroundBean {
return GroundBean(20*cups) / GramGroundBeans
}
// お湯を沸かす
func boil(water Water) HotWater {
time.Sleep(400 * time.Millisecond)
return HotWater(water)
}
// コーヒー豆を挽く
func grind(beans Bean) GroundBean {
time.Sleep(200 * time.Millisecond)
return GroundBean(beans)
}
// コーヒーを淹れる
func brew(hotWater HotWater, groundBeans GroundBean) Coffee {
time.Sleep(1 * time.Second)
// 少ない方を優先する
cups1 := Coffee(hotWater / (1 * CupsCoffee).HotWater())
cups2 := Coffee(groundBeans / (1 * CupsCoffee).GroundBeans())
if cups1 < cups2 {
return cups1
}
return cups2
}
func main() {
// 作るコーヒーの数
const amountCoffee = 20 * CupsCoffee
// 材料
water := amountCoffee.Water()
beans := amountCoffee.Beans()
fmt.Println(water)
fmt.Println(beans)
// お湯を沸かす
var hotWater HotWater
for water > 0 {
water -= 600 * MilliLiterWater
hotWater += boil(600 * MilliLiterWater)
}
fmt.Println(hotWater)
// 豆を挽く
var groundBeans GroundBean
for beans > 0 {
beans -= 20 * GramBeans
groundBeans += grind(20 * GramBeans)
}
fmt.Println(groundBeans)
// コーヒーを淹れる
var coffee Coffee
cups := 4 * CupsCoffee
for hotWater >= cups.HotWater() && groundBeans >= cups.GroundBeans() {
hotWater -= cups.HotWater()
groundBeans -= cups.GroundBeans()
coffee += brew(cups.HotWater(), cups.GroundBeans())
}
fmt.Println(coffee)
}