Created
August 5, 2015 22:08
-
-
Save vznvzn/55900567c49a34a9353e 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 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) | |
l1.each { |x| p(x) } | |
end | |
l = [1] | |
seen = {} | |
ARGV[0].to_i.times \ | |
{ | |
|i| | |
l2 = [] | |
l.each \ | |
{ | |
|x| | |
next if (seen.member?(x)) | |
seen[x] = nil | |
l2 << x * 2 | |
l2 << (x - 1) / 3 if (x % 3 == 1 && ((x - 1) / 3).odd?) | |
} | |
l = l2 | |
} | |
compress(l.sort.map { |x| x.to_s(2).split('') }.flatten) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment