Skip to content

Instantly share code, notes, and snippets.

@ChasManRors
Created June 27, 2021 20:54
Show Gist options
  • Save ChasManRors/2178bb950f558532f1903c149791766a to your computer and use it in GitHub Desktop.
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 ...?
# 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