Author Topic: ==={challenge}=== Word Puzzle  (Read 18339 times)

0 Members and 1 Guest are viewing this topic.

Dashmonkey

  • Newt
  • Posts: 29
  • (defun sleep nil nil)
Re: ==={challenge}=== Word Puzzle
« Reply #15 on: May 07, 2012, 04:18:59 PM »
(setq l (apply 'mapcar (cons 'list l)))

This blew my mind.  :-o
I didn't break it, I swear! ...ok, I broke it.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ==={challenge}=== Word Puzzle
« Reply #16 on: May 07, 2012, 04:29:00 PM »
This blew my mind.  :-o

I believe originally attributable to Mr. Tanzillo back in '91 +/-.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #17 on: May 07, 2012, 05:30:26 PM »
This blew my mind.  :-o

I believe originally attributable to Mr. Tanzillo back in '91 +/-.

Indeed, it's very difficult to find something that has no doubt already been done... but I thank you for the compliment Dashmonkey  :-)
« Last Edit: May 07, 2012, 06:06:48 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #18 on: May 07, 2012, 05:34:00 PM »
Nice one pBe :-)

Its the Bank Holiday weekend here in the UK, so I'll take a closer look at your code sometime this week  :-)

Cool beans.

Enjoy the holidiay and hurry back, You guys need to school me  on recursive coding.

Cheers Lee.  :-)

I just finished disecting your code Lee

Code - Auto/Visual Lisp: [Select]

That  is definitely a better approach for checking the list for the string .
And only now that i get to understand how vl-some works.

Kudos to you . Great programming skills.

Many thanks pBe, I'm delighted that my code was beneficial to your learning  :-)

I shall look over your code some time tomorrow to see how you tackled the puzzle  :wink:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ==={challenge}=== Word Puzzle
« Reply #19 on: May 08, 2012, 10:22:42 AM »
Indeed, it's very difficult to find something that has no doubt already been done...

Worry not, you've ammassed an enviable body of work that is original in concept and implementation. I was was just giving props to Tony. Subtitle: Don't let my brevity suggest or imply anything else. Big fan of your efforts my friend.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #20 on: May 08, 2012, 02:33:57 PM »
Indeed, it's very difficult to find something that has no doubt already been done...
Worry not, you've ammassed an enviable body of work that is original in concept and implementation. I was was just giving props to Tony. Subtitle: Don't let my brevity suggest or imply anything else. Big fan of your efforts my friend.

That's very kind of you to say Michael, thanks. As you know, I have learn a great deal from you and am also a big fan of your code and ideas. We all stand on the shoulders of giants  :-)


Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #21 on: May 08, 2012, 03:40:00 PM »
Its the Bank Holiday weekend here in the UK, so I'll take a closer look at your code sometime this week  :-)

Just finished looking over your code pBe - well done on a successful solution. There are a few places where the code could be made a little more concise and optimised, (for example, the lisp manipulation to generate the diagonals will generate some duplicate entries and some unnecessary entries in the list); but overall, a clear and methodical approach  :-)

LE3

  • Guest
Re: ==={challenge}=== Word Puzzle
« Reply #22 on: May 08, 2012, 11:14:34 PM »
did a revison and update to my solution - here it is again.
(compare and comparePoints functions are on my previous post):

Quote
Command: WORDPUZZLE
Select objects: Specify opposite corner: 25 found
Select objects:
Word found[BOX]at[SBOXC]
Word found[MAD]at[XMADX]
Word found[AMASS]at[AMASS]
Word found[LSP]at[XLSP]
Word found[EGG]at[SPEGG]
Word found[AXE]at[AXEAG]
Word found[ATTIC]at[ATTIC]
Word found[GLAD]at[GLADC]

Code - C++: [Select]
  1. static void LESQWordPuzzle_WORDPUZZLE(void)
  2. {
  3.         AcArray<AcString> words;
  4.         words.append(_T("BOX"));
  5.         words.append(_T("MAD"));
  6.         words.append(_T("AMASS"));
  7.         words.append(_T("LSP"));
  8.         words.append(_T("EGG"));
  9.         words.append(_T("AXE"));
  10.         words.append(_T("ATTIC"));
  11.         words.append(_T("GLAD"));
  12.  
  13.         ads_name ss;
  14.         resbuf *rbFilter = acutBuildList(RTDXF0, _T("TEXT"), RTNONE);
  15.         if (acedSSGet(NULL, NULL, NULL, rbFilter, ss) != RTNORM)
  16.         {
  17.                 acutRelRb(rbFilter); return;
  18.         }
  19.         acutRelRb(rbFilter);
  20.         long length = 0;
  21.         if ((acedSSLength(ss, &length) != RTNORM) || (length == 0))
  22.         {
  23.                 acedSSFree(ss); return;
  24.         }
  25.         AcArray<std::pair<AcString, AcGePoint3d>> allCharacters;
  26.         AcArray<std::pair<AcString, AcGePoint3d>> characters;
  27.         AcGePoint3dArray pts;
  28.         AcDbExtents ext;
  29.         AcDbObjectId id; ads_name ename;
  30.         for (int i = 0; i < length; i++)
  31.         {
  32.                 if (acedSSName(ss, i, ename) == RTNORM)
  33.                 {
  34.                         if (acdbGetObjectId(id, ename) == Acad::eOk)
  35.                         {
  36.                                 AcDbObjectPointer<AcDbText> pText(id, AcDb::kForRead);
  37.                                 if (pText.openStatus() == Acad::eOk)
  38.                                 {
  39.                                         AcString s = pText->textString();
  40.                                         for (int j = 0; j < words.length(); j++)
  41.                                         {
  42.                                                 AcString word = words[j];
  43.                                                 if (word.find(s) != -1)
  44.                                                 {
  45.                                                         pts.append(pText->alignmentPoint());
  46.                                                         ext.addPoint(pText->alignmentPoint());
  47.                                                         allCharacters.append(make_pair(s, pText->alignmentPoint()));
  48.                                                 }
  49.                                         }
  50.                                 }
  51.                         }
  52.                 }
  53.         }
  54.         if (allCharacters.length() > 0)
  55.         {
  56.                 AcDbBlockTableRecordPointer pBTR(acdbCurDwg()->currentSpaceId(), AcDb::kForWrite);
  57.                 if (pBTR.openStatus() != Acad::eOk) return;
  58.                 AcGePoint3d p1 = ext.minPoint();
  59.                 AcGePoint3d p3 = ext.maxPoint();
  60.                 AcGePoint3d p2(p3.x, p1.y, p1.z);
  61.                 AcGePoint3d p4(p1.x, p3.y, p1.z);
  62.                 AcGePoint3dArray pts_hor_top, pts_ver_left, pts_hor_bottom;
  63.                 for (int i = 0; i < pts.length(); i++)
  64.                 {
  65.                         if (pts[i].y == p4.y)
  66.                         {
  67.                                 if (!pts_hor_top.contains(pts[i]))
  68.                                 {
  69.                                         pts_hor_top.append(pts[i]);
  70.                                 }
  71.                         }
  72.                         if (pts[i].x == p4.x)
  73.                         {
  74.                                 if (!pts_ver_left.contains(pts[i]))
  75.                                 {
  76.                                         pts_ver_left.append(pts[i]);
  77.                                 }
  78.                         }
  79.                         if (pts[i].y == p1.y)
  80.                         {
  81.                                 if (!pts_hor_bottom.contains(pts[i]))
  82.                                 {
  83.                                         pts_hor_bottom.append(pts[i]);
  84.                                 }
  85.                         }
  86.                 }
  87.                 std::sort(pts_hor_top.asArrayPtr(), pts_hor_top.asArrayPtr() + pts_hor_top.length(), comparePoints);
  88.                 std::sort(pts_ver_left.asArrayPtr(), pts_ver_left.asArrayPtr() + pts_ver_left.length(), comparePoints);
  89.                 std::sort(pts_hor_bottom.asArrayPtr(), pts_hor_bottom.asArrayPtr() + pts_hor_bottom.length(), comparePoints);
  90.  
  91.                 // not same rows and columns
  92.                 if (pts_hor_top.length() != pts_ver_left.length()) return;
  93.                 int rows = pts_hor_top.length();
  94.                 double d = p4.distanceTo(p3) / (rows - 1);
  95.                 AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> horizontals, verticals, diagonals;
  96.                 AcArray<std::pair<AcString, AcGePoint3d>> horizontal, vertical, diagonal;
  97.                 // save horizontals up-down
  98.                 AcGePoint3d pt = p4;
  99.                 HorizontalSearch(rows, allCharacters, pt, horizontal, horizontals, d);
  100.                 // save verticals up-down
  101.                 pt = p4;
  102.                 VerticalSearch(rows, allCharacters, pt, vertical, verticals, d);
  103.  
  104.                 AcGeVector3d v1 = (p2 - p4).normalize();
  105.                 AcGeVector3d v2 = (p3 - p1).normalize();
  106.                 AcGePoint3d ptx(p4.x - d, p4.y - d, p4.z);
  107.                 double ddiag = p4.distanceTo(ptx);
  108.                 // save diagonals
  109.                 pts_hor_top.reverse();
  110.                 DiagonalSearch(pts_hor_top, pt, rows, allCharacters, diagonal, v1, ddiag, diagonals);
  111.                 pts_ver_left.removeAt(0); // remove first spot
  112.                 DiagonalSearch(pts_ver_left, pt, rows, allCharacters, diagonal, v1, ddiag, diagonals);
  113.                 pts_hor_bottom.reverse();
  114.                 DiagonalSearch(pts_hor_bottom, pt, rows, allCharacters, diagonal, v2, ddiag, diagonals);
  115.                 DiagonalSearch(pts_ver_left, pt, rows, allCharacters, diagonal, v2, ddiag, diagonals);
  116.  
  117.                 for (int i = 0; i < words.length(); i++)
  118.                 {
  119.                         AcString word = words[i];
  120.                         AcGePoint3d sp, ep;
  121.                         bool found = false;
  122.  
  123.                         // search on horizontals
  124.                         SearchOn(horizontals, word, sp, ep, found);
  125.                         if (found)
  126.                         {
  127.                                 AcDbObjectPointer<AcDbLine> pLine;
  128.                                 pLine.create();
  129.                                 pLine->setStartPoint(sp);
  130.                                 pLine->setEndPoint(ep);
  131.                                 pBTR->appendAcDbEntity(pLine);
  132.                         }
  133.  
  134.                         // search on verticals
  135.                         SearchOn(verticals, word, sp, ep, found);
  136.                         if (found)
  137.                         {
  138.                                 AcDbObjectPointer<AcDbLine> pLine;
  139.                                 pLine.create();
  140.                                 pLine->setStartPoint(sp);
  141.                                 pLine->setEndPoint(ep);
  142.                                 pBTR->appendAcDbEntity(pLine);
  143.                         }
  144.  
  145.                         SearchOn(diagonals, word, sp, ep, found);
  146.                         if (found)
  147.                         {
  148.                                 AcDbObjectPointer<AcDbLine> pLine;
  149.                                 pLine.create();
  150.                                 pLine->setStartPoint(sp);
  151.                                 pLine->setEndPoint(ep);
  152.                                 pBTR->appendAcDbEntity(pLine);
  153.                         }
  154.                 }
  155.         }
  156. }
  157.  

Code - C++: [Select]
  1. void CWordPuzzleApp::HorizontalSearch( int rows, AcArray<std::pair<AcString, AcGePoint3d>> &allCharacters, AcGePoint3d &pt, AcArray<std::pair<AcString, AcGePoint3d>> &horizontal, AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> &horizontals, double d )
  2. {
  3.         for (int row = 0; row < rows; row++)
  4.         {
  5.                 for (int i = 0; i < allCharacters.length(); i++)
  6.                 {
  7.                         AcGePoint3d ptSearch = allCharacters[i].second;
  8.                         if (pt.y == ptSearch.y)
  9.                         {
  10.                                 if (!horizontal.contains(allCharacters[i]) && (horizontal.length() < rows))
  11.                                 {
  12.                                         horizontal.append(allCharacters[i]);
  13.                                 }
  14.                         }
  15.                 }
  16.                 if (!horizontals.contains(horizontal))
  17.                 {
  18.                         std::sort(horizontal.asArrayPtr(), horizontal.asArrayPtr() + horizontal.length(), compare);
  19.                         horizontal.reverse();
  20.                         horizontals.append(horizontal);
  21.                 }
  22.                 horizontal.removeAll();
  23.                 pt.set(pt.x, pt.y - d, pt.z); // move down
  24.         }
  25. }
  26.  
  27. void CWordPuzzleApp::DiagonalSearch( AcGePoint3dArray &pts, AcGePoint3d &pt, int rows, AcArray<std::pair<AcString, AcGePoint3d>> &allCharacters, AcArray<std::pair<AcString, AcGePoint3d>> &diagonal, AcGeVector3d v1, double ddiag, AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> &diagonals )
  28. {
  29.         for (int j = 0; j < pts.length(); j++)
  30.         {
  31.                 pt = pts[j];
  32.                 for (int row = 0; row < rows; row++)
  33.                 {
  34.                         for (int i = 0; i < allCharacters.length(); i++)
  35.                         {
  36.                                 AcGePoint3d ptSearch = allCharacters[i].second;
  37.                                 if (pt.isEqualTo(ptSearch))
  38.                                 {
  39.                                         if (!diagonal.contains(allCharacters[i]))
  40.                                         {
  41.                                                 diagonal.append(allCharacters[i]);
  42.                                         }
  43.                                         break;
  44.                                 }
  45.                         }
  46.                         pt = pt + (v1 * ddiag);
  47.                 }
  48.                 if (!diagonals.contains(diagonal))
  49.                 {
  50.                         diagonals.append(diagonal);
  51.                 }
  52.                 diagonal.removeAll();
  53.         }
  54. }
  55.  
  56. void CWordPuzzleApp::VerticalSearch( int rows, AcArray<std::pair<AcString, AcGePoint3d>> &allCharacters, AcGePoint3d &pt, AcArray<std::pair<AcString, AcGePoint3d>> &vertical, AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> &verticals, double d )
  57. {
  58.         for (int row = 0; row < rows; row++)
  59.         {
  60.                 for (int i = 0; i < allCharacters.length(); i++)
  61.                 {
  62.                         AcGePoint3d ptSearch = allCharacters[i].second;
  63.                         if (pt.x == ptSearch.x)
  64.                         {
  65.                                 if (!vertical.contains(allCharacters[i]) && (vertical.length() < rows))
  66.                                 {
  67.                                         vertical.append(allCharacters[i]);
  68.                                 }
  69.                         }
  70.                 }
  71.                 if (!verticals.contains(vertical))
  72.                 {
  73.                         std::sort(vertical.asArrayPtr(), vertical.asArrayPtr() + vertical.length(), compare);
  74.                         verticals.append(vertical);
  75.                 }
  76.                 vertical.removeAll();
  77.                 pt.set(pt.x + d, pt.y - d, pt.z);
  78.         }
  79. }
  80.  
  81. void CWordPuzzleApp::SearchOn( AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> characters, AcString lookForWord, AcGePoint3d& sp, AcGePoint3d& ep, bool &found )
  82. {
  83.         found = false;
  84.         for (int i = 0; i < characters.length(); i++)
  85.         {
  86.                 if (found) { break; }
  87.                 AcArray<std::pair<AcString, AcGePoint3d>> fromLR, fromRL;
  88.                 fromLR = characters[i];
  89.                 fromRL = fromLR;
  90.                 AcString leftRight, rightLeft;
  91.                 int atHere = -1;
  92.                 for (int j = 0; j < fromLR.length(); j++)
  93.                 {
  94.                         AcString s = fromLR[j].first;
  95.                         leftRight = leftRight.concat(s);
  96.                 }
  97.                 fromRL.reverse();
  98.                 for (int j = 0; j < fromRL.length(); j++)
  99.                 {
  100.                         AcString s = fromRL[j].first;
  101.                         rightLeft = rightLeft.concat(s);
  102.                 }
  103.                 if ((atHere = leftRight.find(lookForWord)) != -1)
  104.                 {
  105.                         acutPrintf(_T("\nWord found[%s]at[%s] \n"), lookForWord.kACharPtr(), leftRight.kACharPtr());
  106.                         sp = fromLR[atHere].second;
  107.                         ep = fromLR[atHere+lookForWord.length()-1].second;
  108.                         leftRight.setEmpty();
  109.                         found = true;
  110.                         break;
  111.                 }
  112.                 else if ((atHere = rightLeft.find(lookForWord)) != -1)
  113.                 {
  114.                         acutPrintf(_T("\nWord found[%s]at[%s] \n"), lookForWord.kACharPtr(), rightLeft.kACharPtr());
  115.                         sp = fromRL[atHere].second;
  116.                         ep = fromRL[atHere+lookForWord.length()-1].second;
  117.                         rightLeft.setEmpty();
  118.                         found = true;
  119.                         break;
  120.                 }
  121.         }
  122. }
  123.  

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #23 on: May 08, 2012, 11:21:06 PM »

for example, the lisp manipulation to generate the diagonals will generate some duplicate entries and some unnecessary entries in the list); but overall, a clear and methodical approach  :-)

I knew you will see through that  :-) '((0 (0.0) ......)
Also, I realize that after i peep through your code. i shouldnt have to convert the strings to a list. the _check sub  would be a lot shorter  (AAMOF unnecessary) a function to read the collected strings (like the way you had it) is far better than comparing each element. the _DiagonalLines sub would also be optimised if i add another argument to read only the requried elements.

Thank you for the input and for testing code , I really learn a lot from your code Lee.

Now comes the hard part. With a given list of words create a puzzle using the longest word as the basis for the matrix. How difficult would that be to code?  Are you up to it?  8-)

Cheers
« Last Edit: May 08, 2012, 11:29:21 PM by pBe »

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #24 on: May 08, 2012, 11:35:34 PM »
did a revison and update to my solution - here it is again.
(compare and comparePoints functions are on my previous post):

How do you load this file format LE?
I'm getting this: \acad.exeUnable to load WordPuzzle18.arx  file.

LE3

  • Guest
Re: ==={challenge}=== Word Puzzle
« Reply #25 on: May 08, 2012, 11:42:21 PM »
did a revison and update to my solution - here it is again.
(compare and comparePoints functions are on my previous post):

How do you load this file format LE?
I'm getting this: \acad.exeUnable to load WordPuzzle18.arx  file.

it can be load by drag-&-drop or from the command appload
it was compiled to run into AutoCAD 2010 or 2011.

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #26 on: May 08, 2012, 11:58:14 PM »
it can be load by drag-&-drop or from the command appload
it was compiled to run into AutoCAD 2010 or 2011.

That explains it.
too bad.  pBe -> AutoCAD 2009 <- windows XP
« Last Edit: May 09, 2012, 12:31:38 AM by pBe »

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: ==={challenge}=== Word Puzzle
« Reply #27 on: May 09, 2012, 06:59:04 AM »
I knew you will see through that  :-) '((0 (0.0) ......)
Also, I realize that after i peep through your code. i shouldnt have to convert the strings to a list. the _check sub  would be a lot shorter  (AAMOF unnecessary) a function to read the collected strings (like the way you had it) is far better than comparing each element. the _DiagonalLines sub would also be optimised if i add another argument to read only the requried elements.

 :-)

Thank you for the input and for testing code , I really learn a lot from your code Lee.

You're welcome, I'm pleased that my code served a purpose other than solving the puzzle :-)

With a given list of words create a puzzle using the longest word as the basis for the matrix. How difficult would that be to code?

Very  :evil:

LE3

  • Guest
Re: ==={challenge}=== Word Puzzle
« Reply #28 on: May 09, 2012, 08:53:57 AM »
it can be load by drag-&-drop or from the command appload
it was compiled to run into AutoCAD 2010 or 2011.

That explains it.
too bad.  pBe -> AutoCAD 2009 <- windows XP

was able to compile the solution to run in that version, give it a try (can't tested here don't have A2009).

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Word Puzzle
« Reply #29 on: May 09, 2012, 11:58:31 PM »
With a given list of words create a puzzle using the longest word as the basis for the matrix. How difficult would that be to code?

Very  :evil:

 :-D I was thinking about it yesterdday. Guess you're right , though its  doable but not that easy.

was able to compile the solution to run in that version, give it a try (can't tested here don't have A2009).

Excellent work Luis .  Can't say I understand it at all, But  I'll start digging in to your code later and try to  figure out your method.

Word found[GLAD]at[GLADC]  <-- I like the way it prints  the result on the command prompt.