Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. const fs = require("fs");
  2. const logStream = fs.createWriteStream('output.txt', {'flags': 'a'});
  3. const tempBuffer = fs.createWriteStream('temp.txt', {'flags': 'a'});
  4. const input = require("./input");
  5.  
  6.  
  7. const regex = /\((\d+?)x(\d+?)\)/;
  8.  
  9. let restOfInput = input;
  10.  
  11. let output = "";
  12.  
  13. while (restOfInput.match(regex)) {
  14. const [match, letters, times] = regex.exec(restOfInput);
  15. const indexOfMatch = restOfInput.indexOf(match);
  16. const before = restOfInput.substring(0, indexOfMatch);
  17. appendOutput(before);
  18.  
  19. const after = restOfInput.substring(indexOfMatch + match.length);
  20.  
  21. const expression = after.substring(0, parseInt(letters));
  22. const expandedText = getExpressionText(expression, { letters, times});
  23.  
  24. appendOutput(expandedText);
  25.  
  26. restOfInput = after.substring(parseInt(letters));
  27.  
  28. }
  29.  
  30. appendOutput(restOfInput);
  31. console.log(countExpressions(output, 1, 0));
  32. // console.log(output);
  33.  
  34.  
  35. function countExpressions(expressions, multiplier, count) {
  36. while(expressions.length > 0) {
  37. const firstParen = expressions.indexOf("(");
  38.  
  39. if(firstParen < 0) {
  40. return count + (multiplier * expressions.length);
  41. }
  42.  
  43. if(firstParen > 0) {
  44. const charsBeforeParens = expressions.substring(0, firstParen).length;
  45. count += multiplier * charsBeforeParens;
  46. }
  47.  
  48. const closingParen = findClosingParen(expressions, firstParen);
  49.  
  50. const expression = expressions.substring(firstParen + 1, closingParen);
  51. const [match, subMultiplier] = /:(\d+?)$/.exec(expression);
  52. const subExpression = expression.substring(0, expression.length - match.length);
  53.  
  54. count = countExpressions(subExpression, subMultiplier * multiplier, count);
  55.  
  56. expressions = expressions.substring(closingParen+1);
  57.  
  58. // const quantifier
  59. }
  60.  
  61. return count;
  62. }
  63.  
  64. function findClosingParen(expression, openPos) {
  65. let closePos = openPos;
  66. let counter = 1;
  67. while (counter > 0) {
  68. const c = expression[++closePos];
  69. if (c == '(') {
  70. counter++;
  71. }
  72. else if (c == ')') {
  73. counter--;
  74. }
  75. }
  76. return closePos;
  77. }
  78.  
  79. function getExpressionText(expression, parentExpression) {
  80. if(!expression.match(regex)) {
  81. return quantify(expression, parseInt(parentExpression.times));
  82. }
  83.  
  84. let expandedText = "";
  85.  
  86. while(expression.match(regex)) {
  87. const [match, letters, times] = regex.exec(expression);
  88. const indexOfMatch = expression.indexOf(match);
  89. const before = expression.substring(0, indexOfMatch);
  90.  
  91. const after = expression.substring(indexOfMatch + match.length);
  92. const subExpression = after.substring(0, parseInt(letters));
  93. expandedText += before + getExpressionText(subExpression, { letters, times: parseInt(times)});
  94. expression = after.substring(parseInt(letters));
  95. }
  96.  
  97. return quantify(expandedText, parseInt(parentExpression.times));
  98. }
  99.  
  100. function quantify(text, times) {
  101.  
  102. return `(${text}:${times})`;
  103. }
  104.  
  105. function quantifyText(text, times) {
  106.  
  107. let returnedString = "";
  108.  
  109. for (let i = 0; i < times; i++) {
  110. logStream.write(text);
  111. }
  112.  
  113. return returnedString;
  114. }
  115.  
  116. function appendOutput(text) {
  117. // logStream.write(text);
  118. output += text;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement