3 ms·
I thought this was kinda neat so I taught a computer how to do it: https://github.com/lvh/regex-crossword/blob/master/src/lvh/regex_crossword/logic.clj https://
by lvh 7y ago
I thought this was kinda neat so I taught a computer how to do it: https://github.com/lvh/regex-crossword/blob/master/src/lvh/regex_crossword/logic.clj https://github.com/lvh/regex-crossword/blob/master/src/lvh/r...
Right now, it works using a logic programming engine (core.logic, essentially miniKanren), generating a bunch of strings that match the regex, and then applying them as constraints.
If I were to improve it, I'd parse the regex, walk the parse tree, and assign constraints that way. (There's already a regex parser in there: that's how the string generation works.) That was my first thought, but that's more complex than what I want to tackle before work :)
Right now there's a bug where if e.g. a regex AB|CD will get applied character-wise, so it might erroneously try AC or BD. The way to fix that is to make sure the answers actually match the regex all the way at the end.
Using a constraint engine has the cool feature that it can show you multiple answers if they exist. (That's not true for my current implementation, because the string generation is randomized, so there's no guarantee it will visit each possible string.)
Someone else has pointed out that there are a lot of clues in the titles, e.g. that the answers are palindromes. That would be easy to add:
(map (fn [vars] (l/== vars (reverse vars))) (concat rows cols))
(Read: "for each row and col, create a constraint that the row/col must be equal to itself reversed".)
- CodexArcanum 7y agoA while back I wrote a little Rust library that does exactly what you say: parses a regex to use as a text generator. It was a lot of fun, and really gives you an appreciation for just how much bigger than ASCII matching the regex world can be! Writing a constraint-based solver that uses the generator library sounds like an amusing project as well! Link: https://github.com/CryptArchy/regex_generate https://github.com/CryptArchy/regex_generate