-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
182 lines (151 loc) · 3.76 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
package main
import (
"fmt"
"math"
"strings"
)
type Unit struct {
cost int
damage int
armor int
}
func fs1(bossHitPoints, bossDamage, bossArmor int, storeWeapons []Unit, storeArmors []Unit, storeRings []Unit) int {
equipments := allEquipments(storeWeapons, storeArmors, storeRings)
minGold := math.MaxInt
for _, equipment := range equipments {
playerDamage, playerArmor, gold := equipment.sum(0, 0)
if win(100, playerDamage, playerArmor, bossHitPoints, bossDamage, bossArmor) {
minGold = min(minGold, gold)
}
}
return minGold
}
type Equipment struct {
weapon Unit
armor *Unit
ring1 *Unit
ring2 *Unit
}
func (e Equipment) toString() string {
wb := strings.Builder{}
wb.WriteString(fmt.Sprintf("w=%v ", e.weapon.cost))
if e.armor != nil {
wb.WriteString(fmt.Sprintf(" a=%v", e.armor.cost))
}
if e.ring1 != nil {
wb.WriteString(fmt.Sprintf(" r1=%v", e.ring1.cost))
}
if e.ring2 != nil {
wb.WriteString(fmt.Sprintf(" r2=%v", e.ring2.cost))
}
return wb.String()
}
func (e Equipment) sum(sumDamage int, sumArmor int) (int, int, int) {
gold := 0
sumDamage += e.weapon.damage
sumArmor += e.weapon.armor
gold = e.weapon.cost
if e.armor != nil {
sumDamage += e.armor.damage
sumArmor += e.armor.armor
gold += e.armor.cost
}
if e.ring1 != nil {
sumDamage += e.ring1.damage
sumArmor += e.ring1.armor
gold += e.ring1.cost
}
if e.ring2 != nil {
sumDamage += e.ring2.damage
sumArmor += e.ring2.armor
gold += e.ring2.cost
}
return sumDamage, sumArmor, gold
}
func allEquipments(storeWeapons []Unit, storeArmors []Unit, storeRings []Unit) []Equipment {
var equipments []Equipment
for _, w := range storeWeapons {
// With armor
for _, a := range storeArmors {
a := a
equipments = append(equipments, all(w, &a, nil, nil, storeRings, 0)...)
}
equipments = append(equipments, all(w, nil, nil, nil, storeRings, 0)...)
}
return equipments
}
func all(weapon Unit, armor, ring1, ring2 *Unit, storeRings []Unit, storeIndex int) []Equipment {
if storeIndex == len(storeRings) {
return nil
}
if ring1 != nil && ring2 != nil {
return nil
}
var equipments []Equipment
// With
if ring1 == nil {
equipments = append(equipments, Equipment{
weapon: weapon,
armor: armor,
ring1: &storeRings[storeIndex],
})
equipments = append(equipments, all(weapon, armor, &storeRings[storeIndex], nil, storeRings, storeIndex+1)...)
} else {
equipments = append(equipments, Equipment{
weapon: weapon,
armor: armor,
ring1: ring1,
ring2: &storeRings[storeIndex],
})
equipments = append(equipments, all(weapon, armor, ring1, &storeRings[storeIndex], storeRings, storeIndex+1)...)
}
// Without
equipments = append(equipments, all(weapon, armor, ring1, ring2, storeRings, storeIndex+1)...)
return equipments
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func win(playerHitPoints, playerDamage, playerArmor, bossHitPoints, bossDamage, bossArmor int) bool {
pd := calcDamage(playerDamage, bossArmor)
bd := calcDamage(bossDamage, playerArmor)
for {
// Player
bossHitPoints -= pd
if bossHitPoints <= 0 {
return true
}
// Boss
playerHitPoints -= bd
if playerHitPoints <= 0 {
return false
}
}
}
func calcDamage(damage, armor int) int {
v := damage - armor
if v < 1 {
return 1
}
return v
}
func fs2(bossHitPoints, bossDamage, bossArmor int, storeWeapons []Unit, storeArmors []Unit, storeRings []Unit) int {
equipments := allEquipments(storeWeapons, storeArmors, storeRings)
maxGold := 0
for _, equipment := range equipments {
playerDamage, playerArmor, gold := equipment.sum(0, 0)
if !win(100, playerDamage, playerArmor, bossHitPoints, bossDamage, bossArmor) {
maxGold = max(maxGold, gold)
}
}
return maxGold
}
func max(a, b int) int {
if a > b {
return a
}
return b
}