r/dailyprogrammer • u/rya11111 3 1 • Feb 24 '12
[2/24/2012] Challenge #15 [intermediate]
A 30x30 grid of squares contains 900 fleas, initially one flea per square. When a bell is rung, each flea jumps to an adjacent square at random (usually 4 possibilities, except for fleas on the edge of the grid or at the corners).
What is the expected number of unoccupied squares after 50 rings of the bell? Give your answer rounded to six decimal places.
source: project euler
14
Upvotes
3
u/eruonna Feb 24 '12 edited Feb 24 '12
Haskell: http://hpaste.org/64292
Edit: here's one that's parametrized a little better: http://hpaste.org/64294
Answer:
2
u/omnilynx Feb 24 '12
I know this isn't the question, but just for fun, if the grid was infinite, I think the chance that a square is empty after any (non-zero) number of bells would be about 31.64%. On average there will be 4 fleas adjacent to any square before the bell, and they will each have 4 possible jumps, 3 of which will not fill the square. Any fleas on the square itself before the bell are irrelevant since they must jump off.
2
u/electric_machinery Feb 25 '12 edited Feb 25 '12
I may have made it more complicated than requested. And I'm not sure if I answered the question correctly. Anyway here's my solution:
In C
It produces an image plot of each frame (50 frames) so I animated them. I never really make videos or animations so I apologize in advance for the lack of quality.
Edit:
I'm getting this many empty squares:
2
u/xertoz Feb 26 '12
Average number of empty squares:
Solution written in PHP:
1
u/_redka 0 0 Feb 24 '12 edited Feb 24 '12
Ruby
minified -> 5 short lines
full version -> 10 lines + comments
to give an estimate of the expected number of unoccupied cells the code above was run 100 times and the average was:
1
u/[deleted] Feb 25 '12 edited Feb 25 '12
Perl. I was hoping to use a matrix but perl does not supports true matrices or multi-dimensional arrays. Had to utilize an array of array references so it was pretty educational. Also, I'm getting roughly 282 empty squares averaged after 50 runs, so perhaps there's an slight error somewhere. Good luck reading it though.
1
u/FaafBeNelly Feb 25 '12 edited Feb 25 '12
C#: http://pastie.org/3457042
Not my usual language, but learning C# at the moment, so said why not.
After 1000 times, the average count of empty squares was:
I think I must have done something wrong, or maybe didn't understand the problem properly as my answer seems different to those posted already.
EDIT: If I increase the jumps to 100 then I get:
1
u/callekabo Feb 25 '12
Here's my Python 3 solution, that prints out a matrix of which squares are inhabited and which are not.
Number of uninhabited squares:
1
u/eruonna Feb 27 '12
Python, using MapReduce:
The idea here is actually the same as my Haskell solution. It might illustrate what is going on a bit better than the linear algebra based solution, though. It might also be clearer how this would generalize to different starting positions for the fleas. I ran it with a single machine MapReduce I grabbed from Michael Nielsen's tutorial. Of course, that means there's no real benefit to using MapReduce; indeed, it is slow as dirt. But I think it is clear how you could hook it up to any MapReduce system you wanted. I'd be interested to know how big you have to make the grid before this beats the linear algebraic solution (with whatever size cluster).