aartist has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parks Puzzle
by tybalt89 (Monsignor) on Jan 16, 2018 at 23:37 UTC | |
This is my basic puzzle solver method.
| [reply] [d/l] |
by trippledubs (Deacon) on Jan 22, 2018 at 16:08 UTC | |
Simple, eh? It gets simpler after you look up @-, $`, $&, $', \l modifier, add a couple of data dumps, think through the regex greediness and backtracking, dig into precedence..
That is pretty easy to see how it works, without the /s modifier, . does not match newline, so two #s in the same line. Your comment spells it out, but it makes your intent clear for the next couple lines which are a little bit harder to see how they work.
The greediness of the first portion (?:.{$gap}..)* marches the regex all the way to the end of the grid while .{$gap}.# backtracks so it tries to make a match. Then of course the best line and, also would like to mention that, I super searched for 'while /$&/g' just to see if it was somewhere else, and only this node came up making it pretty unique.
That line makes me love and hate perl at the same time. Creating and pushing the partial solutions to @queue in one line. It iterates through the grid, for the letter it has matched earlier [A-Z], adds this grid with the instance of the letter changed to #, and all other instances of the letter lowercased with \l. The alternation operator in /#(.{$gap,$egap}|)#/s is not used or maybe I don't understand, I think perl should not compile that, but
works and matches, so back to the books, seems that empty alternations always match, so that means this alternative is checking for two '#'s in a row, which would already be matched by that point. In other search result coincidences, exactly one year ago today, someone tried to get perl critic to disallow empty alternations. Very neat solution to a fun problem, easy enough to dig into and understand a little bit more perl. Thanks for sharing. | [reply] [d/l] [select] |
by tybalt89 (Monsignor) on Jan 23, 2018 at 01:09 UTC | |
Good catch on the empty alternative! As far as I can tell it's there from the beginning when I tried to write
the three tests in one regex. I missed it when I split them up to make them clearer. It doesn't hurt anything, however.
| [reply] [d/l] |
|
Re: Parks Puzzle
by davies (Monsignor) on Jan 17, 2018 at 08:46 UTC | |
I don't know this particular puzzle, but whenever I see anything that might involve trial & error, I consider Dancing Links (https://en.wikipedia.org/wiki/Dancing_Links). Masak gave a talk on this to the London Perl Workshop >5 years ago. It is probably on line somewhere, but I have no idea where. He used Perl 6 in his examples, but the techniques were certainly obvious to me. Regards, John Davies Update: having had a VERY quick look at the site (it's not something I want to get), my first thought is that it is a variant on the N queens problem. See, for example, Efficient N-Queen solution with Perl. | [reply] |
|
Re: Parks Puzzle
by stevieb (Canon) on Jan 16, 2018 at 22:26 UTC | |
Please, you've been a member longer than I have. Show some code before you throw out such a broad request. | [reply] |
|
Re: Parks Puzzle
by tybalt89 (Monsignor) on Jan 20, 2018 at 12:54 UTC | |
Let's solve it with a simple? regex :)
Outputs:
| [reply] [d/l] [select] |
by aartist (Pilgrim) on Jan 23, 2018 at 21:16 UTC | |
Thank you, for the wonderful solution that works for puzzle. Having said that, my original idea is to provide rules to human being to solve the puzzle with limited observation at a time. For example, simple observation that if it finds any color is occupied by only a single row or columns than all the other cell of that row or column are 'blanked'. simple rules. It is more about detecting small patterns in the data visually and provide an action associated with it. Pattern needs to be small enough to be identified visually. Thus, in other words, I am looking for helping hand algorithm rather than brute force solution. It is like see the 'X' if you can and do the 'Y'. In the end, solution will be there. | [reply] |
by tybalt89 (Monsignor) on Jan 25, 2018 at 04:50 UTC | |
In setting up a game playing program to allow easy addition of patterns (see X -> do Y), I discovered the complete list of patterns for this puzzle: That's it :) Here's the code for it:
It prints a grid for each step of the solution (slightly more than 120 lines). | [reply] [d/l] |
|
Re: Parks Puzzle
by Anonymous Monk on Jan 18, 2018 at 15:56 UTC | |
| [reply] |
by tybalt89 (Monsignor) on Jan 20, 2018 at 12:48 UTC | |
So let's see the Prolog code for your fun solution to Parks Puzzle... | [reply] |