Created
March 9, 2019 02:04
-
-
Save vznvzn/e8e07e231986aabae44856e5668dceba 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 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) | |
l0, l1 = len01(ns) | |
return l1.max, l0.max | |
end | |
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def add(l, n, x) | |
return if (n == 1 && !x['mxr'].nil?) | |
p = x['p'] + (n % 2).to_s | |
mxp = mx01(p).max | |
return if (mxp > 20) | |
ns = n.to_s(2) | |
mxr = mx01(ns)[0] | |
mxr = {'mxr' => mxr, 'l1i' => x['l1'].size} | |
l << {'n' => n, | |
'l1' => x['l1'] + [n], | |
'p' => p, | |
'd' => (d(ns) - 0.5).abs, | |
'mxp' => mxp, | |
'mxr' => [x['mxr'], mxr].compact.max_by { |x| x['mxr'] }} | |
end | |
l = [] | |
add(l, 1, {'l1' => [], 'p' => '', 'mxr' => nil}) | |
50000.times \ | |
{ | |
|t| | |
l = l.sort_by { |x| [x['mxr']['mxr'], x['l1'].size, x['d']] }.reverse | |
x = l.delete_at(rand([l.size, 100].min)) | |
l.pop if (l.size > 2000) | |
p({'t' => t, 'ls' => l.size} | |
.merge(x['mxr']) | |
.merge({'d' => x['d'], 'l1s' => x['l1'].size})) if (t % 100 == 0) | |
n = x['n'] | |
add(l, (n - 1) / 3, x) if ((n - 1) % 3 == 0 && ((n - 1) / 3).odd?) | |
add(l, n * 2, x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment