Skip to content

Instantly share code, notes, and snippets.

@tompng
Created June 11, 2020 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompng/31ff0f46c20a94ab258c731a3a163969 to your computer and use it in GitHub Desktop.
Save tompng/31ff0f46c20a94ab258c731a3a163969 to your computer and use it in GitHub Desktop.
require'set'
keys=('0000'..'9999').to_a
sets = Set.new keys
queue=[]
k = keys.first
sets.delete k
list = k.chars.map(&:to_i)
k = k[1..]
n = 0
loop do
if n == 10
k, n = queue.pop
break unless k
sets.add(k)
k = k[...-1]
list.pop
next
end
k2 = k+n.to_s
next n += 1 unless sets.include? k2
list << n
sets.delete k2
if sets.empty?
puts list.join
binding.irb
end
queue << [k2, n + 1]
k = k[1..] + n.to_s
n = 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment