# counting triangles # # This file is both input file and source file # awk -f file < file # # LINE 0 5 8 10 # LINE 0 3 7 9 # LINE 0 2 4 6 # LINE 0 1 # LINE 1 2 3 5 # LINE 1 4 7 8 # LINE 1 6 9 10 # # 11 pts, labelled 0 to 10. # Two pts are connected if they are both in one list. # We have a triangle if we find 3 pts connected # It is non-degenerate if they are not all in one list # # Apologies for formatting and brevity: written as a challenge # (not as a product) # \$2=="LINE"{ l++; for(a=3;a<=NF;a++){ \$a+=0; p[\$a]++; # print "added pt:", \$a; for(b=3;b<=NF;b++){ \$b+=0; if(\$b!=\$a){ # record which line connects these two pts k[\$a,\$b]=l; # print "connected: ",\$a, \$b; } } } } END{ # print "read", l, "lines"; # for (a in p) # print "have pt:" a; for (a in p) for (b in p) if (a!=b) # distinct points only if (k[a,b]){ # connected pairs only # print "found a link:",a,b; for (c in p) if (c!=a) if (c!=b) if (k[a,c]) if (k[a,c]!=k[a,b]) # not co-linear if (k[b,c]){ # print "triangle:",a,b,c; t++; } } # lazy: we counted each triangle 6 times print t/6; }