fork(4) download
  1. using System; using System.Collections.Generic; using System.Linq;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. //only labelled vertices (should be 21)
  8. int count = 0;
  9. foreach (string triangle in FindTriangles("EAF", "GH", "BC", "EB", "EGD", "AGB", "AD", "AHC", "FHD", "FC", "BD", "CD")) {
  10. Console.WriteLine(triangle);
  11. count++;
  12. }
  13. Console.WriteLine(count);
  14. Console.WriteLine();
  15. //all vertices (should be 40)
  16. count = 0;
  17. foreach (string triangle in FindTriangles("EAF", "GIH", "BKJLC", "EB", "EGKD", "AGB", "AIJD", "AHC", "FHLD", "FC", "BD", "CD")) {
  18. Console.WriteLine(triangle);
  19. count++;
  20. }
  21. Console.WriteLine(count);
  22. }
  23.  
  24. public static IEnumerable<string> FindTriangles(params string[] lines) { //http://p...content-available-to-author-only...e.com/questions/22889
  25. var edges = (
  26. from line in lines
  27. let length = line.Length
  28. from a in Enumerable.Range(0, length-1)
  29. from b in Enumerable.Range(a+1, length-a-1)
  30. select new string(new [] { line[a], line[b] })
  31. ).ToList();
  32. var vertices = new HashSet<char>();
  33. int len = edges.Count;
  34. for (int a = 0; a < len; a++) {
  35. for (int b = a + 1; b < len; b++) {
  36. for (int c = b + 1; c < len; c++) {
  37. string edge = edges[a];
  38. vertices.Add(edge[0]);
  39. vertices.Add(edge[1]);
  40. edge = edges[b];
  41. vertices.Add(edge[0]);
  42. vertices.Add(edge[1]);
  43. edge = edges[c];
  44. vertices.Add(edge[0]);
  45. vertices.Add(edge[1]);
  46. if (vertices.Count == 3) {
  47. string result = string.Join("", vertices);
  48. if (!lines.Any(l => l.Contains(result[0]) && l.Contains(result[1]) && l.Contains(result[2])))
  49. yield return result;
  50. }
  51. vertices.Clear();
  52. }
  53. }
  54. }
  55. }
  56.  
  57. }
Success #stdin #stdout 0.08s 24224KB
stdin
Standard input is empty
stdout
EAB
EAG
EAD
EFD
AFD
AFH
AFC
GHD
GHA
BCA
BCD
EBG
EBD
GDA
GDB
ABD
ADH
ADC
HCF
HCD
FDC
21

EAB
EAG
EAD
EFD
AFD
AFH
AFC
GID
GIA
GHD
GHA
IHA
IHD
BKE
BKG
BKD
BJA
BJD
BLD
BCA
BCD
KJD
KLD
KCD
JLD
JCA
JCD
LCH
LCF
LCD
EBG
EBD
GDA
GDB
ABD
ADH
ADC
HCF
HCD
FDC
40