Created
June 27, 2021 20:54
-
-
Save ChasManRors/2178bb950f558532f1903c149791766a to your computer and use it in GitHub Desktop.
Secrete agent observes a PIN, but may not have it right. So where are the arrays of arrays of arrays ...?
This file contains hidden or 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
# Here is a slightly earlier version of the code with added variables for clarity (I hope) | |
def pins(str) | |
variations = str.split('').map { |c| @board[c] } | |
return variations.first if str.length <= 1 | |
position, *positions = *(-str.length..-1).to_a | |
nested_arrays = positions.inject(variations[position]) do |result, index| | |
result.product(variations[index]) | |
end | |
nested_arrays.map(&:flatten).map(&:join) | |
end | |
# nested_arrays before flatten and join above | |
[[[["1", "3"], "5"], "7"], | |
[[["1", "3"], "5"], "4"], | |
[[["1", "3"], "5"], "8"], | |
... | |
[[["4", "6"], "8"], "7"], | |
[[["4", "6"], "8"], "4"], | |
[[["4", "6"], "8"], "8"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment