excelpm.k 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // TODO: clean this up
  2. \d u
  3. \l utils.k
  4. / :`i$"A"; // 65
  5. / :`i$"Z"; // 90
  6. / :`i$"0"; // 48
  7. / :`i$"9"; // 57
  8. / :`i$":"; // 58
  9. / :`i$"$"; // 36
  10. \d pat
  11. testAtZ: { i:`i$x; $[(i>64) & (i<91);1;0] };
  12. test0t9: { i:`i$x; $[(i>47) & (i<58);1;0] };
  13. testcol: { i:`i$x; $[i=58;1;0] };
  14. testend: { i:`i$x; $[i=36;1;0] };
  15. s0: ((testAtZ; 1);(test0t9; 2));
  16. s1: ((test0t9; 2);(testcol; 3));
  17. s2: ((testend; 7); (testcol; 3));
  18. s3: ((test0t9; 5);(testAtZ; 5));
  19. s4: ((testAtZ; 5); (test0t9; 5));
  20. s5: (,(test0t9; 5));
  21. // 7 is done
  22. states:: (s0;s1;s2;s3;s4;s5);
  23. i: 1;
  24. runS: {[c;s]
  25. :{
  26. :$[x[0][y];x[1];0]
  27. }[;c]'states[s]
  28. };
  29. // blk 1 stuff ===============================
  30. blk1: {[a;c]
  31. / u.ppr "blk1";
  32. activeidx: 0;
  33. active: (0;a[3]+1);
  34. s: 0;
  35. r: runS[c;s];
  36. r: *($[(+/r)>0;r^0;0]);
  37. / u.ppr (c;r;a[2]);
  38. a0: $[s>4;s;r];
  39. :((,active);activeidx;r;a[3])
  40. };
  41. // blk 2 stuff ===============================
  42. e:{[a;c;r]
  43. / u.ppr ("edge";r;a;c);
  44. // add indices
  45. indices: a[0],(,(a[3];a[3]));
  46. a[2]: r;
  47. a[1] +: 1;
  48. :(indices; a[1]; a[2]; a[3])
  49. };
  50. ne:{[a;c;r]
  51. / u.ppr "no edge";
  52. ai: a[1];
  53. a[0;ai;0]: $[a[2]>0;a[0;ai;0];a[3]];
  54. a[0;ai;1]: a[3]+1;
  55. a[2]: r;
  56. :a
  57. };
  58. blk2: {[a;c]
  59. / u.ppr ("blk2"; a);
  60. s: a[2];
  61. r: runS[c;s];
  62. r: *($[(+/r)>0;r^0;0]);
  63. / u.ppr (c;r;a[2]);
  64. edge: (s=((#states) - 1))&(r=0);
  65. a[3] +: 1;
  66. :$[edge;e[a;c;r];ne[a;c;r]]
  67. };
  68. run: {[str]
  69. / u.ppr ("finding pattern in: ", str);
  70. // ((0),-1,0) -> 0: list of start/end indices, 1: active index, 2: state the machine stopped on, 3: idx
  71. o:((0),-1,0,0) {[a;c]
  72. // if activeidx = -1 => blk1
  73. // else blk2
  74. / u.ppr " ";
  75. :$[a[1]=-1;blk1[a;c];blk2[a;c]]
  76. }/str;
  77. :o
  78. };
  79. // 012345678901
  80. / u.ppr (run[" C1:C1-A1:A1"])
  81. \d .