Skip to content

Instantly share code, notes, and snippets.

@joegiralt
Last active November 5, 2019 04:05
Show Gist options
  • Save joegiralt/cc36c4f7ea9bc1ee94cc8a6fbe35123b to your computer and use it in GitHub Desktop.
Save joegiralt/cc36c4f7ea9bc1ee94cc8a6fbe35123b to your computer and use it in GitHub Desktop.
class NataliasPhoneNumberSolver
attr_reader :init_number
attr_accessor :unique_dials, :current_unique_dial
# 1,2,3
# 4,5,6
# 7,8,9
# 0
LOOKUP = {
'1' => ['6', '8'],
'2' => ['7', '9'],
'3' => ['4','8'],
'4' => ['3', '9', '0'],
'5' => [],
'6' => ['1', '7', '0'],
'7' => ['2', '6'],
'8' => ['1', '3'],
'9' => ['2', '4'],
'0' => ['4','6']
}
def initialize(init_number)
@init_number = init_number
@current_unique_dial = ''
@unique_dials = []
end
def find_unique_dials!
dial(init_number)
end
private
def dial(number, depth = 0)
@current_unique_dial += number
LOOKUP[number].each do |next_number|
next if current_unique_dial[0..depth].include?(next_number)
dial(next_number, depth + 1)
end
@unique_dials << current_unique_dial[0..depth]
end
end
solver = NataliasPhoneNumberSolver.new('1')
solver.find_unique_dials!
solver.unique_dials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment