Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created November 1, 2019 01:37
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/c343d6375766cd4ccb360e0c91fb1825 to your computer and use it in GitHub Desktop.
Save vznvzn/c343d6375766cd4ccb360e0c91fb1825 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 seed(w, w2)
ns = dense(w, 0.5)
i = rand(w - w2 + 1)
ns[i, w2] = '1' * w2
return {'w' => w, 'ns' => ns, 'n' => ns.to_i(2), 'c' => 0}
end
def add(l, n, x)
ns = n.to_s(2)
l << {'w' => ns.length, 'ns' => ns, 'n' => n, 'c' => x['c'] + 1}
end
def fmt1(l, f)
l = l.map { |x| x.to_s(2).reverse }
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 ''")
l1 = []
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")
f.puts("pause -1")
end
def grid2(l)
f = File.open(fn = "gnuplot1.cmd", 'w')
f.puts("set palette negative grayscale; unset colorbox;")
fmt1(l, f)
fmt1(l.sort, f)
f.close
end
def backtrack()
w = 100
w2 = 50
x = seed(w, w2)
l = [x]
24.times \
{
l2 = []
l.each \
{
|x|
n = x['n']
add(l2, (n - 1) / 3, x) if ((n - 1) % 3 == 0 && ((n - 1) / 3).odd?)
add(l2, n * 2, x)
}
l = l2
}
return l
end
begin
l = backtrack()
p(l.size)
end while (l.size == 1)
grid2(l.map { |x| x['n'] })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment