-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
204 lines (180 loc) · 3.98 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"fmt"
"sort"
)
// import (
// "math"
// "sort"
// )
func main() {
picks := []int{1, 3, 2}
// picks := []int{0, 1, 1}
minerals := []string{"diamond", "diamond", "diamond", "iron", "iron", "diamond", "iron", "stone"}
// minerals := []string{"diamond", "diamond", "diamond", "diamond", "diamond", "iron", "iron", "iron", "iron", "iron", "diamond"}
answer := solution(picks, minerals)
fmt.Println(answer)
}
// // 입출력 예
// // picks minerals result
// // [1, 3, 2] ["diamond", "diamond", "diamond", "iron", "iron", "diamond", "iron", "stone"] 12
// // [0, 1, 1] ["diamond", "diamond", "diamond", "diamond", "diamond", "iron", "iron", "iron", "iron", "iron", "diamond"] 50
// // 사용할 곡괭이가 없거나 광산에 있는 모든 광물을 캘 때까지
// func mining() int {
// return 0
// }
// var dia = 25
// var iron = 4
// var stone = 0
// type SortM struct {
// weight int
// pos int
// }
// func solution(picks []int, minerals []string) int {
// // 곡괭이 개수
// pickCount := 0
// tmp := [][]string{}
// for _, s := range picks {
// pickCount += s
// }
// if len(minerals) > pickCount*5 {
// minerals = minerals[:(pickCount * 5)]
// }
// sortMineral := make(map[int]int)
// j := 1
// conv := 0
// for i, s := range minerals {
// if i == j*5 {
// j++
// }
// if s == "diamond" {
// conv = dia
// } else if s == "iron" {
// conv = iron
// } else if s == "stone" {
// conv = stone
// }
// sortMineral[j] = sortMineral[j] + conv
// }
// var sortM []SortM
// for k, v := range sortMineral {
// sortM = append(sortM, SortM{v, k})
// }
// sort.Slice(sortM, func(i, j int) bool {
// return sortM[i].weight > sortM[j].weight
// })
// for i := 0; i < int(math.Ceil(float64(len(minerals)/5))); i++ {
// tmp = append(tmp, minerals[i*5:i*5+5])
// }
// return 0
// }
// 정답
type Fatigue struct {
diamond int
iron int
stone int
}
func NewFatigue(mineral string) *Fatigue {
switch mineral {
case "diamond":
return &Fatigue{
diamond: 1,
iron: 5,
stone: 25,
}
case "iron":
return &Fatigue{
diamond: 1,
iron: 1,
stone: 5,
}
case "stone":
return &Fatigue{
diamond: 1,
iron: 1,
stone: 1,
}
default:
panic(mineral)
}
}
func (f *Fatigue) Add(other *Fatigue) *Fatigue {
return &Fatigue{
diamond: f.diamond + other.diamond,
iron: f.iron + other.iron,
stone: f.stone + other.stone,
}
}
func GetFatigues(minerals []string) []*Fatigue {
if len(minerals) == 0 {
return nil
}
var ret []*Fatigue
for i, mineral := range minerals {
if i%5 == 0 {
ret = append(ret, &Fatigue{})
}
window := ret[len(ret)-1]
fatigue := NewFatigue(mineral)
ret[len(ret)-1] = window.Add(fatigue)
}
if ret[len(ret)-1].diamond == 0 {
ret = ret[:len(ret)-1]
}
return ret
}
const (
DiaPick = iota + 1
IronPick
StonePick
)
func solution(pickCounts []int, minerals []string) int {
fatigues := GetFatigues(minerals)
picks := GetPicks(pickCounts)
min := Min(len(fatigues), len(picks))
fatigues = fatigues[:min]
picks = picks[:min]
var ret int
for len(picks) > 0 {
pick := picks[len(picks)-1]
picks = picks[:len(picks)-1]
switch pick {
case DiaPick:
sort.Slice(fatigues, func(i, j int) bool {
return fatigues[i].diamond < fatigues[j].diamond
})
ret += fatigues[0].diamond
case IronPick:
sort.Slice(fatigues, func(i, j int) bool {
return fatigues[i].iron < fatigues[j].iron
})
ret += fatigues[0].iron
case StonePick:
sort.Slice(fatigues, func(i, j int) bool {
return fatigues[i].stone < fatigues[j].stone
})
ret += fatigues[0].stone
}
fatigues = fatigues[1:]
}
return ret
}
func Min(a int, b int) int {
if a < b {
return a
}
return b
}
func GetPicks(counts []int) []int {
var ret []int
for pick, count := range counts {
ret = AppendPick(ret, pick+1, count)
}
return ret
}
func AppendPick(picks []int, pick int, count int) []int {
for i := 0; i < count; i++ {
picks = append(picks, pick)
}
return picks
}