main.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const fs = require('fs');
  2. const readline = require('readline');
  3. function isMarker(str) {
  4. let tmp = [];
  5. return !str.split("").some(i => {
  6. if (tmp.includes(i))
  7. return true;
  8. tmp.push(i);
  9. return false;
  10. });
  11. }
  12. function findMarker(input, len) {
  13. for (let i =0; i < input.length -len; ++i) {
  14. if (isMarker(input.substring(i, i+len))) {
  15. return i;
  16. }
  17. }
  18. }
  19. function strLine(line) {
  20. let output = "";
  21. let pos14 = -1;
  22. let pos4 = -1;
  23. while (line.length) {
  24. if (pos14 < 0)
  25. pos14 = findMarker(line, 14);
  26. if (pos4 < 0)
  27. pos4 = findMarker(line, 4);
  28. if (pos14 < 4) {
  29. output += line.substring(0, pos14) + '\033[34;1m' + line.substring(pos4, pos4 +14) +'\033[39;00m';
  30. line = line.substring(14);
  31. pos14 = pos4 = -1;
  32. } else if (pos4 === 0) {
  33. output += '\033[95;1m' + line.substring(0, 4) +'\033[39;00m';
  34. line = line.substring(4);
  35. pos4 = -1;
  36. pos14 -= 4;
  37. if (pos14 === 0) pos14 = -1;
  38. } else {
  39. if (pos14 <= pos4) {
  40. output += line.substring(0, pos14);
  41. line = line.substring(pos14);
  42. pos4 -= pos14;
  43. if (pos4 === 0) pos4 = -1;
  44. pos14 = 0;
  45. } else if (pos4) {
  46. output += line.substring(0, pos4);
  47. line = line.substring(pos4);
  48. pos14 -= pos4;
  49. if (pos14 === 0) pos14 = -1;
  50. pos4 = 0;
  51. } else {
  52. output += line;
  53. line = "";
  54. }
  55. }
  56. }
  57. return output;
  58. }
  59. async function main() {
  60. for await (let line of readline.createInterface({ input: process.stdin })) {
  61. console.log(strLine(line));
  62. console.log("Position of distinct 4 chars: " + (findMarker(line, 4)+4));
  63. console.log("Position of distinct 14 chars: " + (findMarker(line, 14)+14));
  64. break;
  65. }
  66. };
  67. (main());