• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Ruby Challenge

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 3 Posters 642 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    Jim
    last edited by 23 Jun 2012, 18:57

    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.

    181388_353671404703078_655818395_n.jpg

    Hi

    1 Reply Last reply Reply Quote 0
    • J Offline
      Jim
      last edited by 30 Jun 2012, 17:03

      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.

      fredo_puzzle.png

      Hi

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by 30 Jun 2012, 17:39

        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.

        Hi

        1 Reply Last reply Reply Quote 0
        • B Offline
          brookefox
          last edited by 30 Jun 2012, 17:44

          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.

          ~ Brooke

          1 Reply Last reply Reply Quote 0
          • F Offline
            fredo6
            last edited by 30 Jun 2012, 21:22

            @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!

            1 Reply Last reply Reply Quote 0
            • F Offline
              fredo6
              last edited by 30 Jun 2012, 23:33

              Jim

              Interesting challenge!
              Here attached is my proposal - Menu entry is in the Plugins menu

              It 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


              to drop in the SU Plugins directory

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by 1 Jul 2012, 17:30

                @unknownuser said:

                @Jim

                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.

                Hi

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  1/7
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement