require('./helpers').getFile(9, input => {
  const output = [];

  for(let i = 0; i < input.length; i++) {
    const char = input[i];
    if (char == '(') {
      // Read marker block and copy code to output
      const endMarkerIndex = input.indexOf(')', i) + 1;
      const marker = input.substring(i, endMarkerIndex);
      const [match, charCount, repeatCount] = marker.match(/\((\d+)x(\d+)\)/);
      const repeatGroup = input.substr(endMarkerIndex, charCount);
      for(j = 0;j < repeatCount;j++) {
        output.push(repeatGroup);
      }

      // Move cursor forward to continue
      i += marker.length + (charCount-1);
    } else {
      output.push(char);
    }
  }

  console.log('The length is:', output.length);
  // 183269
});

@@ -42,233 +42,242 @@
 %7B%0A  
-const output = %5B%5D;%0A%0A  for(let i = 0; i %3C input.length; i++) %7B%0A    const char = input%5Bi%5D;%0A    if (char == '(') %7B%0A      // Read marker block and copy code to output%0A      const endMarkerIndex = input.indexOf(')', i) + 1
+function decompress(str, noNesting) %7B%0A    let output = 0;%0A    const markerIndex = str.indexOf('(');%0A    if (~markerIndex) %7B%0A      output += markerIndex; // Add first x normal chars%0A      const fullTail = str.slice(markerIndex)
 ;%0A      
 cons
@@ -276,21 +276,18 @@
     
-const
+//
  marker 
 = in
@@ -286,45 +286,25 @@
 ker 
-= input.substring(i, endMarkerIndex);
++ tail = fullTail
 %0A   
@@ -346,18 +346,26 @@
 ount
-%5D = marker
+, tail%5D = fullTail
 .mat
@@ -387,65 +387,121 @@
 +)%5C)
-/);%0A      const repeatGroup = input.substr(endMarkerIndex
+(.*)/);%0A%0A      // slice + next = tail%0A      const next = tail.slice(charCount);%0A      const slice = tail.substr(0
 , ch
@@ -506,24 +506,25 @@
 charCount);%0A
+%0A
       for(j 
@@ -521,16 +521,20 @@
     for(
+let 
 j = 0;j 
@@ -572,25 +572,55 @@
 tput
-.push(repeatGroup
+ += noNesting ? slice.length : decompress(slice
 );%0A 
@@ -630,135 +630,119 @@
   %7D%0A
-%0A
       
-// Move cursor forward to continue%0A      i += marker.length + (charCount-1);%0A    %7D else %7B%0A      output.push(char);%0A    %7D
+output += decompress(next, noNesting);%0A    %7D else %7B%0A      output += str.length;%0A    %7D%0A%0A    return output;
 %0A  %7D
@@ -762,16 +762,24 @@
 og('The 
+initial 
 length i
@@ -787,35 +787,146 @@
 :', 
-output.length);%0A  // 183269
+decompress(input, true));%0A  // 183269%0A%0A  console.log('The length is:', decompress(input)); // Watch out, takes a while...%0A  // 11317278863
 %0A%7D);

Add solution to Day 9b