Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: The N-queens problem using pure regexes

by dragonchild (Archbishop)
on Oct 08, 2003 at 16:56 UTC ( #297656=note: print w/ replies, xml ) Need Help??


in reply to The N-queens problem using pure regexes

Bravo! Very neat solution. And, a beautiful demonstration of the fact that the right data structure isn't just important, it's everything.

Also, I like the heavy usage of map and grep. A little harder to read at first (as a programmer who's functional at play, not at work), but very concise.

Question - how does the regex engine not choose a free square in d4 when looking for free squares in d3? I'm guessing it has to do with [\x00-\xFF]*, but I'm not positive as to how that's working ...

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


Comment on Re: The N-queens problem using pure regexes
Download Code
Re: The N-queens problem using pure regexes
by Abigail-II (Bishop) on Oct 09, 2003 at 09:04 UTC
    how does the regex engine not choose a free square in d4 when looking for free squares in d3?

    I do not know what you mean by this. Do you mean "why doesn't the regex engine never try to place a queen on both d4 and d3?" In that case, the answer is simple - it will never even attempt to place two queens on the same column. The first part of the string is:

    ,a1,a2,a3,a4,a5,a6,a7,a8, ,b1,b2,b3,b4,b5,b6,b7,b8, ,c1,c2,c3,c4,c5,c6,c7,c8, ,d1,d2,d3,d4,d5,d6,d7,d8, ,e1,e2,e3,e4,e5,e6,e7,e8, ,f1,f2,f3,f4,f5,f6,f7,f8, ,g1,g2,g3,g4,g5,g6,g7,g8, ,h1,h2,h3,h4,h5,h6,h7,h8,
    is being matched against the first part of the regex:
    .*,(\w+),.* .*,(\w+),.* .*,(\w+),.* .*,(\w+),.* .*,(\w+),.* .*,(\w+),.* .*,(\w+),.* .*,(\w+),.*
    The newlines in the string and regex are significant here (. won't match a newline). This part will make \1 one of 'a1' .. 'a8', \2 one of 'b1' .. 'b8', \3 one of 'c1' .. 'c8', etc. So, it will never try to place two queens on the 'd' column.

    But you may ask, "why doesn't it put a queen on d3 and one on e4?". That's where the second part of the string and regex come in. \4 contains the position of the queen on the d column, so, in this case, \4 equals 'd3'. Then there's this line in the second part of the regex:

    [\x00-\xFF]*\n\4:.*\1,.*\2,.*\3,.*\5,.*\6,.*\7,.*\8,.*
    We know that \4 equals 'd3', so part of this lines read '\nd3:'. Looking back at the second part of the string, there is only one line that starts with 'd3:':
    d3:a1,a2,a4,a5,a7,a8,b2,b4,b6,b7,b8,c1,c5,c6,c7,c8,e1,e5,e6,e7,e8,f2,f +4,f6,f7,f8,g1,g2,g4,g5,g7,g8,h1,h2,h4,h5,h6,h8,
    If you look carefully, after the colon are all the fields that aren't attacked by a queen on d3. Specifically, the field 'e4' is missing. But the line
    [\x00-\xFF]*\n\4:.*\1,.*\2,.*\3,.*\5,.*\6,.*\7,.*\8,.*
    is saying "match a line that starts with 'd3', and has after the colon a list of fields that include the positions of all other 7 queens". The [\x00-\xFF]* just skips enough lines to get to the next queen.

    Abigail

      The [\x00-\xFF]* just skips enough lines to get to the next queen.

      Your explanation above is good. However, the above quote is the actual meat of my question ...

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Well, [\x00-\xFF]* matches anything, or any length. I can't use .* here because it should skip over newlines as well. And adding /s to the regex won't do it either, as other </code>.*</code>'s should *not* match newlines.

        Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://297656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-09-01 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (9 votes), past polls