Advertisement
theosib

Untitled

Apr 5th, 2020
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.69 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. # One digit is right and in its place
  4. def rule_682(digits)
  5.   correct = 0
  6.   correct += 1 if digits[0] == 6
  7.   correct += 1 if digits[1] == 8
  8.   correct += 1 if digits[2] == 2
  9.   return correct == 1
  10. end
  11.  
  12. # One digit is right but in the wrong place
  13. def rule_614(digits)
  14.   correct = 0
  15.   correct += 1 if (digits[1] == 6 || digits[2] == 6) && digits[0] != 6
  16.   correct += 1 if (digits[0] == 1 || digits[2] == 1) && digits[1] != 1
  17.   correct += 1 if (digits[0] == 4 || digits[1] == 4) && digits[2] != 4
  18.   return correct == 1
  19. end
  20.  
  21. # Two digits are right but both are in the wrong place
  22. def rule_206(digits)
  23.   correct = 0
  24.   correct += 1 if (digits[1] == 2 || digits[2] == 2) && digits[0] != 2
  25.   correct += 1 if (digits[0] == 0 || digits[2] == 0) && digits[1] != 0
  26.   correct += 1 if (digits[0] == 6 || digits[1] == 6) && digits[2] != 6
  27.   return correct == 2
  28. end
  29.  
  30. # All digits are wrong
  31. def rule_738(digits)
  32.   wrong = [7, 3, 8]
  33.   return false if wrong.include?(digits[0])
  34.   return false if wrong.include?(digits[1])
  35.   return false if wrong.include?(digits[1])
  36.   return true
  37. end
  38.  
  39. # One digit is right but in the wrong place
  40. def rule_380(digits)
  41.   correct = 0
  42.   correct += 1 if (digits[1] == 3 || digits[2] == 3) && digits[0] != 3
  43.   correct += 1 if (digits[0] == 8 || digits[2] == 8) && digits[1] != 8
  44.   correct += 1 if (digits[0] == 0 || digits[1] == 0) && digits[2] != 0
  45.   return correct == 1
  46. end
  47.  
  48. for i in 0..999
  49.   digits = []
  50.   digits[0] = i / 100;
  51.   digits[1] = (i / 10) % 10
  52.   digits[2] = i % 10
  53.  
  54.   next if !rule_682(digits)
  55.   next if !rule_614(digits)
  56.   next if !rule_206(digits)
  57.   next if !rule_738(digits)
  58.   next if !rule_380(digits)
  59.  
  60.   puts digits.join()
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement