Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 15, 2019 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/555036731ac5747bd9fb862b924e9637 to your computer and use it in GitHub Desktop.
Save vznvzn/555036731ac5747bd9fb862b924e9637 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s)
end
def lenz(ns, p)
l = ns.split(p)
l = [''] if (l.nil? || l.empty?)
return l.map { |x| x.length }
end
def len01z(ns)
return len(ns, /0+/), len(ns, /1+/)
end
def mx01z(ns)
l1, l0 = len01(ns)
return l1.max, l0.max
end
def lenx(ns, p)
l = ns.split(p)
l = [] if (l.nil?)
l.shift if (l[0] == '')
return l.map { |x| x.length }
end
def len01x(ns)
return lenx(ns, /0+/), lenx(ns, /1+/)
end
def mx01x(ns)
l1, l0 = len01x(ns)
return (l1 + l0).max
end
def add(l, mxp, n, x)
p = (n % 2).to_s + x['p']
# l1m, l0m = mx01(p)
# return if ([l0m, l1m].max >= mxp)
return if (mx01x(p) >= mxp)
w = n.to_s(2).length
l1 = [n] + x['l']
c = x['c'] + (n.even? ? 1 : 0)
l << {'n' => n,
'p' => p,
'w' => n.to_s(2).length,
'r' => c.to_f / w,
'l' => l1,
'c' => c}
end
def back(ns)
mxp = 4
add(l = [], mxp, ns.to_i(2), {'p' => '', 'l' => [], 'c' => 0})
mx = nil
k = 'r'
t = 0
e = 2
loop \
{
return if (l.empty?)
break if (!mx.nil? && mx[k] >= e)
l.sort_by! { |x| -x[k] }
mx = [l[0], mx].compact.max_by { |x| x[k] }
x = l.delete_at(rand([l.size, 50].min))
puts([t, x[k]].join("\t")) if (t % 100 == 0)
n = x['n']
add(l, mxp, (n - 1) / 3, x) if ((n - 1) % 3 == 0 && ((n - 1) / 3).odd?)
add(l, mxp, n * 2, x)
t += 1
}
return mx
end
def reverse(w, mk)
t = 0
x = nil
loop \
{
t += 1
ns = mk.call(w)
x = back(ns)
break if (!x.nil?)
}
# $stderr.puts([t, x].inspect)
return x
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def e(ns)
return len01x(ns).flatten.size.to_f / ns.length
end
def fmt1(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top right opaque;\\")
f.puts("plot \\")
f.puts("[0:#{l.size - 1}][0:#{w}] '-' matrix using 2:1:3 with image title '', \\")
f.puts("'-' using 1 with line title 'e' axes x1y2 lw 3, \\")
f.puts("'-' using (column('d')) with line axes x1y2 lw 3 ")
l1 = []
l.each_with_index \
{
|z, i|
l1 << [d(z), e(z)]
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
l1.each { |x| f.puts(x[1]) }
f.puts("eof")
f.puts("d")
l1.each { |x| f.puts(x[0]) }
f.puts("eof")
end
def grid(l, b)
f = File.open(fn = "gnuplot#{b}.cmd", 'w')
f.puts("set palette negative grayscale; unset colorbox;")
fmt1(l.map { |x| x.to_s(2).reverse }, f)
f.close
end
def adj(x, m, n, s2, p2, p, x3)
s2.replace(p[0...x] + (x3 % 2).to_s)
m = (s2[x, 1] == p[x, 1])
n = p2.reverse.to_i(2)
p2[x, 1] = m ? '11' : '01'
return m, n, x3
end
def terras121(p)
p2 = ['01', '11'][p[0, 1].to_i]
n = 1
n3 = 0
m = p2 == '11'
x3 = (p2.reverse.to_i(2)) >> 1
(1...p.length).each \
{
|x|
if (!m)
ns = n.to_s(2)
ns[0, 1] = ''
m1 = x3 - 3**n3
n1 = f2(m1)
end
n3 = p[0...x].split('').select { |z| z == '1' }.size
s2 = ''
m, n, x3 = adj(x, m, n, s2, p2, p, m ? (f2(x3) + 3**n3) : (n1 + 3**n3))
}
n = p2.reverse.to_i(2)
return n
end
def seq(n)
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def construct(b)
x = nil
begin
ns = dense(100, 0.5)
ns[49, 50] = b * 50
ns2 = terras121(ns).to_s(2)
x = back(ns2)
end while (x.nil?)
x.delete('l')
p(x)
l = seq(x['n'])
return l
end
['0', '1'].each { |x| grid(construct(x), x) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment