Created
November 25, 2015 23:08
-
-
Save vznvzn/cd59dc475a718b21fcf7 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 adv(n) | |
l = [] | |
s = '' | |
while (n != 1) | |
l << n | |
while (n.odd?) | |
n = (n * 3 + 1) / 2 | |
s << '1' | |
end | |
while (n.even?) | |
n /= 2 | |
s << '0' | |
end | |
end | |
return 0, l, s | |
end | |
def f(l) | |
l2 = [] | |
t = 0 | |
l.each_with_index \ | |
{ | |
|l1, x| | |
i = l1[0] | |
next if (i == l1[1].size) | |
t += l1[1][i] | |
j = i + 10 | |
j = l1[1].size - 1 if (j >= l1[1].size) | |
next if (i == j) | |
m = l1[1][(i + 1)..j].max | |
l2 << [m, l1[1][i + 1] - l1[1][i], x] | |
} | |
$t, $z = t, 0 if ($t.nil?) | |
d = (t - $t) | |
$z += d if (d > 0) | |
puts([t, d, $z].join("\t")) | |
$t = t | |
return l2 | |
end | |
def step(l) | |
i = l[0] | |
l[0] += 1 | |
return if (i + 1 > l[1].size - 1) | |
# puts([$x, Math.log(l[1][i])].join("\t")) | |
# puts([$x + 1, Math.log(l[1][i + 1])].join("\t")) | |
# puts | |
end | |
n = 10001 | |
l = [] | |
100.times { l << adv(n); n += 2 } | |
$x = 0 | |
loop \ | |
{ | |
l2 = f(l).sort.reverse | |
break if (l2.empty?) | |
l3 = l2.select { |x| x[1] < 0 }.sort_by { |x| x[1] }.reverse | |
t = 0 | |
l3.each { |x| t += x[1] } | |
i = 0 | |
l2.each { |x| break if (x[1] + t < 0); i += 1 } | |
if (i < l2.size) then | |
a = l2[i][1] | |
l1 = [l2[i][2]] | |
b = 0 | |
if (a > 0) then | |
i = 0 | |
l3.each { |x| b += x[1]; l1 << x[2]; break if (a + b < 0); i += 1 } | |
end | |
l1.each { |x| step(l[x]) } | |
else | |
m = l2.min_by { |x| x[1] } | |
step(l[m[2]]) | |
end | |
$x += 1 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment