Ruby Challenge
-
I thought this would make a fun little Ruby challenge.
Instructions:
Correctly solve this puzzle using Ruby, then post the answer along with the Ruby code you used to solve it. (Originally posted by solo.)
Use the numbers 1 through 16 to complete the equations.
Each number is only used once.
Each row is a math equation. Each column is a math equation.
Remember that multiplication and division are performed before addition and subtraction.The goal is to have fun and hopefully learn something new about Ruby.
-
Just perfect, Fredo. I think the "algorithm" for solving the puzzle is the easy part, but your code could not have expressed it more succinctly.
I hope you do not mind the screen capture.
-
I had initially assumed the puzzle was a "trick" question, so my first (2) attempts actually were iterating every possible permutation (forwards and backwards.) But there are no tricks as it turns out and after many hours without a solution (I left for work and came back) it was obvious it would take a veeery long time to try every possible solution, so there needed to be a way to reduce the solution set. (Oh, I actually started a third instance that was trying random permutations.)
In the end, I had 4 files which represented the possible solutions for each row, then I brute force iterated every combination (although skipping subsequent solutions which contained duplicate numbers.) However my code lacks any of the elegance of Fredo's, which is why you'll never see it.
Since there turns out to be only one solution, finding the solution for just the rows first, you could stop there and not need to perform the column calcs. But this insight comes in hindsight - I was not clever enough to think about it while writing the code.
-
Haven't we given you guys enough to do already?
Get back in your rooms and get back to work.
5-7 seconds: that's a freaking eternity.
-
@Jim
Thanks for the challenge. Actually, I figured out quickly that there was no implied 'trick' and that you'll have to go through numerous tests. The obvious approach is alternate horizontal and vertical equations. Then, the second idea is to find the order of equations which have the most discrimination in order to reduce the solutions set. Apparently, the best is to start with the equation where there is a '/' sign, which leads to less than 3 seconds.I must say that I am bluffed with the speed of Ruby, thinking that all the lines are interpreted and that I use many Array operations which have a lot of code behind them.
Anyway, as you were predicting it, this type of challenge helps to think on how you write Ruby code. I discovered for instance that, unlike what I have read elsewhere in the DEV forum, the 'each' loops seems more performing than the 'for' loops.
Fredo
PS: another challenge would be to create a Sudoku solution workbench in Ruby!
-
Jim
Interesting challenge!
Here attached is my proposal - Menu entry is in the Plugins menuIt takes between 5 and 7 seconds on my machine, with ~1.4 millions attempts. I did not try all possible orders for solving the equations, so there may be a 'faster' order.
Fred
EDIT: Updated version: seems faster by using 'each' loops instead of 'for' loops
EDIT 2 (26 Jun 12): Update for integer division (must be valid) - Also best order of equation resolution gives a response time below 3 seconds with ~590,000 attempts
-
@unknownuser said:
I must say that I am bluffed with the speed of Ruby, thinking that all the lines are interpreted and that I use many Array operations which have a lot of code behind them.[/qoute]
My thought is that it is fast because there are no objects being created. The arrays being manipulated contain only pointers to integers which there is only a single instance of in Ruby.
@unknownuser said:
PS: another challenge would be to create a Sudoku solution workbench in Ruby!
Yes, that's a good one; and i had a similar one in mind called the "100 squares" puzzle.
Advertisement