Skip to content

Instantly share code, notes, and snippets.

@jingxia1219
Last active June 18, 2018 01:54
Show Gist options
  • Save jingxia1219/0a8f4d6ce1fba20890021db0b356b322 to your computer and use it in GitHub Desktop.
Save jingxia1219/0a8f4d6ce1fba20890021db0b356b322 to your computer and use it in GitHub Desktop.
code_war: The observed PIN
def get_pins(observed)
adjacents = []
chars = observed.chars
chars.each do |char|
adjacents << adjacent(char)
end
adjacents.reduce { |acc,el| acc.product el }.map { |el| el.join("")}.sort
end
def adjacent(str)
adjacent = []
COMBINATION.each_with_index do |el, idx|
if el.include?(str)
index = el.index(str)
adjacent << el[index+1] if el[index+1] #left
adjacent << el[index-1] if index-1 >= 0 #right
adjacent << COMBINATION[idx-1][index] if idx -1 >= 0
adjacent << COMBINATION[idx+1][index] if COMBINATION[idx+1][index] #down
adjacent << str
end
end
adjacent
end
arr = []
arr << ("1".."3").to_a
arr << ("4".."6").to_a
arr << ("7".."9").to_a
arr << [nil, "0", nil]
COMBINATION = arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment