Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Motivation for this page was to add more list and regex example code. I had a hard time creating the regex which was acceptable to the groovy interpretor or which worked as I expected. I found examples of using static regex but very little variable based regex.

Secondly, I had seen this problem posed in multiple places and a solution in perl for the "lines2" which did not seem to work for "lines1".(my mistake, did not use enough points in the loops)

Problem: Count all triangles in the diagram.
Solution: select 3 points shared by 3 different lines

Code Block

 
 /*
lines1
               A
               /\
            O .--. D
             / \/ \
         N  .--P---. E
           / \/ \ / \
        M .--U---Q---. F
         / \ / \/ \ / \
      L .-- T---S---R--. G
       / \ / \ / \ /\ / \
      .---.---.---.--.---.  
    C     K   J   I  H    B 




lines2
                 (A) 
                 /\ 
               / /\ \           
            /   /  \   \        
          /    /    \    \      
        /     /      \     \    
      /   (C)/        \(D)   \  
 (B)/_______/__________\_______\(E)
    |  \__ /            \ __/  |
    |  (F)X__          __X(G)  |
    |    /   \___  ___/   \    |
    |   /     ___><___     \   |
    |  /   __/   (H)  \__   \  |
    | / __/              \__ \ |
    |/_/____________________\_\|
 (I)                            (J)

 */

 lines2 = ['ae',
           'adgj',
           'acfi',
           'ab',
           'bcde',
           'bfhj',
           'bi',
           'ij',
           'ihge',
           'je'
           ]
       	

 lines1 = ['adefgb',
         'aonmlc',
         'bhijkc',
         'do',
         'dputk',
         'epn',
         'eqsj',
         'fri',
         'fqum',
         'gh',
         'grstl',
         'hrqpo',
         'isun',
         'jtm',
         'kl'
         ]
         
//  Echo Lines

 
def computeTriangles = 
{ lines -> 
    
    println()

    // Initialize
    count = 0
    size = lines.size
    println "find triangles, $count, $size"

      
    for (pt1 in 'a'..'u')
    {
        for (pt2 in 'b'..'u')
        {
            for (pt3 in 'c'..'u')
            {
                line = lines.grep(~/(.*$pt1.*$pt2.*|.*$pt2.*$pt3.*|.*$pt1.*$pt3.*)/)
                if (line.size == 3)
                {
                    println (++count + ": $pt1,$pt2,$pt3 : " + line )
                }
            }
        }
    }
}

computeTriangles (lines1)
computeTriangles (lines2)

    
//  Termination

println ("Terminated Normally")

Output

Code Block
find triangles, 0, 15
1: a,b,c : ["adefgb", "aonmlc", "bhijkc"]
2: a,d,o : ["adefgb", "aonmlc", "do"]
3: a,e,n : ["adefgb", "aonmlc", "epn"]
4: a,f,m : ["adefgb", "aonmlc", "fqum"]
5: a,g,l : ["adefgb", "aonmlc", "grstl"]
6: d,b,k : ["adefgb", "bhijkc", "dputk"]
7: d,e,p : ["adefgb", "dputk", "epn"]
8: d,f,u : ["adefgb", "dputk", "fqum"]
9: d,g,t : ["adefgb", "dputk", "grstl"]
10: d,p,o : ["do", "dputk", "hrqpo"]
11: e,b,j : ["adefgb", "bhijkc", "eqsj"]
12: e,f,q : ["adefgb", "eqsj", "fqum"]
13: e,g,s : ["adefgb", "eqsj", "grstl"]
14: e,q,p : ["epn", "eqsj", "hrqpo"]
15: e,s,n : ["epn", "eqsj", "isun"]
16: f,b,i : ["adefgb", "bhijkc", "fri"]
17: f,g,r : ["adefgb", "fri", "grstl"]
18: f,i,u : ["fri", "fqum", "isun"]
19: f,r,q : ["fri", "fqum", "hrqpo"]
20: g,b,h : ["adefgb", "bhijkc", "gh"]
21: g,h,r : ["gh", "grstl", "hrqpo"]
22: h,o,c : ["aonmlc", "bhijkc", "hrqpo"]
23: h,p,k : ["bhijkc", "dputk", "hrqpo"]
24: h,q,j : ["bhijkc", "eqsj", "hrqpo"]
25: h,r,i : ["bhijkc", "fri", "hrqpo"]
26: i,n,c : ["aonmlc", "bhijkc", "isun"]
27: i,s,j : ["bhijkc", "eqsj", "isun"]
28: i,u,k : ["bhijkc", "dputk", "isun"]
29: j,m,c : ["aonmlc", "bhijkc", "jtm"]
30: j,t,k : ["bhijkc", "dputk", "jtm"]
31: k,l,c : ["aonmlc", "bhijkc", "kl"]
32: p,o,n : ["aonmlc", "epn", "hrqpo"]
33: p,u,n : ["dputk", "epn", "isun"]
34: q,j,m : ["eqsj", "fqum", "jtm"]
35: q,o,m : ["aonmlc", "fqum", "hrqpo"]
36: q,p,u : ["dputk", "fqum", "hrqpo"]
37: q,s,u : ["eqsj", "fqum", "isun"]
38: r,i,s : ["fri", "grstl", "isun"]
39: r,o,l : ["aonmlc", "grstl", "hrqpo"]
40: r,p,t : ["dputk", "grstl", "hrqpo"]
41: r,q,s : ["eqsj", "grstl", "hrqpo"]
42: s,j,t : ["eqsj", "grstl", "jtm"]
43: s,n,l : ["aonmlc", "grstl", "isun"]
44: s,u,t : ["dputk", "grstl", "isun"]
45: t,k,l : ["dputk", "grstl", "kl"]
46: t,m,l : ["aonmlc", "grstl", "jtm"]
47: u,n,m : ["aonmlc", "fqum", "isun"]
48: u,t,m : ["dputk", "fqum", "jtm"]

find triangles, 0, 10
1: a,b,c : ["acfi", "ab", "bcde"]
2: a,b,d : ["adgj", "ab", "bcde"]
3: a,b,e : ["ae", "ab", "bcde"]
4: a,b,f : ["acfi", "ab", "bfhj"]
5: a,b,i : ["acfi", "ab", "bi"]
6: a,b,j : ["adgj", "ab", "bfhj"]
7: a,c,d : ["adgj", "acfi", "bcde"]
8: a,c,e : ["ae", "acfi", "bcde"]
9: a,d,e : ["ae", "adgj", "bcde"]
10: a,f,j : ["adgj", "acfi", "bfhj"]
11: a,g,e : ["ae", "adgj", "ihge"]
12: a,i,e : ["ae", "acfi", "ihge"]
13: a,i,g : ["adgj", "acfi", "ihge"]
14: a,i,j : ["adgj", "acfi", "ij"]
15: a,j,e : ["ae", "adgj", "je"]
16: b,c,f : ["acfi", "bcde", "bfhj"]
17: b,c,i : ["acfi", "bcde", "bi"]
18: b,d,j : ["adgj", "bcde", "bfhj"]
19: b,f,i : ["acfi", "bfhj", "bi"]
20: b,h,e : ["bcde", "bfhj", "ihge"]
21: b,i,e : ["bcde", "bi", "ihge"]
22: b,i,h : ["bfhj", "bi", "ihge"]
23: b,i,j : ["bfhj", "bi", "ij"]
24: b,j,e : ["bcde", "bfhj", "je"]
25: c,i,e : ["acfi", "bcde", "ihge"]
26: d,g,e : ["adgj", "bcde", "ihge"]
27: d,j,e : ["adgj", "bcde", "je"]
28: f,i,h : ["acfi", "bfhj", "ihge"]
29: f,i,j : ["acfi", "bfhj", "ij"]
30: g,j,e : ["adgj", "ihge", "je"]
31: h,g,j : ["adgj", "bfhj", "ihge"]
32: h,j,e : ["bfhj", "ihge", "je"]
33: i,g,j : ["adgj", "ij", "ihge"]
34: i,h,j : ["bfhj", "ij", "ihge"]
35: i,j,e : ["ij", "ihge", "je"]
Terminated Normally