Created
June 11, 2020 14:42
-
-
Save tompng/31ff0f46c20a94ab258c731a3a163969 to your computer and use it in GitHub Desktop.
This file contains 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
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