TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: pBe on May 04, 2012, 12:36:35 PM

Title: ==={challenge}=== Word Puzzle
Post by: pBe on May 04, 2012, 12:36:35 PM
Find these words and cross out with a line using the attached drawing file
"BOX"
"MAD"
"AMASS"
"LSP"
"EGG"
"AXE"
"ATTIC"
"GLAD"

I dont have a code yet. its just a spur of the moment thingy  :-D
Using a code of course .

I'l ltry to wirte one tonigt. and check in tommorow
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 04, 2012, 01:54:19 PM
My entry:

Code - Auto/Visual Lisp: [Select]
  1. ;; Solve Word Puzzle  -  Lee Mac
  2. ;; For Swamp Challenge:
  3. ;; http://www.theswamp.org/index.php?topic=41649.0
  4.  
  5. (defun c:solvewordpuzzle ( / e i l s words )
  6.  
  7.     (setq words
  8.        '(
  9.             "BOX"
  10.             "MAD"
  11.             "AMASS"
  12.             "LSP"
  13.             "EGG"
  14.             "AXE"
  15.             "ATTIC"
  16.             "GLAD"
  17.         )
  18.     )
  19.    
  20.     (if (setq s (ssget "_X" '((0 . "TEXT") (72 . 1) (73 . 2))))
  21.         (progn
  22.             (repeat (setq i (sslength s))
  23.                 (setq e (entget (ssname s (setq i (1- i))))
  24.                       l (cons (cons (cdr (assoc 1 e)) (cdr (assoc 11 e))) l)
  25.                 )
  26.             )
  27.             (setq l
  28.                 (apply 'append
  29.                     (mapcar
  30.                         (function
  31.                             (lambda ( a )
  32.                                 (if (cdr a)
  33.                                     (list a (reverse a))
  34.                                     (list a)
  35.                                 )
  36.                             )
  37.                         )
  38.                         (append
  39.                             (setq l
  40.                                 (mapcar
  41.                                     (function
  42.                                         (lambda ( a )
  43.                                             (vl-sort a
  44.                                                 (function
  45.                                                     (lambda ( a b ) (< (cadr a) (cadr b)))
  46.                                                 )
  47.                                             )
  48.                                         )
  49.                                     )
  50.                                     (vl-sort (LM:GroupByFunction l (lambda ( a b ) (equal (caddr a) (caddr b) 0.01)))
  51.                                         (function
  52.                                             (lambda ( a b ) (> (caddar a) (caddar b)))
  53.                                         )
  54.                                     )
  55.                                 )
  56.                             )
  57.                             (setq l (apply 'mapcar (cons 'list l)))
  58.                             (_getdiagonals l)
  59.                             (_getdiagonals (mapcar 'reverse l))
  60.                         )
  61.                     )
  62.                 )
  63.             )
  64.             (foreach word words
  65.                 (vl-some
  66.                     (function
  67.                         (lambda ( a / p )
  68.                             (if (setq p (vl-string-search word (apply 'strcat (mapcar 'car a))))
  69.                                 (entmake
  70.                                     (list
  71.                                        '(0 . "LINE")
  72.                                         (cons 10 (cdr (nth p a)))
  73.                                         (cons 11 (cdr (nth (+ p (1- (strlen word))) a)))
  74.                                        '(62 . 3)
  75.                                     )
  76.                                 )
  77.                             )
  78.                         )
  79.                     )
  80.                     l
  81.                 )
  82.             )
  83.         )
  84.     )
  85.     (princ)
  86. )
  87.  
  88. (defun _getdiagonals ( m / i l )
  89.     (setq l (1- (length m))
  90.           i -1
  91.     )
  92.     (mapcar (function (lambda ( x ) (apply 'append x)))
  93.         (apply 'mapcar
  94.             (cons 'list
  95.                 (mapcar
  96.                     (function
  97.                         (lambda ( a )
  98.                             (setq a (mapcar 'list a))
  99.                             (repeat (- l (setq i (1+ i))) (setq a (cons nil a)))
  100.                             (setq a (reverse a))
  101.                             (repeat i (setq a (cons nil a)))
  102.                             (reverse a)
  103.                         )
  104.                     )
  105.                     m
  106.                 )
  107.             )
  108.         )
  109.     )        
  110. )
  111.  
  112. ;; Group By Function  -  Lee Mac
  113. ;; Groups items considered equal by a given predicate function
  114.  
  115. (defun LM:GroupByFunction ( lst fun / tmp1 tmp2 x1 )
  116.     (if (setq x1 (car lst))
  117.         (progn
  118.             (foreach x2 (cdr lst)
  119.                 (if (fun x1 x2)
  120.                     (setq tmp1 (cons x2 tmp1))
  121.                     (setq tmp2 (cons x2 tmp2))
  122.                 )
  123.             )
  124.             (cons (cons x1 tmp1) (LM:GroupByFunction tmp2 fun))
  125.         )
  126.     )
  127. )
  128.  
Title: Re: ==={challenge}=== Word Puzzle
Post by: kruuger on May 04, 2012, 02:51:06 PM
My entry:
W O W  :-o
Lee could give a short description how it works?
thanks
kruuger
Title: Re: ==={challenge}=== Word Puzzle
Post by: LE3 on May 04, 2012, 03:05:14 PM
reading the description above your avatar - indeed and true +1

bet that now you even dream with all the solutions on your mind, and can't wait to wake up > to start coding <

molto bene signore.
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 04, 2012, 04:55:29 PM
W O W  :-o
Lee could give a short description how it works?
thanks
kruuger

Thanks Kruuger, you're too kind  :-)

The method is actually rather simple, print out the list variable 'l' before the foreach loop and a keen programmer such as yourself will see right through it  :-)

Of course, if you want further explanation, I would be more than happy to oblige.
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 04, 2012, 04:58:25 PM
reading the description above your avatar - indeed and true +1

bet that now you even dream with all the solutions on your mind, and can't wait to wake up > to start coding <

"Needs a day job"

I admit, I've long been addicted to this programming and can't resist a challenge  8-)

molto bene signore.

Cheers Luis  :-)
Title: Re: ==={challenge}=== Word Puzzle
Post by: KewlToyZ on May 04, 2012, 05:27:26 PM
reading the description above your avatar - indeed and true +1

bet that now you even dream with all the solutions on your mind, and can't wait to wake up > to start coding <

"Needs a day job"

I admit, I've long been addicted to this programming and can't resist a challenge  8-)

molto bene signore.

Cheers Luis  :-)

I see your stuff in all of the CAD forums lol!
Quote
(foreach word words
                (vl-some
                    (function
That messes with my head =P
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 05, 2012, 12:49:24 AM
Incredible.
I'm not even halfway done with mine. Having trouble with diagonal search.

Trying not to look  at your code. I copeid and paste it with my eyes close  :-D

I'll post mine when i'm done.

Again. Incredible
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 05, 2012, 05:38:40 AM
I see your stuff in all of the CAD forums lol!

For a short time I participated in a number of different CAD forums, ADG / AUGI, but since, in my experience, TheSwamp is so much better (and I'm not just talking about the much cleaner / clearer layout of the site, but the more friendly and jovial atmosphere we have here and an incredibly knowledgeable community too) that I now tend to spend the majority of my time here.

Quote
(foreach word words
                (vl-some
                    (function
That messes with my head =P

 :lol:
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 05, 2012, 05:43:10 AM
Incredible.
I'm not even halfway done with mine. Having trouble with diagonal search.

Trying not to look  at your code. I copeid and paste it with my eyes close  :-D

I'll post mine when i'm done.

Again. Incredible

Cheers pBe, that's very kind. I look forward to seeing your alternative solution! 
Its always interesting to see how different minds tackle a problem :-)

Also, thanks for posting the Challenge, I really enjoyed writing this one.
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 06, 2012, 02:50:04 AM
Cheers pBe, that's very kind. I look forward to seeing your alternative solution! 
Its always interesting to see how different minds tackle a problem :-)

Also, thanks for posting the Challenge, I really enjoyed writing this one.

Glad you like it Lee. It didt take you long to come up with a solution though  :-D
I'm still trying to figure out the diagonal search.. I have an idea how to go about it but cant put it on code yet.

Here's what i have so far.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:solveit (/ _build _HiLow stp Where is it crossed ss Olist)
  2. ;;;                                                             ;;;;
  3. (defun _build (lst_ ref mode / func)
  4. (setq Func (if (zerop mode)
  5.                (list (cadr ref) cadadr caadr)
  6.                (list (car ref) caadr cadadr)))
  7.               (vl-remove-if-not
  8.                     '(lambda (w) (eq ((cadr func) w) (car func)))
  9.                     lst_)
  10.       )
  11. (defun _HiLow  (lev lev2 lst)
  12.       (list
  13.             (apply lev (mapcar 'car lst))
  14.             (apply lev2 (mapcar 'cadr lst))
  15.             ))
  16. (defun _check (w ln / run f woot i)
  17. (while (and ln (not woot)
  18.        (setq run (member (car w)  (mapcar 'car ln))))
  19.        (if (and (>= (length run)(length w))
  20.                 (apply '= (mapcar '(lambda (k s)
  21.                                 (if (= k s)1 0)) w run )))
  22.            (foreach l w
  23.                  (setq woot (cons (assoc l ln) woot)
  24.                          ln (member (assoc l ln) ln)
  25.                          ln (vl-remove (assoc l ln) ln)))
  26.            (setq ln (if run (vl-remove (assoc (car run) ln) ln) nil))
  27.            )
  28.         ) woot
  29.       )
  30. (setq  _ReList (lambda  (lst_ num / a i)
  31.       (setq n (/ num 2)  i -1)
  32.       (setq a (member (nth n lst_) lst_))
  33.       (repeat n
  34.             (setq a (append a (list (nth (setq i (1+ i)) lst_)))))
  35.       a ))
  36. (defun _DiagonalLines ( r lst / s l)
  37. (setq s -1 _repf (lambda  (i n / x te )
  38.       (repeat r  (setq x (cons  (if (setq te (nth (setq i (1+ i))
  39.                        (nth (setq n (1+ n)) lst))) te '(0 (0.0))) x))) x))
  40. (repeat (1- r) (setq v (_repf s -1)  l (cons v l) s (1+ s)))
  41.       l)      
  42. ;;;                                                             ;;;      
  43. (setq wlist '("BOX" "MAD"  "AMASS" "LSP" "EGG" "AXE" "ATTIC" "GLAD"))
  44. (setq Olist nil
  45.       ss (ssget "_X" '((0 . "TEXT")(1 . "@"))))
  46. (repeat (setq i (sslength ss))
  47.       (setq Olist (cons (list (ascii (cdr (assoc 1 (setq e  (entget
  48.                 (ssname ss (setq i (1- i))))))))
  49.                               (cdr (assoc 11 e)))
  50.                         Olist))
  51.       )
  52. (setq Where (apply 'append
  53. (mapcar '(lambda (h / x ln)
  54.                (setq lst (eval (cadr h)) f (eval (last h)))
  55.                (while (and (setq stp (_HiLow 'min
  56.                                                'max
  57.                                                (mapcar 'cadr lst)))
  58.                            (setq x (_build lst stp (car h)))
  59.                            (setq ln (cons (vl-sort x '(lambda (x1 x2)(< (f x1)(f x2))))
  60.                                            ln))
  61.                            (foreach
  62.                                   il  x
  63.                                  (setq lst (vl-remove il lst)))))
  64.                ln)
  65.         (list '(0 Olist caadr) '(1 Olist cadadr)))
  66.                   )
  67.       )
  68. (setq row (length (cadr where))
  69.       where (apply 'append (list (_DiagonalLines row where)
  70.                                  (_DiagonalLines  row (_ReList where (length where)))
  71.                                  (cdr (_DiagonalLines  row (mapcar 'reverse where)))
  72.                                  (cdr (_DiagonalLines  row (_ReList (reverse
  73.                                         (mapcar 'reverse where))(length where))))
  74.                                   where)))
  75. (foreach wl (mapcar 'vl-string->list wlist)
  76.       (setq is Where crossed nil)
  77.       (while (and (setq it (car is))(null crossed))
  78.            (if  (setq z (if (setq y (_check wl it))
  79.                                   y (_check (reverse wl) it)))
  80.                 (setq crossed
  81.                         (entmake  (list '(0 . "LINE")
  82.                         (cons 10 (cadar z))
  83.                         (cons 11 (cadr (last z)))
  84.                         ))
  85.                         )
  86.                 )
  87.                     (setq is (cdr is))
  88.                 )
  89.       )(princ)
  90. )

Maybe later today I'll update it and post the complete code later  (hopefully)
 <<<<  I need an aspirin >>>>   :laugh:

Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 07, 2012, 04:00:39 AM
I updated the code , but it only works for even number of rows and columnc i.e 5x5 6x6....

And i'm still missing the upper left quadrant search.
I know its there somewhere , but cant quite put my finger on it.


Got it.

Code - Auto/Visual Lisp: [Select]
  1. (cdr (_DiagonalLines  row (_ReList (reverse
  2.                                         (mapcar 'reverse where))(length where))))

Also i tried a recursive version for this

Code - Auto/Visual Lisp: [Select]
  1. (defun _check (w ln / run f woot i)
  2. (while (and ln (not woot)
  3.        (setq run (member (car w)  (mapcar 'car ln))))
  4.        (if (and (>= (length run)(length w))
  5.                 (apply '= (mapcar '(lambda (k s)
  6.                                 (if (= k s)1 0)) w run )))
  7.            (foreach l w
  8.                  (setq woot (cons (assoc l ln) woot)
  9.                          ln (member (assoc l ln) ln)
  10.                          ln (vl-remove (assoc l ln) ln)))
  11.            (setq ln (if run (vl-remove (assoc (car run) ln) ln) nil))
  12.            )
  13.         ) woot
  14.       )

But i failed miserably.


CODE COMPLETED... just need 2 bottles of beer for my brain to work overtime  :-D
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 07, 2012, 10:56:58 AM
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  :-)
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 07, 2012, 11:11:22 AM
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.
Title: Re: ==={challenge}=== Word Puzzle
Post by: LE3 on May 07, 2012, 03:37:54 PM
lost all my lsp powers - so -> will try to make an arx version of my own > and post the solution here <  edit: here it is...

have not been able to post any code lately so, got some time to play and did some practice - here it goes.
you can see the difference between how a task can be done in autolisp versus arx/c++ > hope that helps <

edit2: it needs to be updated for cases with different number of rows and columns >todo in a possible future<

Code - C++: [Select]
  1. [code]
  2. #include "StdAfx.h"
  3. #include "resource.h"
  4. using namespace std;
  5. using std::map;
  6. #include <algorithm>
  7. #include <vector>
  8.  
  9. bool comparePoints (const AcGePoint3d a, const AcGePoint3d b)
  10. {
  11.         if (a.x == b.x)
  12.         {
  13.                 if (a.y == b.y)
  14.                         return (a.z > b.z);
  15.                 else
  16.                         return (a.y > b.y);
  17.         }
  18.         else
  19.                 return (a.x > b.x);
  20. }
  21.  
  22. bool compare(std::pair<AcString, AcGePoint3d> pairOne, std::pair<AcString, AcGePoint3d> pairTwo)
  23. {
  24.         //return pairOne.first < pairTwo.first; // sort alphabetically
  25.         AcGePoint3d a = pairOne.second, b = pairTwo.second;
  26.         if (a.x == b.x)
  27.         {
  28.                 if (a.y == b.y)
  29.                         return (a.z > b.z);
  30.                 else
  31.                         return (a.y > b.y);
  32.         }
  33.         else
  34.                 return (a.x > b.x);
  35. }
  36.  
  37. static void LESQWordPuzzle_WORDPUZZLE(void)
  38. {
  39.         AcArray<AcString> words;
  40.         words.append(_T("BOX"));
  41.         words.append(_T("MAD"));
  42.         words.append(_T("AMASS"));
  43.         words.append(_T("LSP"));
  44.         words.append(_T("EGG"));
  45.         words.append(_T("AXE"));
  46.         words.append(_T("ATTIC"));
  47.         words.append(_T("GLAD"));
  48.         ads_name ss;
  49.         resbuf *rbFilter = acutBuildList(RTDXF0, _T("TEXT"), RTNONE);
  50.         if (acedSSGet(NULL, NULL, NULL, rbFilter, ss) != RTNORM)
  51.         {
  52.                 acutRelRb(rbFilter); return;
  53.         }
  54.         acutRelRb(rbFilter);
  55.         long length = 0;
  56.         if ((acedSSLength(ss, &length) != RTNORM) || (length == 0))
  57.         {
  58.                 acedSSFree(ss); return;
  59.         }
  60.         AcArray<std::pair<AcString, AcGePoint3d>> allCharacters;
  61.         AcArray<std::pair<AcString, AcGePoint3d>> characters;
  62.         AcArray<AcString> justStrings;
  63.         AcGePoint3dArray pts;
  64.         AcDbExtents ext;
  65.         AcDbObjectId id; ads_name ename;
  66.         for (int i = 0; i < length; i++)
  67.         {
  68.                 if (acedSSName(ss, i, ename) == RTNORM)
  69.                 {
  70.                         if (acdbGetObjectId(id, ename) == Acad::eOk)
  71.                         {
  72.                                 AcDbObjectPointer<AcDbText> pText(id, AcDb::kForRead);
  73.                                 if (pText.openStatus() == Acad::eOk)
  74.                                 {
  75.                                         AcString s = pText->textString();
  76.                                         for (int j = 0; j < words.length(); j++)
  77.                                         {
  78.                                                 AcString word = words[j];
  79.                                                 if (word.find(s) != -1)
  80.                                                 {
  81.                                                         pts.append(pText->alignmentPoint());
  82.                                                         ext.addPoint(pText->alignmentPoint());
  83.                                                         allCharacters.append(make_pair(s, pText->alignmentPoint()));
  84.                                                 }
  85.                                         }
  86.                                 }
  87.                         }
  88.                 }
  89.         }
  90.         if (allCharacters.length() > 0)
  91.         {
  92.                 AcDbBlockTableRecordPointer pBTR(acdbCurDwg()->currentSpaceId(), AcDb::kForWrite);
  93.                 if (pBTR.openStatus() != Acad::eOk) return;
  94.                 AcGePoint3d p1 = ext.minPoint();
  95.                 AcGePoint3d p3 = ext.maxPoint();
  96.                 AcGePoint3d p2(p3.x, p1.y, p1.z);
  97.                 AcGePoint3d p4(p1.x, p3.y, p1.z);
  98.                 AcGePoint3dArray pts_hor_top, pts_ver_left, pts_hor_bottom;
  99.                 for (int i = 0; i < pts.length(); i++)
  100.                 {
  101.                         if (pts[i].y == p4.y)
  102.                         {
  103.                                 if (!pts_hor_top.contains(pts[i]))
  104.                                 {
  105.                                         pts_hor_top.append(pts[i]);
  106.                                 }
  107.                         }
  108.                         if (pts[i].x == p4.x)
  109.                         {
  110.                                 if (!pts_ver_left.contains(pts[i]))
  111.                                 {
  112.                                         pts_ver_left.append(pts[i]);
  113.                                 }
  114.                         }
  115.                         if (pts[i].y == p1.y)
  116.                         {
  117.                                 if (!pts_hor_bottom.contains(pts[i]))
  118.                                 {
  119.                                         pts_hor_bottom.append(pts[i]);
  120.                                 }
  121.                         }
  122.                 }
  123.                 std::sort(pts_hor_top.asArrayPtr(), pts_hor_top.asArrayPtr() + pts_hor_top.length(), comparePoints);
  124.                 std::sort(pts_ver_left.asArrayPtr(), pts_ver_left.asArrayPtr() + pts_ver_left.length(), comparePoints);
  125.                 std::sort(pts_hor_bottom.asArrayPtr(), pts_hor_bottom.asArrayPtr() + pts_hor_bottom.length(), comparePoints);
  126.                 int rows = pts_hor_top.length();
  127.                 int columns = rows;
  128.                 double d = p4.distanceTo(p3) / (rows - 1);
  129.                 AcArray<AcArray<std::pair<AcString, AcGePoint3d>>> horizontals, verticals, diagonals;
  130.                 AcArray<std::pair<AcString, AcGePoint3d>> horizontal, vertical, diagonal;
  131.                 // save horizontals up-down
  132.                 AcGePoint3d pt = p4;
  133.                 for (int row = 0; row < rows; row++)
  134.                 {
  135.                         for (int i = 0; i < allCharacters.length(); i++)
  136.                         {
  137.                                 AcGePoint3d ptSearch = allCharacters[i].second;
  138.                                 if (pt.y == ptSearch.y)
  139.                                 {
  140.                                         if (!horizontal.contains(allCharacters[i]) && (horizontal.length() < rows))
  141.                                         {
  142.                                                 horizontal.append(allCharacters[i]);
  143.                                         }
  144.                                 }
  145.                         }
  146.                         if (!horizontals.contains(horizontal))
  147.                         {
  148.                                 std::sort(horizontal.asArrayPtr(), horizontal.asArrayPtr() + horizontal.length(), compare);
  149.                                 horizontal.reverse();
  150.                                 horizontals.append(horizontal);
  151.                         }
  152.                         horizontal.removeAll();
  153.                         pt.set(pt.x, pt.y - d, pt.z); // move down
  154.                 }
  155.                 // save verticals up-down
  156.                 pt = p4;
  157.                 for (int row = 0; row < rows; row++)
  158.                 {
  159.                         for (int i = 0; i < allCharacters.length(); i++)
  160.                         {
  161.                                 AcGePoint3d ptSearch = allCharacters[i].second;
  162.                                 if (pt.x == ptSearch.x)
  163.                                 {
  164.                                         if (!vertical.contains(allCharacters[i]) && (vertical.length() < rows))
  165.                                         {
  166.                                                 vertical.append(allCharacters[i]);
  167.                                         }
  168.                                 }
  169.                         }
  170.                         if (!verticals.contains(vertical))
  171.                         {
  172.                                 std::sort(vertical.asArrayPtr(), vertical.asArrayPtr() + vertical.length(), compare);
  173.                                 verticals.append(vertical);
  174.                         }
  175.                         vertical.removeAll();
  176.                         pt.set(pt.x + d, pt.y - d, pt.z);
  177.                 }
  178.                 AcGeVector3d v1 = (p2 - p4).normalize();
  179.                 AcGeVector3d v2 = (p3 - p1).normalize();
  180.                 AcGePoint3d ptx(p4.x - d, p4.y - d, p4.z);
  181.                 double ddiag = p4.distanceTo(ptx);
  182.                 // save diagonals
  183.                 pts_hor_top.reverse();
  184.                 for (int j = 0; j < pts_hor_top.length(); j++)
  185.                 {
  186.                         pt = pts_hor_top[j];
  187.                         for (int row = 0; row < rows; row++)
  188.                         {
  189.                                 for (int i = 0; i < allCharacters.length(); i++)
  190.                                 {
  191.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  192.                                         if (pt.isEqualTo(ptSearch))
  193.                                         {
  194.                                                 if (!diagonal.contains(allCharacters[i]))
  195.                                                 {
  196.                                                         diagonal.append(allCharacters[i]);
  197.                                                 }
  198.                                                 break;
  199.                                         }
  200.                                 }
  201.                                 pt = pt + (v1 * ddiag);
  202.                         }
  203.                         if (!diagonals.contains(diagonal))
  204.                         {
  205.                                 diagonals.append(diagonal);
  206.                         }
  207.                         diagonal.removeAll();
  208.                 }
  209.                 pts_ver_left.removeAt(0); // remove first spot
  210.                 for (int j = 0; j < pts_ver_left.length(); j++)
  211.                 {
  212.                         pt = pts_ver_left[j];
  213.                         diagonal.removeAll();
  214.                         for (int row = 0; row < rows; row++)
  215.                         {
  216.                                 for (int i = 0; i < allCharacters.length(); i++)
  217.                                 {
  218.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  219.                                         if (pt.isEqualTo(ptSearch))
  220.                                         {
  221.                                                 if (!diagonal.contains(allCharacters[i]))
  222.                                                 {
  223.                                                         diagonal.append(allCharacters[i]);
  224.                                                 }
  225.                                                 break;
  226.                                         }
  227.                                 }
  228.                                 pt = pt + (v1 * ddiag);
  229.                         }
  230.                         if (!diagonals.contains(diagonal))
  231.                         {
  232.                                 diagonals.append(diagonal);
  233.                         }
  234.                         diagonal.removeAll();
  235.                 }
  236.                 // save diagonals
  237.                 pts_hor_bottom.reverse();
  238.                 for (int j = 0; j < pts_hor_bottom.length(); j++)
  239.                 {
  240.                         pt = pts_hor_bottom[j];
  241.                         diagonal.removeAll();
  242.                         for (int row = 0; row < rows; row++)
  243.                         {
  244.                                 for (int i = 0; i < allCharacters.length(); i++)
  245.                                 {
  246.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  247.                                         if (pt.isEqualTo(ptSearch))
  248.                                         {
  249.                                                 if (!diagonal.contains(allCharacters[i]))
  250.                                                 {
  251.                                                         diagonal.append(allCharacters[i]);
  252.                                                 }
  253.                                                 break;
  254.                                         }
  255.                                 }
  256.                                 pt = pt + (v2 * ddiag);
  257.                         }
  258.                         if (!diagonals.contains(diagonal))
  259.                         {
  260.                                 diagonals.append(diagonal);
  261.                         }
  262.                         diagonal.removeAll();
  263.                 }
  264.                 for (int j = 0; j < pts_ver_left.length(); j++)
  265.                 {
  266.                         pt = pts_ver_left[j];
  267.                         diagonal.removeAll();
  268.                         for (int row = 0; row < rows; row++)
  269.                         {
  270.                                 for (int i = 0; i < allCharacters.length(); i++)
  271.                                 {
  272.                                         AcGePoint3d ptSearch = allCharacters[i].second;
  273.                                         if (pt.isEqualTo(ptSearch))
  274.                                         {
  275.                                                 if (!diagonal.contains(allCharacters[i]))
  276.                                                 {
  277.                                                         diagonal.append(allCharacters[i]);
  278.                                                 }
  279.                                                 break;
  280.                                         }
  281.                                 }
  282.                                 pt = pt + (v2 * ddiag);
  283.                         }
  284.                         if (!diagonals.contains(diagonal))
  285.                         {
  286.                                 diagonals.append(diagonal);
  287.                         }
  288.                         diagonal.removeAll();
  289.                 }
  290.                 // look for the word
  291.                 for (int k = 0; k < words.length(); k++)
  292.                 {
  293.                         AcString word = words[k];
  294.                         AcArray<AcString> temp;
  295.                         AcGePoint3d sp, ep;
  296.                         bool found = false;
  297.                         AcString firstChar = word.substr(0, 1);
  298.                         AcString lastChar = word.substr(word.length() - 1, 1);
  299.                         // search on horizontals
  300.                         for (int i = 0; i < horizontals.length(); i++)
  301.                         {
  302.                                 AcArray<std::pair<AcString, AcGePoint3d>> row_horizontal = horizontals[i];
  303.                                 temp.removeAll();
  304.                                 for (int j = 0; j < row_horizontal.length(); j++)
  305.                                 {
  306.                                         AcString s = row_horizontal[j].first;
  307.                                         if (word.find(s) != -1)
  308.                                         {
  309.                                                 temp.append(s);
  310.                                         }
  311.                                 }
  312.                                 int counter = 0;
  313.                                 bool hasDupplicates = false;
  314.                                 bool atTheEnd = false;
  315.                                 bool startLeftRight = false;
  316.                                 AcArray<AcString> chars;
  317.                                 for (int c = 0; c < word.length(); c++)
  318.                                 {
  319.                                         AcString ch = word.substr(c, 1);
  320.                                         chars.append(ch);
  321.                                         if (temp.contains(ch))
  322.                                         {
  323.                                                 counter++;
  324.                                         }
  325.                                 }
  326.                                 for (int i = 0; i < chars.length(); i++)
  327.                                 {
  328.                                         AcString s = chars[i];
  329.                                         for (int ii = 0; ii < chars.length(); ii++)
  330.                                         {
  331.                                                 if (chars[ii] == s)
  332.                                                 {
  333.                                                         if (chars[ii+1] == s)
  334.                                                         {
  335.                                                                 hasDupplicates = true;
  336.                                                                 if (ii+1 == (chars.length() - 1))
  337.                                                                 {
  338.                                                                         atTheEnd = true;
  339.                                                                 }
  340.                                                                 break;
  341.                                                         }
  342.                                                 }
  343.                                         }
  344.                                 }
  345.                                 if (temp.length() >= word.length() && counter >= word.length())
  346.                                 {
  347.                                         if (temp[0] == firstChar && temp[1] == word.substr(1, 1))
  348.                                         {
  349.                                                 startLeftRight = true;
  350.                                         }
  351.                                         else
  352.                                         {
  353.                                                 startLeftRight = false;
  354.                                         }
  355.                                         bool foundSP = false;
  356.                                         bool foundEP = false;
  357.                                         for (int i = 0; i < row_horizontal.length(); i++)
  358.                                         {
  359.                                                 if (row_horizontal[i].first == firstChar)
  360.                                                 {
  361.                                                         sp = row_horizontal[i].second;
  362.                                                         foundSP = true;
  363.                                                         break;
  364.                                                 }
  365.                                         }
  366.                                         for (int i = 0; i < row_horizontal.length(); i++)
  367.                                         {
  368.                                                 if (row_horizontal[i].first == lastChar)
  369.                                                 {
  370.                                                         ep = row_horizontal[i].second;
  371.                                                         if (atTheEnd)
  372.                                                         {
  373.                                                                 ep = row_horizontal[i+1].second;
  374.                                                         }
  375.                                                         foundEP = true;
  376.                                                         break;
  377.                                                 }
  378.                                         }
  379.                                         if (foundSP && foundEP && startLeftRight)
  380.                                         {
  381.                                                 AcDbObjectPointer<AcDbLine> pLine;
  382.                                                 pLine.create();
  383.                                                 pLine->setStartPoint(sp);
  384.                                                 pLine->setEndPoint(ep);
  385.                                                 pBTR->appendAcDbEntity(pLine);
  386.                                         }
  387.                                         else
  388.                                         {
  389.                                                 row_horizontal.reverse();
  390.                                                 for (int i = 0; i < row_horizontal.length(); i++)
  391.                                                 {
  392.                                                         if (row_horizontal[i].first == firstChar)
  393.                                                         {
  394.                                                                 sp = row_horizontal[i].second;
  395.                                                                 break;
  396.                                                         }
  397.                                                 }
  398.                                                 for (int i = 0; i < row_horizontal.length(); i++)
  399.                                                 {
  400.                                                         if (row_horizontal[i].first == lastChar)
  401.                                                         {
  402.                                                                 ep = row_horizontal[i].second;
  403.                                                                 if (atTheEnd)
  404.                                                                 {
  405.                                                                         ep = row_horizontal[i+1].second;
  406.                                                                 }
  407.                                                                 break;
  408.                                                         }
  409.                                                 }
  410.                                                 AcDbObjectPointer<AcDbLine> pLine;
  411.                                                 pLine.create();
  412.                                                 pLine->setStartPoint(sp);
  413.                                                 pLine->setEndPoint(ep);
  414.                                                 pBTR->appendAcDbEntity(pLine);
  415.                                         }
  416.                                         break;
  417.                                 }
  418.                         }
  419.                         // search on verticals
  420.                         for (int i = 0; i < verticals.length(); i++)
  421.                         {
  422.                                 AcArray<std::pair<AcString, AcGePoint3d>> row_vertical = verticals[i];
  423.                                 temp.removeAll();
  424.                                 for (int j = 0; j < row_vertical.length(); j++)
  425.                                 {
  426.                                         AcString s = row_vertical[j].first;
  427.                                         if (word.find(s) != -1)
  428.                                         {
  429.                                                 temp.append(s);
  430.                                         }
  431.                                 }
  432.                                 int counter = 0;
  433.                                 bool hasDupplicates = false;
  434.                                 bool atTheEnd = false;
  435.                                 bool startLeftRight = false;
  436.                                 AcArray<AcString> chars;
  437.                                 for (int c = 0; c < word.length(); c++)
  438.                                 {
  439.                                         AcString ch = word.substr(c, 1);
  440.                                         chars.append(ch);
  441.                                         if (temp.contains(ch))
  442.                                         {
  443.                                                 counter++;
  444.                                         }
  445.                                 }
  446.                                 for (int i = 0; i < chars.length(); i++)
  447.                                 {
  448.                                         AcString s = chars[i];
  449.                                         for (int ii = 0; ii < chars.length(); ii++)
  450.                                         {
  451.                                                 if (chars[ii] == s)
  452.                                                 {
  453.                                                         if (chars[ii+1] == s)
  454.                                                         {
  455.                                                                 hasDupplicates = true;
  456.                                                                 if (ii+1 == (chars.length() - 1))
  457.                                                                 {
  458.                                                                         atTheEnd = true;
  459.                                                                 }
  460.                                                                 break;
  461.                                                         }
  462.                                                 }
  463.                                         }
  464.                                 }
  465.                                 if (temp.length() >= word.length() && counter >= word.length())
  466.                                 {
  467.                                         if (temp[0] == firstChar && temp[1] == word.substr(1, 1))
  468.                                         {
  469.                                                 startLeftRight = true;
  470.                                         }
  471.                                         else
  472.                                         {
  473.                                                 startLeftRight = false;
  474.                                         }
  475.                                         bool foundSP = false;
  476.                                         bool foundEP = false;
  477.                                         for (int i = 0; i < row_vertical.length(); i++)
  478.                                         {
  479.                                                 if (row_vertical[i].first == firstChar)
  480.                                                 {
  481.                                                         sp = row_vertical[i].second;
  482.                                                         foundSP = true;
  483.                                                         break;
  484.                                                 }
  485.                                         }
  486.                                         for (int i = 0; i < row_vertical.length(); i++)
  487.                                         {
  488.                                                 if (row_vertical[i].first == lastChar)
  489.                                                 {
  490.                                                         ep = row_vertical[i].second;
  491.                                                         if (atTheEnd)
  492.                                                         {
  493.                                                                 ep = row_vertical[i+1].second;
  494.                                                         }
  495.                                                         foundEP = true;
  496.                                                         break;
  497.                                                 }
  498.                                         }
  499.                                         if (foundSP && foundEP && startLeftRight)
  500.                                         {
  501.                                                 AcDbObjectPointer<AcDbLine> pLine;
  502.                                                 pLine.create();
  503.                                                 pLine->setStartPoint(sp);
  504.                                                 pLine->setEndPoint(ep);
  505.                                                 pBTR->appendAcDbEntity(pLine);
  506.                                         }
  507.                                         else
  508.                                         {
  509.                                                 row_vertical.reverse();
  510.                                                 for (int i = 0; i < row_vertical.length(); i++)
  511.                                                 {
  512.                                                         if (row_vertical[i].first == firstChar)
  513.                                                         {
  514.                                                                 sp = row_vertical[i].second;
  515.                                                                 break;
  516.                                                         }
  517.                                                 }
  518.                                                 for (int i = 0; i < row_vertical.length(); i++)
  519.                                                 {
  520.                                                         if (row_vertical[i].first == lastChar)
  521.                                                         {
  522.                                                                 ep = row_vertical[i].second;
  523.                                                                 if (atTheEnd)
  524.                                                                 {
  525.                                                                         ep = row_vertical[i+1].second;
  526.                                                                 }
  527.                                                                 break;
  528.                                                         }
  529.                                                 }
  530.                                                 AcDbObjectPointer<AcDbLine> pLine;
  531.                                                 pLine.create();
  532.                                                 pLine->setStartPoint(sp);
  533.                                                 pLine->setEndPoint(ep);
  534.                                                 pBTR->appendAcDbEntity(pLine);
  535.                                         }
  536.                                 }
  537.                                 temp.removeAll();
  538.                         }
  539.                         // search on diagonals
  540.                         for (int i = 0; i < diagonals.length(); i++)
  541.                         {
  542.                                 AcArray<std::pair<AcString, AcGePoint3d>> row_diagonal = diagonals[i];
  543.                                 temp.removeAll();
  544.                                 for (int j = 0; j < row_diagonal.length(); j++)
  545.                                 {
  546.                                         AcString s = row_diagonal[j].first;
  547.                                         if (word.find(s) != -1)
  548.                                         {
  549.                                                 temp.append(s);
  550.                                         }
  551.                                 }
  552.                                 int counter = 0;
  553.                                 bool hasDupplicates = false;
  554.                                 bool atTheEnd = false;
  555.                                 bool startLeftRight = false;
  556.                                 AcArray<AcString> chars;
  557.                                 for (int c = 0; c < word.length(); c++)
  558.                                 {
  559.                                         AcString ch = word.substr(c, 1);
  560.                                         chars.append(ch);
  561.                                         if (temp.contains(ch))
  562.                                         {
  563.                                                 counter++;
  564.                                         }
  565.                                 }
  566.                                 for (int i = 0; i < chars.length(); i++)
  567.                                 {
  568.                                         AcString s = chars[i];
  569.                                         for (int ii = 0; ii < chars.length(); ii++)
  570.                                         {
  571.                                                 if (chars[ii] == s)
  572.                                                 {
  573.                                                         if (chars[ii+1] == s)
  574.                                                         {
  575.                                                                 hasDupplicates = true;
  576.                                                                 if (ii+1 == (chars.length() - 1))
  577.                                                                 {
  578.                                                                         atTheEnd = true;
  579.                                                                 }
  580.                                                                 break;
  581.                                                         }
  582.                                                 }
  583.                                         }
  584.                                 }
  585.                                 if (temp.length() >= word.length() && counter >= word.length())
  586.                                 {
  587.                                         if (temp[0] == firstChar && temp[1] == word.substr(1, 1))
  588.                                         {
  589.                                                 startLeftRight = true;
  590.                                         }
  591.                                         else
  592.                                         {
  593.                                                 startLeftRight = false;
  594.                                         }
  595.                                         bool foundSP = false;
  596.                                         bool foundEP = false;
  597.                                         for (int i = 0; i < row_diagonal.length(); i++)
  598.                                         {
  599.                                                 if (row_diagonal[i].first == firstChar)
  600.                                                 {
  601.                                                         sp = row_diagonal[i].second;
  602.                                                         foundSP = true;
  603.                                                         break;
  604.                                                 }
  605.                                         }
  606.                                         for (int i = 0; i < row_diagonal.length(); i++)
  607.                                         {
  608.                                                 if (row_diagonal[i].first == lastChar)
  609.                                                 {
  610.                                                         ep = row_diagonal[i].second;
  611.                                                         if (atTheEnd)
  612.                                                         {
  613.                                                                 ep = row_diagonal[i+1].second;
  614.                                                         }
  615.                                                         foundEP = true;
  616.                                                         break;
  617.                                                 }
  618.                                         }
  619.                                         if (foundSP && foundEP && startLeftRight)
  620.                                         {
  621.                                                 AcDbObjectPointer<AcDbLine> pLine;
  622.                                                 pLine.create();
  623.                                                 pLine->setStartPoint(sp);
  624.                                                 pLine->setEndPoint(ep);
  625.                                                 pBTR->appendAcDbEntity(pLine);
  626.                                         }
  627.                                         else
  628.                                         {
  629.                                                 row_diagonal.reverse();
  630.                                                 for (int i = 0; i < row_diagonal.length(); i++)
  631.                                                 {
  632.                                                         if (row_diagonal[i].first == firstChar)
  633.                                                         {
  634.                                                                 sp = row_diagonal[i].second;
  635.                                                                 break;
  636.                                                         }
  637.                                                 }
  638.                                                 for (int i = 0; i < row_diagonal.length(); i++)
  639.                                                 {
  640.                                                         if (row_diagonal[i].first == lastChar)
  641.                                                         {
  642.                                                                 ep = row_diagonal[i].second;
  643.                                                                 if (atTheEnd)
  644.                                                                 {
  645.                                                                         ep = row_diagonal[i+1].second;
  646.                                                                 }
  647.                                                                 break;
  648.                                                         }
  649.                                                 }
  650.                                                 AcDbObjectPointer<AcDbLine> pLine;
  651.                                                 pLine.create();
  652.                                                 pLine->setStartPoint(sp);
  653.                                                 pLine->setEndPoint(ep);
  654.                                                 pBTR->appendAcDbEntity(pLine);
  655.                                         }
  656.                                 }
  657.                                 temp.removeAll();
  658.                         }
  659.                 }
  660.         }
  661. }
  662.  
[/code]

and if someone wants to see if works > did my tests on autocad 2011 < command: WordPuzzle

note: the code can be refactored - but did not have any time for that.
le.-
Title: Re: ==={challenge}=== Word Puzzle
Post by: Dashmonkey on May 07, 2012, 04:18:59 PM
(setq l (apply 'mapcar (cons 'list l)))

This blew my mind.  :-o
Title: Re: ==={challenge}=== Word Puzzle
Post by: MP on May 07, 2012, 04:29:00 PM
This blew my mind.  :-o

I believe originally attributable to Mr. Tanzillo back in '91 +/-.
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac 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  :-)
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac 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:
Title: Re: ==={challenge}=== Word Puzzle
Post by: MP 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.
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac 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  :-)

Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac 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  :-)
Title: Re: ==={challenge}=== Word Puzzle
Post by: LE3 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.  
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe 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
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe 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.
Title: Re: ==={challenge}=== Word Puzzle
Post by: LE3 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.
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe 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
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac 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:
Title: Re: ==={challenge}=== Word Puzzle
Post by: LE3 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).
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe 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.


Title: Re: ==={challenge}=== Word Puzzle
Post by: LE3 on May 10, 2012, 09:05:13 AM
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.

thanks!
Title: Re: ==={challenge}=== Word Puzzle
Post by: ElpanovEvgeniy on May 11, 2012, 11:41:27 AM
my variant:
Code - Auto/Visual Lisp: [Select]
  1. (defun wordpuzzle (l / A B C I S)
  2.   (defun f (b i)
  3.     (cond ((or (not (car b)) (< i 0)) nil)
  4.           ((nth i b) (cons (car (nth i b)) (f (mapcar (function cdr) b) (1- i))))
  5.           ((f (mapcar (function cdr) b) (1- i)))
  6.     )
  7.   )
  8.   (defun f1 (b i)
  9.     (if (<= 0 i)
  10.       (cons (f b i) (f1 b (1- i)))
  11.     )
  12.   )
  13.   (if (setq s (ssget "_x" '((0 . "text"))))
  14.     (progn (setq a (vl-sort (mapcar (function (lambda (a) (setq a (entget (cadr a))) (cons (cdr (assoc 1 a)) (cdr (assoc 11 a)))))
  15.                                     (ssnamex s)
  16.                             )
  17.                             (function (lambda (a b)
  18.                                         (if (equal (cadr a) (cadr b) 1e-3)
  19.                                           (>= (caddr a) (caddr b))
  20.                                           (>= (cadr a) (cadr b))
  21.                                         )
  22.                                       )
  23.                             )
  24.                    )
  25.                  b nil
  26.            )
  27.            (while a
  28.              (setq b (cons (vl-remove-if-not (function (lambda (b) (equal (cadar a) (cadr b) 1e-3))) a) b)
  29.                    a (vl-remove-if (function (lambda (b) (equal (cadar a) (cadr b) 1e-3))) a)
  30.              )
  31.            )
  32.            (setq i (+ (length b) (length (car b)) -2)
  33.                  b (list b
  34.                          (mapcar (function reverse) b)
  35.                          (apply (function mapcar) (cons (function list) b))
  36.                          (apply (function mapcar) (cons (function list) (reverse b)))
  37.                    )
  38.                  b (apply (function append) (append b (mapcar (function (lambda (a) (f1 a i))) b)))
  39.                  c (mapcar (function
  40.                              (lambda (a) (cons (apply (function strcat) (mapcar (function car) a)) (mapcar (function cdr) a)))
  41.                            )
  42.                            b
  43.                    )
  44.            )
  45.            (foreach a l
  46.              (if (setq b (vl-member-if (function (lambda (b) (setq i (vl-string-search a (car b))))) c))
  47.                (entmakex (list '(0 . "line") (cons 10 (nth (1+ i) (car b))) (cons 11 (nth (+ i (strlen a)) (car b)))))
  48.              )
  49.            )
  50.     )
  51.   )
  52.   (princ)
  53. )

test:
Code - Auto/Visual Lisp: [Select]
  1. (wordpuzzle '("BOX" "MAD" "AMASS" "LSP" "EGG" "AXE" "ATTIC" "GLAD" "test"))
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 12, 2012, 02:03:00 AM
Great code  ElpanovEvgeniy. Another head spinner  :-D

Question for you guys,
Whats the diffrerence?

Code - Auto/Visual Lisp: [Select]
  1.               (lambda (j)
  2.                     (+ (* j j) j)))
  3.         '(1 2 3 4))

Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda (j)
  2.                (+ (* j j) j))
  3.         '(1 2 3 4))


Title: Re: ==={challenge}=== Word Puzzle
Post by: ElpanovEvgeniy on May 12, 2012, 02:16:42 AM
http://www.theswamp.org/~john/avlisp/#function (http://www.theswamp.org/~john/avlisp/#function)
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 12, 2012, 02:39:12 AM
http://www.theswamp.org/~john/avlisp/#function (http://www.theswamp.org/~john/avlisp/#function)

Thanks Evgeniy, does that mean with the use function function it acts as if the code is compiled? Does it mean better perfiormance? or just a "cleaner" code and easier to debug?
Title: Re: ==={challenge}=== Word Puzzle
Post by: ElpanovEvgeniy on May 12, 2012, 02:44:08 AM
http://www.theswamp.org/~john/avlisp/#function (http://www.theswamp.org/~john/avlisp/#function)

Thanks Evgeniy, does that mean with the use function function it acts as if the code is compiled? Does it mean better perfiormance? or just a "cleaner" code and easier to debug?

function increases the speed of the program, after compiling!  :-)
For me, this is the most important thing...

I have long been accustomed to, be sure to write a FUNCTION.
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 12, 2012, 02:58:53 AM
function increases the speed of the program, after compiling!  :-)

Got it.

For me, this is the most important thing...

I believe  it does matter a lot doesnt it?  :-)

Thank you for your insights Evgeniy.
kudos to you.
Title: Re: ==={challenge}=== Word Puzzle
Post by: Lee Mac on May 12, 2012, 08:27:42 AM
function increases the speed of the program, after compiling!  :-)

Though, I think the use of function only has an effect on compiled performance when used with the anonymous lambda function, not with regular AutoLISP functions which are already compiled and optimised.

http://www.theswamp.org/index.php?topic=40271 (http://www.theswamp.org/index.php?topic=40271)

Will be spending some time looking over your code Evgeniy :-)
Title: Re: ==={challenge}=== Word Puzzle
Post by: pBe on May 13, 2012, 12:06:27 AM

http://www.theswamp.org/index.php?topic=40271 (http://www.theswamp.org/index.php?topic=40271)

Thank you for that link Lee  :-)