Skip to content

Instantly share code, notes, and snippets.

@eggsyntax
Created March 24, 2021 01:59
Show Gist options
  • Save eggsyntax/81f28fd69d9dbaa895a2c38138e255fd to your computer and use it in GitHub Desktop.
Save eggsyntax/81f28fd69d9dbaa895a2c38138e255fd to your computer and use it in GitHub Desktop.
'''
Implementation of the puzzle given at
https://worldspiritsockpuppet.com/2021/03/07/sleep-math-red-clay-blue-clay.html
I strongly suggest thinking about the puzzle for a while before looking at the
code; it's really interesting and counterintuitive, or was for me.
"Suppose that you have 1 kg of red clay that is 100 degrees and 1 kg of blue
clay that is 0 degrees. You can divide and recombine clay freely. If two pieces
of clay come into contact, temperature immediately equilibrates—if you put the
1kg of red clay next to 0.5 kg of blue clay, all the clay will immediately
become 66 degrees. Other than that the temperature of the clay doesn’t change
(i.e. no exchange with air or your hands, no radiation, etc.). Your goal is to
end up with all of the blue clay in a single clump that is as hot as possible.
How hot can you make it? (Equivalently: how cold can you make the red clay?)"
'''
def ave(xs):
'''Average of a list of numbers'''
return sum(xs) / len(xs)
def run(num_pieces):
'''num_pieces is the number of pieces that you break each color of clay
into. For simplicity, all pieces are the same size, and there are the same
number of red and blue pieces.'''
red = [100.0] * num_pieces
blue = [0.0] * num_pieces
for i in range(num_pieces):
for j in range(num_pieces):
t = ave([red[i], blue[j]])
red[i] = t
blue[j] = t
print("Given %d pieces:" % num_pieces)
print("Final temperature of red is %0.2f" % (ave(red),))
print("Final temperature of blue is %0.2f" % (ave(blue),))
run(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment