Last active
May 28, 2020 13:55
-
-
Save joaohornburg/221d7ac9c4c673bc7c8507be6db4c11c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [6, 2, 8, 4, 7] | |
# 2 numeros corretos, 1 na posição certa | |
b = [0, 5, 9, 4, 7] | |
# 2 números corretos, mas na posição errada | |
c = [1, 3, 0, 8, 7] | |
# 1 número correto na posição correta | |
d = [9, 7, 6, 3, 2] | |
# 3 númeors corretos, mas no lugar errado | |
e = [3, 0, 4, 7, 9] | |
# 1 número correto, mas no lugar errado | |
def positional_matches(iteration, num, match_count) | |
matches = 0 | |
for i in 0..4 do | |
matches += 1 if iteration[i] == num[i] | |
end | |
match_count == matches | |
end | |
possibilities = [] | |
for i in 0..99999 do | |
possible_solution = i.to_s.rjust(5, '0').split('').map(&:to_i) | |
a_ok = (possible_solution & a).count == 2 && positional_matches(possible_solution, a, 1) | |
b_ok = (possible_solution & b).count == 2 && positional_matches(possible_solution, b, 0) | |
c_ok = (possible_solution & c).count == 1 && positional_matches(possible_solution, c, 1) | |
d_ok = (possible_solution & d).count == 3 && positional_matches(possible_solution, d, 0) | |
e_ok = (possible_solution & e).count == 1 && positional_matches(possible_solution, e, 0) | |
possibilities << possible_solution if a_ok && b_ok && c_ok && d_ok && e_ok | |
end | |
possibilities |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment