-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
593 lines (497 loc) · 21.4 KB
/
run.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import asyncio
async def my_input(prompt):
print(prompt)
while True:
await asyncio.sleep(0.1)
x = input()
if x:
return x
class Colors:
RESET = "\033[0m"
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
GREY = "\033[90m"
HELLO = r"""
_____ _
/ ____| | |
| (___ _ __ _____ _____ __ _ _ __ | |_
\___ \| '_ \ / _ \ \ /\ / / __|/ _` | '_ \| __|
____) | | | | (_) \ V V /\__ \ (_| | | | | |_
|_____/|_| |_|\___/ \_/\_/ |___/\__,_|_| |_|\__|
_____ _ _ _
/ ____| | | | | | |
| | __ _| | ___ _ _| | __ _| |_ ___ _ __
| | / _` | |/ __| | | | |/ _` | __/ _ \| '__|
| |___| (_| | | (__| |_| | | (_| | || (_) | |
\_____\__,_|_|\___|\__,_|_|\__,_|\__\___/|_|
"""
ALERT = r"""
=============================================================
| |
| 如果遇到一启动就 Connection closed 的情况,可以稍后重试 |
| |
============================================================="""
# print(Colors.RED + ALERT + Colors.RESET)
print(Colors.CYAN + HELLO + Colors.RESET)
async def input_int(prompt, valid=None):
while True:
try:
value = int(await my_input(prompt))
if isinstance(valid, (range, list)):
valid = tuple(valid)
if isinstance(valid, tuple):
assert value in valid, f"[!] 输入的值不在合法范围内:{valid}"
if isinstance(valid, int):
assert value % valid == 0, f"[!] 输入的值需要是 {valid} 的倍数。"
return value
except AssertionError as e:
print(Colors.RED + str(e) + Colors.RESET)
except Exception:
print(Colors.RED + "[!] 输入有误,请重新输入。" + Colors.RESET)
async def input_ints(prompt, nums, sum_to):
while True:
try:
values = tuple(map(int, (await my_input(prompt)).strip().split(" ")))
assert len(values) == nums, f"[!] 输入的数目不对,请重新输入。"
assert sum(values) == sum_to, f"[!] 进货总数量与顾客数量不符,请重新输入。"
return values
except AssertionError as e:
print(Colors.RED + str(e) + Colors.RESET)
except Exception:
print(Colors.RED + "[!] 输入有误,请重新输入。" + Colors.RESET)
good_stage = await input_int("[*] 今日经营的商品是哪个阶段的商品?(1-4) ", range(1, 5))
if good_stage == 1:
buy_drink = [20, 28, 38]
buy_snack = [10, 20, 32]
buy_token = [50, 100, 200]
sell_base_drink = 50
sell_base_snack = 40
sell_base_token = 1000
elif good_stage == 2:
buy_drink = [30, 42, 57]
buy_snack = [15, 30, 48]
buy_token = [50, 100, 200]
sell_base_drink = 100
sell_base_snack = 80
sell_base_token = 1000
elif good_stage == 3:
print(Colors.RED + "[!] 该商品种类的基础数据尚未添加。" + Colors.RESET)
exit()
elif good_stage == 4:
print(Colors.RED + "[!] 该商品种类的基础数据尚未添加。" + Colors.RESET)
exit()
customer_drink = await input_int("[*] 今日的饮品爱好者有多少?", 90)
customer_snack = await input_int("[*] 今日的餐点爱好者有多少?", 90)
customer_token = await input_int("[*] 今日的纪念品爱好者有多少?", 10)
customer = [customer_drink, customer_snack, customer_token]
buy_prices = [buy_drink, buy_snack, buy_token]
stock_drink = customer_drink
stock_snack = customer_snack
stock_token = customer_token
remain_me = await input_int("[*] 纪念品库存有多少(不知道的话填 0 即可)?")
remain_rival = await input_int("[*] 对方的纪念品库存有多少(不知道的话填 0 即可)?")
remain_strategy = await input_int("[*] 是否允许用更少的当日收入换取期望更多的未来收入?(0/1) ", (0, 1))
if remain_strategy:
print(Colors.RED + "[!] 请注意,后续的期望收益包括纪念品库存未来可能的收益。" + Colors.RESET)
# aaa = (30, 30, 30)
# aab = (36, 36, 18)
# abb = (36, 27, 27)
# abc = (40, 30, 20)
# aa = (5, 5)
# ab = (6, 4)
sell_drink = list(range(sell_base_drink - 5, sell_base_drink + 6, 1))
sell_snack = list(range(sell_base_snack - 5, sell_base_snack + 6, 1))
sell_token = list(range(sell_base_token - 50, sell_base_token + 51, 10))
sell = [sell_drink, sell_snack, sell_token]
future_value = (sell_token[5] + sell_token[4]) // 2
# ============================================================
from itertools import product
async def average_with_weight(pairs):
return sum([x[0] * x[-1] for x in pairs]) / sum([x[-1] for x in pairs])
# ============================================================
sell_result_cache = {}
async def sell_result(bid, me) -> int:
if (bid, me) in sell_result_cache:
return sell_result_cache[(bid, me)]
ranks = [sum([1 for y in bid if y < x]) for x in bid]
sorted_ranks = sorted(ranks)
if sorted_ranks == [0, 0, 0]:
ret = 30
elif sorted_ranks == [0, 0, 2]:
ret = [36, None, 18][ranks[bid.index(me)]]
elif sorted_ranks == [0, 1, 1]:
ret = [36, 27][ranks[bid.index(me)]]
elif sorted_ranks == [0, 1, 2]:
ret = [40, 30, 20][ranks[bid.index(me)]]
elif sorted_ranks == [0, 0]:
ret = 5
elif sorted_ranks == [0, 1]:
ret = [6, 4][ranks[bid.index(me)]]
else:
raise NotImplementedError
sell_result_cache[(bid, me)] = ret
return ret
async def sell_conflict_checker(statement, info) -> bool:
if len(info) == 0:
return True
if len(info) == 1:
return info == statement
if len(info) == 2:
if info == (None, None):
return True
elif None not in info:
return info == statement
elif info[0] == None:
return info[1] == statement[1] and info[1] >= statement[0]
elif info[1] == None:
return info[0] == statement[0] and info[0] >= statement[1]
else:
raise NotImplementedError
return False
sell_action_cache = {}
async def sell_action(gid, num, rival_num, info):
num += remain_me * (gid == 2)
if (gid, num, rival_num, info) in sell_action_cache:
return sell_action_cache[(gid, num, rival_num, info)]
# print(num, rival_num, info, gid)
statements = [
statement
for statement in product(range(11), repeat=[2, 2, 1][gid])
if await sell_conflict_checker(statement, info)
]
best_income, best_choice = -1e9, None
for c in range(11):
incomes = []
sameprice = None
for statement in statements:
customer_base = customer[gid] // [90, 90, 10][gid]
customer_num = customer_base * await sell_result(statement + (c,), c)
income = min(customer_num, num) * sell[gid][c]
income_rank = 0
for _c, _n in zip(statement, rival_num):
_n += remain_rival * (gid == 2)
rival_customer_num = customer_base * await sell_result(statement + (c,), _c)
rival_income = min(rival_customer_num, _n) * sell[gid][_c]
if rival_income > income:
income_rank += 1
# print(c, statement + (c,), sell_result(statement + (c,), c), sell[gid][c], income, income_rank)
if income_rank == 0:
income += 5000
elif income_rank == 1:
income += 2000
else:
income += 1000
if remain_strategy and gid == 2 and num > customer_num:
income += (num - customer_num) * future_value
incomes.append(income)
if len(info) == 2 and None in info and statement[0] == statement[1]:
sameprice = income
if sameprice is not None:
exp_income = (sum(incomes) - 0.5 * sameprice) / (len(incomes) - 0.5)
else:
exp_income = sum(incomes) / len(incomes)
# print(c, incomes, sameprice, exp_income)
if exp_income > best_income:
best_income, best_choice = exp_income, (c,)
# print(gid, info, best_income, best_choice, statements)
sell_action_cache[(gid, num, rival_num, info)] = (best_income, best_choice)
return best_income, best_choice
sell_stage_cache = {}
async def sell_stage(clues, gid, nums, rival_nums, info):
if gid == 3:
return -clues, -1, None
if (clues, gid, nums[gid:], rival_nums[gid:], info) in sell_stage_cache:
return sell_stage_cache[(clues, gid, nums[gid:], rival_nums[gid:], info)]
best_income, best_choice = await sell_action(gid, nums[gid], rival_nums[gid], info)
best_income += (await sell_stage(clues, gid + 1, nums, rival_nums, ()))[0]
if clues:
if info == ():
if gid != 2:
pry_income = await average_with_weight(
[
await sell_stage(clues - 1, gid, nums, rival_nums, (i, None))
for i in range(11)
]
+ [
await sell_stage(clues - 1, gid, nums, rival_nums, (None, i))
for i in range(11)
]
)
if pry_income > best_income:
best_income, best_choice = pry_income, -1
else:
pry_income = await average_with_weight(
[
await sell_stage(clues - 1, gid, nums, rival_nums, (i,))
for i in range(11)
]
)
if pry_income > best_income:
best_income, best_choice = pry_income, -1
elif None in info:
__idx = 1 - info.index(None)
pry_income = await average_with_weight(
[
await sell_stage(
clues - 1,
gid,
nums,
rival_nums,
(i, info[1]) if __idx == 1 else (info[0], i),
)
for i in range(info[__idx] + 1)
]
)
if pry_income > best_income:
best_income, best_choice = pry_income, -1
statements = [
statement
for statement in product(range(11), repeat=[2, 2, 1][gid])
if await sell_conflict_checker(statement, info)
]
statements_num = len(statements)
if len(info) == 2 and None in info:
statements_num -= 0.5
sell_stage_cache[(clues, gid, nums[gid:], rival_nums[gid:], info)] = (
best_income,
best_choice,
statements_num,
)
return best_income, best_choice, statements_num
# ============================================================
buy_result_cache = {}
async def buy_result(bid, me):
if (bid, me) in buy_result_cache:
return buy_result_cache[(bid, me)]
ranks = [sum([1 for y in bid if y > x]) for x in bid]
sorted_ranks = sorted(ranks)
if sorted_ranks == [0, 0, 0]:
ret = 30
elif sorted_ranks == [0, 0, 2]:
ret = [36, None, 18][ranks[bid.index(me)]]
elif sorted_ranks == [0, 1, 1]:
ret = [36, 27][ranks[bid.index(me)]]
elif sorted_ranks == [0, 1, 2]:
ret = [40, 30, 20][ranks[bid.index(me)]]
elif sorted_ranks == [0, 0]:
ret = 5
elif sorted_ranks == [0, 1]:
ret = [6, 4][ranks[bid.index(me)]]
else:
raise NotImplementedError
buy_result_cache[(bid, me)] = ret
return ret
async def buy_conflict_checker(statement, info):
if len(info) == 0 or statement == info:
return True
if len(info) == 1:
return info[0] == statement[0]
return False
async def buy_action(clues, infos):
statements = [
statement
for statement in product(range(3), repeat=5)
if await buy_conflict_checker(statement[0:2], infos[0])
and await buy_conflict_checker(statement[2:4], infos[1])
and await buy_conflict_checker(statement[4:5], infos[2])
]
if len(statements) == 0:
print(clues, infos)
best_income, best_choice = -1e9, None
for c in product(range(3), repeat=3):
incomes = []
for statement in statements:
drink_nums = stock_drink // 90 * await buy_result(statement[0:2] + (c[0],), c[0])
snack_nums = stock_snack // 90 * await buy_result(statement[2:4] + (c[1],), c[1])
token_nums = stock_token // 10 * await buy_result(statement[4:5] + (c[2],), c[2])
costs = (
drink_nums * buy_drink[c[0]]
+ snack_nums * buy_snack[c[1]]
+ token_nums * buy_token[c[2]]
)
nums = (drink_nums, snack_nums, token_nums)
__sd = stock_drink // 90
__ss = stock_snack // 90
__st = stock_token // 10
rival_nums = (
(
__sd * await buy_result(statement[0:2] + (c[0],), statement[0]),
__sd * await buy_result(statement[0:2] + (c[0],), statement[1]),
),
(
__ss * await buy_result(statement[2:4] + (c[1],), statement[2]),
__ss * await buy_result(statement[2:4] + (c[1],), statement[3]),
),
(__st * await buy_result(statement[4:5] + (c[2],), statement[4]),),
)
sell_income = (await sell_stage(clues, 0, nums, rival_nums, ()))[0]
incomes.append(sell_income - costs)
exp_income = sum(incomes) / len(incomes)
if exp_income > best_income:
best_income, best_choice = exp_income, c
return best_income, best_choice
buy_stage_cache = {}
async def buy_stage(clues, infos):
if (clues, infos) in buy_stage_cache:
return buy_stage_cache[(clues, infos)]
best_income, best_choice = await buy_action(clues, infos)
if clues:
if len(infos[0]) < 2:
pry_income = await average_with_weight(
[
await buy_stage(clues - 1, (infos[0] + (i,), infos[1], infos[2]))
for i in range(3)
]
)
if pry_income > best_income:
best_income, best_choice = pry_income, 0
if len(infos[1]) < 2:
pry_income = await average_with_weight(
[
await buy_stage(clues - 1, (infos[0], infos[1] + (i,), infos[2]))
for i in range(3)
]
)
if pry_income > best_income:
best_income, best_choice = pry_income, 1
if len(infos[2]) < 1:
pry_income = await average_with_weight(
[
await buy_stage(clues - 1, (infos[0], infos[1], infos[2] + (i,)))
for i in range(3)
]
)
if pry_income > best_income:
best_income, best_choice = pry_income, 2
statements = [
statement
for statement in product(range(3), repeat=5)
if await buy_conflict_checker(statement[0:2], infos[0])
and await buy_conflict_checker(statement[2:4], infos[1])
and await buy_conflict_checker(statement[4:5], infos[2])
]
buy_stage_cache[(clues, infos)] = (best_income, best_choice, len(statements))
return (best_income, best_choice, len(statements))
# ============================================================
import sys
async def action_confirm():
await my_input(Colors.GREY + "行动结束后按下回车键……" + Colors.RESET)
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
clues = await input_int("[*] 请输入可打探的次数:", (4, 5, 6))
infos = ((), (), ())
print("请稍等,正在计算中……")
while True:
await asyncio.sleep(0)
ret = await buy_stage(clues, infos)
print(Colors.GREEN + f"[+] 当前期望收益为:{ret[0]}" + Colors.RESET)
if isinstance(ret[1], int):
goods = ["饮品", "餐点", "纪念品"][ret[1]]
print(Colors.YELLOW + f"[A] 请选择打探 {goods} 的信息(选择从上往下第一个未打探过的商店)" + Colors.RESET)
await action_confirm()
choice = await input_int("[*] 请问打探到的消息中,该商店的进货策略是?(1-3) ", range(1, 4))
infos = list(infos)
infos[ret[1]] = infos[ret[1]] + (choice - 1,)
infos = tuple(infos)
clues -= 1
else:
strategy = [["保守", "稳健", "激进"][ret[1][i]] for i in range(3)]
print(Colors.YELLOW + "[A] 进货策略已确定:" + ",".join(strategy) + Colors.RESET)
await action_confirm()
break
print("[+] 请告知每种商品的进货数量,店与店之间用空格隔开。")
print("[+] 注意,商店的次序需要是特定的顺序(雪雉商店,其他商店,钟表商店)而不是进货数量排行榜上的顺序。")
drink = await input_ints("[*] 饮品进货数量(三个数,中间用空格隔开):", 3, customer_drink)
snack = await input_ints("[*] 餐点进货数量(三个数,中间用空格隔开):", 3, customer_snack)
token = await input_ints("[*] 纪念品进货数量(两个数,中间用空格隔开):", 2, customer_token)
nums = (drink[0], snack[0], token[0])
rival_nums = (drink[1:], snack[1:], token[1:])
cost = sum([nums[i] * buy_prices[i][ret[1][i]] for i in range(3)])
income = 0
print("饮品售卖阶段。")
info = ()
while True:
ret = await sell_stage(clues, 0, nums, rival_nums, info)
print(Colors.GREEN + f"[+] 当前期望收益为:{ret[0] - cost + income}" + Colors.RESET)
if isinstance(ret[1], int):
print(Colors.YELLOW + f"[A] 请进行一次消息打探。" + Colors.RESET)
await action_confirm()
rival = await input_int("[*] 请问打探到了哪个商店的信息?1 为其他商店,2 为钟表商店 (1-2) ", (1, 2))
price = await input_int("[*] 请问他们给出的售价是?", sell_drink)
price_idx = list(sell_drink).index(price)
if info == ():
info = (None, None)
info = list(info)
info[rival - 1] = price_idx
info = tuple(info)
clues -= 1
else:
price = sell_drink[ret[1][0]]
print(Colors.YELLOW + f"[A] 饮品售卖策略已确定。请设定价格为:{price}" + Colors.RESET)
await action_confirm()
break
income += await input_int("[*] 请输入饮品售卖收益(包括激励奖励):")
print("餐品售卖阶段。")
info = ()
while True:
ret = await sell_stage(clues, 1, nums, rival_nums, info)
print(Colors.GREEN + f"[+] 当前期望收益为:{ret[0] - cost + income}" + Colors.RESET)
if isinstance(ret[1], int):
print(Colors.YELLOW + f"[A] 请进行一次消息打探。" + Colors.RESET)
await action_confirm()
rival = await input_int("[*] 请问打探到了哪个商店的信息?1 为其他商店,2 为钟表商店 (1-2) ", (1, 2))
price = await input_int("[*] 请问他们给出的售价是?", sell_snack)
price_idx = list(sell_snack).index(price)
if info == ():
info = (None, None)
info = list(info)
info[rival - 1] = price_idx
info = tuple(info)
clues -= 1
else:
price = sell_snack[ret[1][0]]
print(Colors.YELLOW + f"[A] 餐品售卖策略已确定。请设定价格为:{price}" + Colors.RESET)
await action_confirm()
break
income += await input_int("[*] 请输入餐点售卖收益(包括激励奖励):")
print("纪念品售卖阶段。")
info = ()
while True:
ret = await sell_stage(clues, 2, nums, rival_nums, info)
print(Colors.GREEN + f"[+] 当前期望收益为:{ret[0] - cost + income}" + Colors.RESET)
if isinstance(ret[1], int):
print(Colors.YELLOW + f"[A] 请进行一次消息打探。" + Colors.RESET)
await action_confirm()
price = await input_int("[*] 请问他们给出的售价是?", sell_token)
price_idx = list(sell_token).index(price)
info = (price_idx,)
clues -= 1
else:
price = sell_token[ret[1][0]]
print(Colors.YELLOW + f"[A] 纪念品售卖策略已确定。请设定价格为:{price}" + Colors.RESET)
await action_confirm()
break
income += await input_int("[*] 请输入纪念品售卖收益(包括激励奖励):")
sold_token_me = await input_int("[*] 请输入纪念品售卖数量:")
sold_token_rival = await input_int("[*] 请输入钟表商店的纪念品售卖数量:")
new_remain_me = remain_me + nums[-1] - sold_token_me
new_remain_rival = remain_rival + rival_nums[-1][-1] - sold_token_rival
print(Colors.GREEN + f"[+] 纪念品库存为:{new_remain_me}" + Colors.RESET)
print(Colors.GREEN + f"[+] 钟表商店的纪念品库存为:{new_remain_rival}" + Colors.RESET)
if new_remain_rival < 0:
print(Colors.RED + f"[!] 哦!貌似钟表商店之前是有库存的!没关系,他们现在大概率没库存了 =v=" + Colors.RESET)
print("请记录好以上信息。")
print("最后……")
print(Colors.GREEN + f"[+] 实际收益为:{income - cost}" + Colors.RESET)
if new_remain_me:
print(Colors.GREEN + f"[+] 未来期望收益为:{future_value * new_remain_me}" + Colors.RESET)
print("结束了!")
await action_confirm()