Created
January 3, 2020 01:23
-
-
Save vznvzn/39e0e59f4199d1ba583ed91a222c49cb 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 f2(n) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def seq(n) | |
n1 = n | |
l = [n1] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def flip(s, n) | |
s = s.dup | |
s[n, 1] = (s[n, 1].to_i ^ 1).to_s | |
return s | |
end | |
def delete(s, n) | |
s = s.dup | |
s[n, 1] = '' | |
return s | |
end | |
def insert(s, n) | |
s0 = s.dup | |
s0[n, 0] = '0' | |
s1 = s.dup | |
s1[n, 0] = '1' | |
return s0, s1 | |
end | |
def edit1(s) | |
l = [] | |
s.length.times \ | |
{ | |
|n| | |
l << flip(s, n) | |
l << delete(s, n) | |
} | |
(s.length + 1).times \ | |
{ | |
|n| | |
l += insert(s, n) | |
} | |
return l.select { |x| x[0, 1] != '0' } | |
end | |
def sum(l) | |
return l.inject { |t, x| t + x } | |
end | |
def count(ns) | |
l1 = seq(ns.to_i(2)) | |
cg = l1.index { |x| x < l1[0] } | |
cg = l1.size if (cg.nil?) | |
cm = (0...cg).max_by { |x| l1[x] } | |
nw = ns.length | |
b = [0, 0] | |
l1[nw..cg].each { |x| b[x % 2] += 1 } | |
ba = (b[0] - b[1]).abs | |
return {'ns' => ns, 'cm' => cm, 'cg' => cg, 'c' => l1.size, 'nw' => nw, 'ba' => ba} | |
end | |
def out(a, a2 = {}, t = nil, ct = "(column('t')):", w = ' with line ') | |
fn = $fn.nil? ? 'out.txt' : $fn | |
k = a.keys | |
if ($f1.nil?) then | |
$f1 = File.open(fn, 'w') | |
$f1.puts(k.join("\t")) | |
f2 = File.open('gnuplot.cmd', 'w') | |
st = t.nil? ? 'unset title; ' : "set title \"#{t}\"; " | |
f2.puts("set ytics nomirror; set y2tics; set colors classic; set key top left box opaque; #{st}; plot \\") | |
(a.keys - ['t']).each \ | |
{ | |
|x| | |
next if (a2.member?(x) && a2[x].nil?) | |
m = a2.fetch(x, '') | |
m += w if (!m.include?('with')) | |
f2.puts("'#{fn}' using #{ct}(column('#{x}')) title '#{x}' #{m}, \\") | |
} | |
f2.puts | |
f2.close | |
end | |
$f1.puts(a.values_at(*k).join("\t")) | |
$f1.flush | |
end | |
l1 = [count('1')] | |
seen = {'' => nil, '0' => nil, '1' => nil} | |
l = [] | |
c = 5e3.to_i | |
m = 5e3.to_i | |
d = c / 2000 | |
d = 1 if ($d == 0) | |
k = 'ba' | |
c.times \ | |
{ | |
|t| | |
$stderr.puts(t) if (t % 100 == 0) | |
l2 = l1.map { |x| x[k] } | |
l2 = (0...l1.size).sort_by { |x| l2[x] }.reverse | |
i = l2[rand([l2.size, 20].min)] | |
l3 = l2[m..-1] | |
l3 = [] if (l3.nil?) | |
x = nil | |
(l3 | [i]).sort.reverse.each \ | |
{ | |
|x1| | |
x2 = l1.delete_at(x1) | |
x = x2 if (x1 == i) | |
} | |
l << x | |
out(x.merge({'t' => t}), | |
{'ns' => nil, | |
'ba' => 'axes x1y2 lw 2', | |
'nw' => 'axes x1y2'}) if (t % d == 0) | |
edit1(x['ns']).each \ | |
{ | |
|ns| | |
next if (seen.member?(ns)) | |
l1 << count(ns) | |
seen[ns] = nil | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment