Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 28, 2019 04:49
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/da0508bbe6c85cfc907a8a2181965e11 to your computer and use it in GitHub Desktop.
Save vznvzn/da0508bbe6c85cfc907a8a2181965e11 to your computer and use it in GitHub Desktop.
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 rwalk(u1, d1, z, w)
x = (z + w * d1) / (u1 + d1)
y = w - x
xw = x / w
$stderr.puts({'z' => z, 'w' => w, 'x' => x, 'y' => y, 'xw' => xw, 'u1' => u1, 'd1' => d1}.inspect)
d = dense(w, xw)
return d
end
def walk(s, u1, d1, j)
c = 0
s.split('').each_with_index \
{
|x, i|
c += (x == '1') ? u1 : -d1
puts([i, c, j].join("\t"))
}
puts
end
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
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 fmt1(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top left opaque;\\")
f.puts("plot \\")
f.puts("[0:#{w}][0:#{l.size - 1}] '-' matrix using 1:2:3 with image title ''")
l.each_with_index \
{
|z, i|
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
end
def grid(l)
f = File.open(fn = "gnuplot1.cmd", 'w')
f.puts("set palette negative grayscale; unset colorbox;")
fmt1(l.map { |x| x.reverse }, f)
f.close
end
u = 3.0 / 2
d = 1.0 / 2
u1 = Math.log(u) / Math.log(2)
d1 = -Math.log(d) / Math.log(2)
z = 50
w = 500
wa = (z / u1).to_i
wb = (z / -(u1 - d1) * 2).to_i
f = File.open('out1.txt', 'w')
l2 = []
(wa..(w - wb)).each_with_index \
{
|w1, j|
s1 = rwalk(u1, d1, z, w1)
s2 = dense(w - w1, 0.5)
walk(s1 + s2, u1, d1, j)
l2 << (s = terras121(s1 + s2).to_s(2))
f.puts(s)
}
f.close
grid(l2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment