Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- # One digit is right and in its place
- def rule_682(digits)
- correct = 0
- correct += 1 if digits[0] == 6
- correct += 1 if digits[1] == 8
- correct += 1 if digits[2] == 2
- return correct == 1
- end
- # One digit is right but in the wrong place
- def rule_614(digits)
- correct = 0
- correct += 1 if (digits[1] == 6 || digits[2] == 6) && digits[0] != 6
- correct += 1 if (digits[0] == 1 || digits[2] == 1) && digits[1] != 1
- correct += 1 if (digits[0] == 4 || digits[1] == 4) && digits[2] != 4
- return correct == 1
- end
- # Two digits are right but both are in the wrong place
- def rule_206(digits)
- correct = 0
- correct += 1 if (digits[1] == 2 || digits[2] == 2) && digits[0] != 2
- correct += 1 if (digits[0] == 0 || digits[2] == 0) && digits[1] != 0
- correct += 1 if (digits[0] == 6 || digits[1] == 6) && digits[2] != 6
- return correct == 2
- end
- # All digits are wrong
- def rule_738(digits)
- wrong = [7, 3, 8]
- return false if wrong.include?(digits[0])
- return false if wrong.include?(digits[1])
- return false if wrong.include?(digits[1])
- return true
- end
- # One digit is right but in the wrong place
- def rule_380(digits)
- correct = 0
- correct += 1 if (digits[1] == 3 || digits[2] == 3) && digits[0] != 3
- correct += 1 if (digits[0] == 8 || digits[2] == 8) && digits[1] != 8
- correct += 1 if (digits[0] == 0 || digits[1] == 0) && digits[2] != 0
- return correct == 1
- end
- for i in 0..999
- digits = []
- digits[0] = i / 100;
- digits[1] = (i / 10) % 10
- digits[2] = i % 10
- next if !rule_682(digits)
- next if !rule_614(digits)
- next if !rule_206(digits)
- next if !rule_738(digits)
- next if !rule_380(digits)
- puts digits.join()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement