Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created November 25, 2015 22:38
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/85a5e4d9341c8a573002 to your computer and use it in GitHub Desktop.
Save vznvzn/85a5e4d9341c8a573002 to your computer and use it in GitHub Desktop.
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(l1, w)
i = l1[0]
return 0 if (i == l1[1].size)
$t += l1[1][i]
j = i + 10
j = l1[1].size - 1 if (j >= l1[1].size)
t = 0
l1[1][i..j].each { |x| t += x }
case w
when 0
return t.to_f / (j - i + 1)
when 1
return l1[1][j]
when 2
m = l1[1][(i + 1)..j].max
return m.nil? ? 0 : m
when 3
return l1[1][i..j].max
when 4
return t
end
end
def run(w)
n = 10001
l = []
100.times { l << adv(n); n += 2 }
z = 0
loop \
{
$t = 0
l2 = l.map { |l1| f(l1, w) }
c = l2.size - l2.select { |x| x == 0 }.size
break if (c == 0)
puts([z, $t, c, w].join("\t"))
j = (0...l2.size).max_by { |x| l2[x] }
l2b = l2
l2 = l.map { |l1| i = l1[0]; i >= l1[1].size - 1 ? 0 : l1[1][i + 1] - l1[1][i] }
l4 = [j]
if (l2[j] > 0) then
l3 = (0...l.size).select { |x| l2[x] < 0}.sort_by { |x| l2[x] }.reverse
t = 0
i = 0
while (l2[j] + t > 0 && i < l3.size)
t += l2[l3[i]]
l4 << l3[i]
i += 1
end
end
l4.each { |x| l[x][0] += 1 }
z += 1
}
puts
end
(0..4).each { |w| run(w) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment