-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.py
68 lines (58 loc) · 2.21 KB
/
simulator.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
from collections import defaultdict, Counter
import enum
import sys
from solver import *
def simulate_reply(input_word, answer):
reply = [LetterReply.NOTIN]*LETTER_NUM
letter_count = defaultdict(int, Counter(answer))
# porc Green
for index, (input_letter, ans_letter) in enumerate(zip(input_word, answer)):
if input_letter == ans_letter:
reply[index] = LetterReply.CORRECT
letter_count[input_letter] -= 1
# porc Yellow
for index, (input_letter, ans_letter) in enumerate(zip(input_word, answer)):
if input_letter != ans_letter and letter_count[input_letter] > 0:
reply[index] = LetterReply.EXISITS
letter_count[input_letter] -= 1
return reply
def simulate(answer):
ans_candidates = all_5letter_words
input_word = FIRST_ANSWER
print(f"------- start simulation for {answer} --------")
for turn in range(1, MAX_TURN+1):
print(f" answer candidates: {len(ans_candidates)}")
print(f" turn{turn} input: {input_word}")
reply = simulate_reply(input_word, answer)
print(f" reply: {[r.value for r in reply]}")
if reply == [LetterReply.CORRECT]*5:
print(" solved")
return turn
ans_candidates = filter_by_reply(ans_candidates, input_word, reply)
input_word = pararell_search(ans_candidates)
print(" failed")
return 7
def simulate_manual(answer):
for turn in range(1, MAX_TURN+1):
input_word = input()
reply = simulate_reply(input_word, answer)
print(f" reply: {[r.value for r in reply]}")
if reply == [LetterReply.CORRECT]*5:
print(" solved")
return turn
print(" failed")
return 7
def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="""Wordle Solver Simulator""")
parser.add_argument("-m", "--manual", action='store_true',
help="simulate by manual")
parser.add_argument("answer", help='answer of simulation')
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
if args.manual:
simulate_manual(args.answer)
else:
simulate(args.answer)