Created
November 23, 2018 05:40
-
-
Save vznvzn/94bb77a08c3b58f2aa86ca92b54c3c65 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 init1(w, m) | |
l = [] | |
b = 1 | |
while (w > 0) | |
t = [w, m].min | |
r = (t <= 1) ? 1 : rand(t) + 1 | |
l << [b] * r | |
b ^= 1 | |
w -= r | |
end | |
s = l.flatten.join | |
return s | |
end | |
def len01(ns) | |
nl = ns.length | |
l1 = ns.split(/0+/).map { |x| x.length } | |
l2 = ns.split(/1+/)[1..-1] | |
l2 = [''] if (l2.nil?) | |
l0 = l2.map { |x| x.length } | |
return l0, l1 | |
end | |
def mx01(ns) | |
return len01(ns).flatten.max | |
end | |
def crunch(ns) | |
i = 0 | |
ns1 = ns | |
mx1 = mx01(ns) | |
return if (mx1 != $mx) | |
begin | |
ns = f2(ns.to_i(2)).to_s(2) | |
mx = mx01(ns) | |
i += 1 | |
end while (mx >= mx1 && ns != '1') | |
return {'n' => ns1.to_i(2), 'mxc' => i, 'ns' => ns1, 'mx1' => mx1} | |
end | |
def init(w, n = 200) | |
c = 0 | |
l = [] | |
n.times \ | |
{ | |
|i| | |
c += 1 | |
ns = init1(w, $mx) | |
z = crunch(ns) | |
redo if (z.nil?) | |
l << z | |
} | |
return l | |
end | |
def cycle() | |
return Time.now.to_i / 5 | |
end | |
def mix(fn1, w = 1000) | |
File.open(fn = "out#{$mx}.txt", 'w').close | |
l = init(w) | |
t1 = Time.now.to_i | |
a = nil | |
j = 0 | |
tz = cycle() | |
loop \ | |
{ | |
l.sort_by! { |x| -x['mxc'] } | |
if (l[0] != a) | |
a = l[0] | |
f = File.open(fn, 'a') | |
f.puts(a) | |
f.close | |
t1 = Time.now.to_i | |
end | |
t2 = Time.now.to_i - t1 | |
s = {'mx' => $mx, 'mxc' => a['mxc'], 'j' => j, 't' => Time.now.to_s, 't2' => t2} | |
if ((t = cycle()) != tz) then | |
p(s) | |
tz = t | |
end | |
if (t2 > 60 * 5) then | |
f = File.open(fn1, 'a') | |
f.puts(s.inspect) | |
f.close | |
break | |
end | |
c = l.size / 4 | |
l1 = (0..c).to_a | |
x, y = (1..2).map { l1.delete_at(rand(l1.size)) } | |
case (rand(3)) | |
when 0 | |
ns1 = l[x]['ns'] | |
ns = ns1.dup | |
i = rand(w - 1) + 1 | |
ns[i, 1] = (ns1[i, 1].to_i ^ 1).to_s | |
when 1 | |
ns1 = l[x]['ns'] | |
ns2 = l[y]['ns'] | |
ns = '' | |
w.times \ | |
{ | |
|i| | |
ns[i, 1] = [ns1[i, 1], ns2[i, 1]][rand(2)] | |
} | |
when 2 | |
ns1 = l[x]['ns'] | |
ns2 = l[y]['ns'] | |
i = rand(w) | |
ns = ns1[0...i] + ns2[i..-1] | |
end | |
z = crunch(ns) | |
j += 1 | |
redo if (z.nil?) | |
l.pop if (l.size > 500) | |
l << z | |
} | |
end | |
File.open(fn1 = "out.txt", 'w').close | |
(10..20).each \ | |
{ | |
|mx| | |
$mx = mx | |
mix(fn1) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment