-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.coffee
125 lines (114 loc) · 2.3 KB
/
21.coffee
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
_log = require 'ololog'
{permute} = require './util'
boss_spec = '''
Hit Points: 103
Damage: 9
Armor: 2
'''
shop_spec = '''
Weapons: Cost Damage Armor
Dagger 8 4 0
Shortsword 10 5 0
Warhammer 25 6 0
Longsword 40 7 0
Greataxe 74 8 0
Armor: Cost Damage Armor
Leather 13 0 1
Chainmail 31 0 2
Splintmail 53 0 3
Bandedmail 75 0 4
Platemail 102 0 5
Rings: Cost Damage Armor
Damage +1 25 1 0
Damage +2 50 2 0
Damage +3 100 3 0
Defense +1 20 0 1
Defense +2 40 0 2
Defense +3 80 0 3
'''
fight = ( player, boss )->
pa = Math.max 1, player.dmg - boss.def
ba = Math.max 1, boss.dmg - player.def
hp = player.hp
bhp = boss.hp
step = 0
#_log step, hp, bhp
while hp>0 and bhp>0
if ++step%2
bhp -= pa
else
hp -= ba
#_log step, hp, bhp
hp>0
equip = ( pl, eq )->
pl.hp = 100
pl.cost = 0
pl.dmg = 0
pl.def = 0
for it in eq
pl.cost += it[0]
pl.dmg += it[1]
pl.def += it[2]
pl.cost
find_min_gold_to_win = ( shop, boss )->
min = Infinity
player = {}
permute.n_of 1, shop.weapons, (w)->
permute.minmax_of 0,1, shop.armors, (a)->
permute.minmax_of 0,2, shop.rings, (r)->
cost = equip player, cfg = w.concat a.concat r
if cost < min and fight player, boss
_log.yellow cost, cfg
min = cost
return
min
find_max_gold_to_lose = ( shop, boss )->
max = 0
player = {}
permute.n_of 1, shop.weapons, (w)->
permute.minmax_of 0,1, shop.armors, (a)->
permute.minmax_of 0,2, shop.rings, (r)->
cost = equip player, cfg = w.concat a.concat r
if cost > max and not fight player, boss
_log.yellow cost, cfg
max = cost
return
max
do ->
try
boss =
hp: 103
dmg: 9
def: 2
test =
hp:100
dmg:9
def:0
#_log.cyan fight test,boss
shop =
weapons: [
[8, 4, 0]
[10, 5, 0]
[25, 6, 0]
[40, 7, 0]
[74, 8, 0]
]
armors: [
[13, 0, 1]
[31, 0, 2]
[53, 0, 3]
[75, 0, 4]
[102, 0, 5]
]
rings: [
[25, 1, 0]
[50, 2, 0]
[100, 3, 0]
[20, 0, 1]
[40, 0, 2]
[80, 0, 3]
]
_log.green find_min_gold_to_win shop, boss
_log.green find_max_gold_to_lose shop, boss
catch e
_log.red e