Skip to content

Instantly share code, notes, and snippets.

@rennex
Last active January 25, 2018 02:41
Show Gist options
  • Save rennex/d978762b6b3fdf3156164da4026e2a31 to your computer and use it in GitHub Desktop.
Save rennex/d978762b6b3fdf3156164da4026e2a31 to your computer and use it in GitHub Desktop.
Nondeterministic Ruby behaviour (both with Ruby 1.9.3p392 on a physical machine and with Ruby 2.3.1p112 on a VirtualBox VM)
$ for i in `seq 200`; do ruby puzzle6_2_debug.rb ; done | uniq
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [13, 12, 12, 10, 10, 8, 7, 6, 6, 4, 4, 2, 2, 1, 15, 0]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [6, 6, 4, 2, 2, 0, 0, 15, 13, 12, 11, 11, 9, 8, 4, 9]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [2, 1, 0, 15, 13, 12, 11, 11, 9, 9, 7, 7, 5, 5, 4, 1]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [8, 7, 7, 5, 5, 3, 2, 1, 1, 0, 14, 14, 12, 12, 10, 11]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [1, 1, 14, 12, 11, 11, 9, 9, 14, 7, 6, 7, 4, 3, 3, 0]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [7, 6, 5, 3, 2, 2, 0, 0, 14, 13, 12, 11, 10, 8, 5, 14]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [4, 2, 2, 14, 14, 12, 12, 11, 1, 10, 8, 0, 7, 6, 5, 4]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [7, 4, 3, 2, 0, 0, 14, 12, 7, 12, 11, 6, 9, 9, 14, 2]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [9, 5, 4, 4, 2, 1, 1, 0, 9, 14, 14, 8, 13, 11, 10, 7]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [5, 4, 4, 2, 1, 0, 15, 13, 13, 11, 10, 10, 9, 8, 1, 6]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
result = [8, 6, 5, 4, 3, 2, 1, 1, 15, 14, 12, 12, 11, 10, 8, 0]
result = [0, 14, 13, 12, 11, 10, 8, 8, 6, 6, 5, 3, 3, 2, 1, 10]
input = "4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3"
banks = input.split(/\s+/).map(&:to_i)
seen = {}
count = 0
loop do
break if seen[banks]
seen[banks] = true
m = banks.max
i = banks.find_index(m)
banks[i] = 0
m.times do
i = (i+1) % banks.size
banks[i] += 1
end
count += 1
end
puts "result = #{banks}"
@rennex
Copy link
Author

rennex commented Jan 25, 2018

Oh yeah, the reason for this was: I modified the Array object I used as hash keys multiple times. Every time you launch Ruby, the hash values for given objects are different, and on 32-bit rubies collisions were much more likely than on 64 bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment