-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_guesser.py
More file actions
113 lines (92 loc) · 2.37 KB
/
code_guesser.py
File metadata and controls
113 lines (92 loc) · 2.37 KB
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
inp = """6
402 0
390 0
816 2
848 2
777 0
815 1"""
"""
816 2
81_
_16
8_6
848 2
84_
8_8
_48
"""
def get_match(code1, code2):
"""Returns None if no match is found, otherwise return the match"""
if len(code1) != len(code2):
return None
match = ""
for i in range(len(code1)):
c1 = code1[i]
c2 = code2[i]
if c1 == "_":
match += c2
elif c2 == "_":
match += c1
elif c1 == c2:
match += c1
else:
return None
return match
def parse():
lines = inp.split("\n")
lines = lines[1:]
candidates = []
for line in lines:
code, num_correct = line.split()
N = len(code)
num_correct = int(num_correct)
if num_correct == 0:
# TODO figure out what to do when nothing is correct
continue
elif num_correct == 1:
subcandidates = []
for i in range(N):
subsubcandidate = ""
for j in range(N):
if i == j:
subsubcandidate += code[i]
else:
subsubcandidate += "_"
subcandidates.append(subsubcandidate)
candidates.append(subcandidates)
elif num_correct == 2:
# TODO: handle inputs of length 4
subcandidates = []
for i in range(N):
subcandidates.append(code[:i] + "_" + code[i+1:])
candidates.append(subcandidates)
elif num_correct == 3:
# TODO: inputs of length 4
pass
print("candidates", candidates)
return candidates
def eval_helper(candidates, i, current):
if i == len(candidates):
return current
subcandidates = candidates[i]
for j in range(len(subcandidates)):
code = subcandidates[j]
match = get_match(current, code)
if match is None:
continue
result = eval_helper(candidates, i+1, match)
if result is not None:
return result
return None
def eval(candidates):
# '___'
# '_16' X
# '8_6'
# '846'
# [['_16', '8_6', '81_'], ['_48', '8_8', '84_'], ['8__', '_1_', '__5']]
i = 0
return eval_helper(candidates, i, "___")
if __name__ == "__main__":
cands = parse()
result = eval(cands)
print(result)