Another Puzzle Solved with CF

In an earlier post, I mentioned how I used CF to solve a simple puzzle. I was lazy again today and decided I would use it to solve another puzzle. This time the puzzle was a 4x4 grid with the letters W, X, Y, and Z. Each row had a different combination of letters with a sum at the end. So for example, W + X + Y + Z = 17 and W + Z + Z + W = 16. You had to solve for Y + X + X + Y. So, being the lazy person I am, I used a simple script:

<cfloop index="x" from=1 to=8>
<cfloop index="y" from=1 to=8>
<cfloop index="w" from=1 to=8>
<cfloop index="z" from=1 to=8>
<cfif (w + x + y + z is 17) and
          (z + 2*x + y is 19) and
          (z + 3*x is 18) and
          (w + 2*y + w is 14)>

            <cfoutput>
             x=#x#, y=#y#, w=#w#, z=#z# is GOOD
            </cfoutput>
            <cfbreak>
</cfif>
</cfloop>
</cfloop>
</cfloop>
</cfloop>

I used 8 as a max as all the sums were below 20 and I figured it would be a safe assumption. Running the code took about one second and quickly spit out the solution:

x=4, y=5, w=2, z=6 is GOOD

I then solved for Y + X + X + Y to get 18. Once again CF saves the day!

Archived Comments

Comment 1 by PaulKD posted on 2/27/2004 at 12:02 AM

Ray,

Nothing to do with your puzzle, but I've often wondered if there is a performance benefit to having cfoutput tags inside or outside nested loops.

Comment 2 by Brian posted on 2/27/2004 at 11:24 AM

Can you put a link to the original puzzle out there?

Thx,
B

Comment 3 by Raymond Camden posted on 2/27/2004 at 6:13 PM

PaulKD: Um, I can't think of any thing in particular. In my case I use cfbreak which immidiately ends once the solution is found.

Brian: Sorry, it's a physical calendar. The company actually has an online component as well, but you have to enter a code to get the calendar.

Comment 4 by mike posted on 3/6/2004 at 12:57 AM

hmm... this reminds me of first year engineering... linear algebra... oh yes, systems of linear equasions.

i got a question for you ray, how do you know the answer you got is the only answer? or is that not important?

Comment 5 by Raymond Camden posted on 3/11/2004 at 2:03 AM

mike:
Nah, I'm not concerned. Of course you could remove the cfbreak and see for yourself. :)