Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script>
    function makeAddInstruction(amount, target) {
      if (amount < 0) {
        target.push("SUB "+(-amount));
      } else if (amount > 0) {
        target.push("ADD "+(amount));
      } else if (force) {
        target.push("NOP");
      }
    }
    
    function buildLookupTable(amounts, bias) {
      var instructions = [], i, dispSoFar=0;
      instructions.push("JRO ACC");
      offsets = [];
      for (i=amounts.length-1; i>=0; i--) {
        offsets[i+1] = amounts[i]-(i+1)-dispSoFar;
        dispSoFar += offsets[i+1];
      }
      offsets[offsets.length-1] += bias
      for (i=1; i<offsets.length; i++) {
        makeAddInstruction(offsets[i], instructions, true);
      }
      
      return instructions;
    }
    
    function buildLookupTables(amounts, chunkSize) {
      var instructions = [];
      var chunks = [];
      var chunkSize = chunkSize || 10;
      for (let i = 0; i < amounts.length; i += chunkSize) { 
        const chunk = amounts.slice(i, i + chunkSize);
        chunks.push(chunk);
      }
      instructions.push("#"+(chunks.length+1)+" NODES REQUIRED\n");
      
      var bias = amounts.length;
      for (let i = 0; i < chunks.length; i += 1) { 
        if(i > 0) {
          instructions.push("MOV -"+chunkSize+" ACC");
          instructions.push("ADD <PREV>");
        } else {
          instructions.push("MOV <INPUT> ACC");
        }
        instructions = instructions.concat(buildLookupTable(chunks[i], bias));
        instructions.push("MOV ACC <NEXT>\n")
        bias -= chunkSize;
      }
      instructions.push("MOV -"+(chunkSize+bias)+" ACC")
      instructions.push("ADD <PREV>")
      instructions.push("MOV ACC <OUTPUT>")
      return instructions;
    }
    
    $(function() {
      $('#build').on('click', function() {
        var amounts = $('#outputs').val().split(',').map(function(x) {return +x;});
        console.log(amounts);
        var firstIndex = +($('#firstIndex').val()) || 10;
        var instructions = buildLookupTables(amounts, firstIndex);
        console.log(instructions);
        $('#tis100code').text(instructions.join('\n'));
      });
    });
  </script>
</head>
<body>
  <p><label>Output values (comma separated) <input type=text id=outputs value="1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990"></label></p>
  <p><label>Chunk Size <input type=text id=firstIndex value=8></label></p>
  <p><input type=button value=Calculate id=build></p>
  <output><pre id=tis100code></pre><output>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers