-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.rb
More file actions
63 lines (34 loc) · 728 Bytes
/
Solution.rb
File metadata and controls
63 lines (34 loc) · 728 Bytes
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
# @param {String} secret
# @param {String} guess
# @return {String}
def get_hint(secret, guess)
bulls = 0; cows = 0
index = 0
size = secret.size
while index < size
if secret[index] == guess[index]
bulls += 1
secret[index] = guess[index] = 'X'
end
index += 1
end
i = 0; j = 0
while i < size
if secret[i] != 'X'
j = 0
while j < size
if guess[j] != 'X' and secret[i] == guess[j]
cows += 1
secret[i] = 'X'
guess[j] = 'X'
end
j += 1
end
end
i += 1
end
bulls.to_s + 'A' + cows.to_s + 'B'
end
puts get_hint '1111', '1111'
puts get_hint '1807', '7810'
puts get_hint '1123', '0111'