Created
September 22, 2016 00:54
-
-
Save vznvzn/a386a242716a355ae4ba880a894f2899 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
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def adv(x1) | |
n1 = n = x1['n'] | |
d2 = nil | |
s = '' | |
c = nil | |
ns = n.to_s(2) | |
x1['d'] = d(ns) | |
x1['w'] = ns.length | |
while (n >= n1 && n != 1) | |
d = d(n.to_s(2)) | |
c = s.length if (c.nil? && !d2.nil? && (0.5 - d) * (0.5 - d2) < 0) | |
d2 = d | |
while (n.odd?) | |
n = (n * 3 + 1) / 2 | |
s << '1' | |
end | |
while (n.even?) | |
n /= 2 | |
s << '0' | |
end | |
end | |
x1['c'] = c.nil? ? 0 : c | |
m1 = m0 = nil | |
if (!c.nil?) then | |
s2 = s[c..-1] | |
m1 = s2.split(/0+/).map { |x| x.length }.max | |
m0 = s2.split(/1+/).map { |x| x.length }.max | |
end | |
m1 = 0 if (m1.nil?) | |
m0 = 0 if (m0.nil?) | |
x1['m1'] = m1 | |
x1['m0'] = m0 | |
return x1 | |
end | |
def next2(z) | |
l = [z] | |
p = z['p'] + 1 | |
l << adv({'n'=>z['n'] + 2**p, 'p'=>p}) | |
l << z.merge({'p'=>p}) | |
return l | |
end | |
l = [next2({'n'=>1, 'p'=>0})] | |
l2 = [] | |
w = ARGV[0] | |
fn = "out_#{ARGV[0]}_#{ARGV[1]}_#{ARGV[2]}.txt" | |
f = File.open(fn, 'w') | |
(ARGV[1].to_i * 1000).to_i.times \ | |
{ | |
|i| | |
$stderr.puts(i) if (i % 100 == 0) | |
w2 = w | |
w2 = 'c' if (i % 5 == 0) | |
l = l.sort_by! { |x| [x[1][w2], x[1]['d']] } | |
l.reverse! | |
j = rand([l.size, ARGV[2].to_i].min) | |
z = l.delete_at(j) | |
l << next2(z[1]) | |
l << next2(z[2]) | |
f.puts([l.size, z[1]['w'], z[1]['c'], z[1][w]].join("\t")) if (i % 2 == 1) | |
f.flush | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment