Skip to content

Instantly share code, notes, and snippets.

@cristianrasch
Last active March 6, 2018 19:07
Show Gist options
  • Save cristianrasch/676d54fa512a6ee67fdc9f40382014cc to your computer and use it in GitHub Desktop.
Save cristianrasch/676d54fa512a6ee67fdc9f40382014cc to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# RBENV_VERSION=jruby-9.1.16.0 JRUBY_OPTS="--server -J-Xms2048m -J-Xmx2048m" N=12 ./dict-gen -o ~/Downloads/dicts
require "optparse"
require "fileutils"
require "unique_permutation"
SYMBOLS = ("A".."Z").to_a.freeze
N = Integer(ENV.fetch("N", 12))
THREADS = Integer(ENV.fetch("THREADS", 4))
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"
opts.on("-o", "--out OUT_DIR", "Output dir") do |o|
options[:out_dir] = o
end
end.parse!
out_dir = options[:out_dir] || "~"
FileUtils.mkdir_p(out_dir)
FileUtils.rm_f(Dir[File.join(out_dir, "dict*.txt")])
q = Queue.new
SYMBOLS.combination(N) { |c| q << c }
(1..THREADS).map {
Thread.new do
combination = nil
begin
combination = begin
q.pop(true)
rescue ThreadError; end
if combination
out_file = File.expand_path(File.join(out_dir, "dict#{combination.join}.txt"))
File.open(out_file, "w") do |f|
combination.unique_permutation do |permutation|
f.puts(permutation.join)
end
end
end
end while combination
end
}.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment