Skip to content

Instantly share code, notes, and snippets.

@joaohornburg
Last active May 28, 2020 13:55
Show Gist options
  • Save joaohornburg/221d7ac9c4c673bc7c8507be6db4c11c to your computer and use it in GitHub Desktop.
Save joaohornburg/221d7ac9c4c673bc7c8507be6db4c11c to your computer and use it in GitHub Desktop.
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