Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created July 29, 2015 20:18
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/bab543058d6a62ebe2dc to your computer and use it in GitHub Desktop.
Save vznvzn/bab543058d6a62ebe2dc to your computer and use it in GitHub Desktop.
def compress(l)
s = 'a'
l1 = []
loop \
{
l2 = (0...l.size).sort_by { |x| l[x..-1] }
z = (0...(l2.size - 1)).to_a.map \
{
|x|
n = 1
m = [-1]
while (x + n < l2.size)
i, j = l2[x], l2[x + n]
i, j = [j, i] if (i > j)
t = 0
t += 1 while (l[i + t] == l[j + t] && i + t < j && j + t <= l2.size)
m = [m, [t, i, j]].max
n += 1
end
m
}.sort.last
t, i, j = z
break if (z.nil? || t <= 1)
s2 = l[j, t]
l1 << [s, s2]
l[j, t] = s
l[i, t] = s
i = 0
while (i + t < l.size)
l[i, t] = s if (s2 == l[i, t])
i += 1
end
s = s.succ
}
# p(l)
# p(l1)
return (l + l1.flatten).size
end
def entropy(l, z)
return compress((l.map { |x| x[1] } + [z.to_s(2).split('')]).sort.flatten)
end
l = [[1, "1", 0, 0]]
seen = {}
2000.times \
{
l.sort_by! { |x| [x[2], x[0], x[3]] }
l2 = l.map { |x| x[3] }
n, b, e, d = l.shift
puts([n, e, d, l2.max - l2.min].join("\t"))
$stdout.flush
([n * 2] + (((n % 3 == 1) && (y = (n - 1) / 3).odd?) ? [y] : [])).each \
{
|x|
next if (seen.member?(x))
seen[x] = nil
l << [x, x.to_s(2).split(''), entropy(l, x), d + 1]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment