Advent of Code 2019-05 with R & JavaScript

[This article was first published on Colin Fay, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Solving Advent of Code 2019-05 with R and JavaScript.

[Disclaimer] Obviously, this post contains a big spoiler about Advent
of Code, as it gives solutions for solving day 5.

[Disclaimer bis] I’m no JavaScript expert so this might not be the
perfect solution. TBH, that’s also the case for the R solution.

About the JavaScript code

The JavaScript code has been written in the same RMarkdown as the R
code. It runs thanks to the {bubble} package:
https://github.com/ColinFay/bubble

Instructions

Find the instructions at: https://adventofcode.com/2019/day/5

R solution

<span class="n">vec</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">scan</span><span class="p">(</span><span class="s2">"input5.txt"</span><span class="p">,</span><span class="w"> </span><span class="n">numeric</span><span class="p">(),</span><span class="w"> </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">","</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">magrittr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">zeallot</span><span class="p">)</span><span class="w">
</span><span class="n">parse_opcode</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">pos</span><span class="p">){</span><span class="w">
  </span><span class="n">code</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span><span class="w">
  </span><span class="n">op_code</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_pad</span><span class="p">(</span><span class="w">
    </span><span class="nf">as.character</span><span class="p">(</span><span class="n">code</span><span class="p">),</span><span class="w"> 
    </span><span class="m">5</span><span class="p">,</span><span class="w"> 
    </span><span class="n">side</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"left"</span><span class="p">,</span><span class="w"> </span><span class="s2">"0"</span><span class="w">
  </span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">strsplit</span><span class="p">(</span><span class="n">split</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w">
  </span><span class="n">op_code</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">op_code</span><span class="p">[[</span><span class="m">1</span><span class="p">]]</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
    </span><span class="n">setNames</span><span class="p">(</span><span class="nb">letters</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">5</span><span class="p">])</span><span class="w">
  </span><span class="nf">list</span><span class="p">(</span><span class="w">
    </span><span class="n">op_code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="w">
      </span><span class="n">op_code</span><span class="p">[</span><span class="m">4</span><span class="o">:</span><span class="m">5</span><span class="p">],</span><span class="w"> 
      </span><span class="n">collapse</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="w">
    </span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(),</span><span class="w"> 
    </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">op_code</span><span class="p">[</span><span class="s2">"c"</span><span class="p">],</span><span class="w">
    </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">op_code</span><span class="p">[</span><span class="s2">"b"</span><span class="p">],</span><span class="w"> 
    </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">op_code</span><span class="p">[</span><span class="s2">"a"</span><span class="p">]</span><span class="w">
  </span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">one_two_three</span><span class="p">(</span><span class="w">
      </span><span class="n">vec</span><span class="p">,</span><span class="w"> 
      </span><span class="n">pos</span><span class="w">
    </span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">one_two_three</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">pos</span><span class="p">){</span><span class="w">
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">res</span><span class="o">$</span><span class="n">c</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"0"</span><span class="p">){</span><span class="w">
    </span><span class="n">one</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">one</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w"> 
  </span><span class="p">}</span><span class="w"> 
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">res</span><span class="o">$</span><span class="n">b</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"0"</span><span class="p">){</span><span class="w">
    </span><span class="n">two</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">2</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">two</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">2</span><span class="p">]</span><span class="w"> 
  </span><span class="p">}</span><span class="w"> 
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">res</span><span class="o">$</span><span class="n">a</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"0"</span><span class="p">){</span><span class="w">
    </span><span class="n">three</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">3</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">three</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">3</span><span class="p">]</span><span class="w">
  </span><span class="p">}</span><span class="w"> 
  </span><span class="nf">list</span><span class="p">(</span><span class="w">
    </span><span class="n">res</span><span class="o">$</span><span class="n">op_code</span><span class="p">,</span><span class="w">
    </span><span class="n">one</span><span class="p">,</span><span class="w"> 
    </span><span class="n">two</span><span class="p">,</span><span class="w"> 
    </span><span class="n">three</span><span class="w">
  </span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">prog</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">ipt</span><span class="p">){</span><span class="w">
  </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">1</span><span class="w">
  </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">pos</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">vec</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
    
    </span><span class="nf">c</span><span class="p">(</span><span class="n">op_code</span><span class="p">,</span><span class="w"> </span><span class="n">one</span><span class="p">,</span><span class="w"> </span><span class="n">two</span><span class="p">,</span><span class="w"> </span><span class="n">three</span><span class="p">)</span><span class="w"> </span><span class="o">%<-%</span><span class="w">  </span><span class="n">parse_opcode</span><span class="p">(</span><span class="w"> </span><span class="n">vec</span><span class="p">,</span><span class="w">  </span><span class="n">pos</span><span class="w"> </span><span class="p">)</span><span class="w">
    
    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">99</span><span class="p">)</span><span class="w"> </span><span class="k">break</span><span class="w">
    
    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="m">1</span><span class="o">:</span><span class="m">2</span><span class="p">){</span><span class="w">
      
      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="n">fun</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">`+`</span><span class="w">
      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="n">fun</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">`*`</span><span class="w">
      </span><span class="n">vec</span><span class="p">[</span><span class="n">three</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">fun</span><span class="p">(</span><span class="n">one</span><span class="p">,</span><span class="w"> </span><span class="n">two</span><span class="p">)</span><span class="w">
      </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">4</span><span class="w">
    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">3</span><span class="p">){</span><span class="w">
      </span><span class="n">vec</span><span class="p">[</span><span class="w"> </span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ipt</span><span class="w">
      </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">2</span><span class="w">
    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">4</span><span class="p">){</span><span class="w">
      </span><span class="n">print</span><span class="p">(</span><span class="n">vec</span><span class="p">[</span><span class="n">vec</span><span class="p">[</span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">])</span><span class="w">
      </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">2</span><span class="w">
    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">5</span><span class="p">){</span><span class="w">
      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">one</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="m">0</span><span class="p">){</span><span class="w">
        </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">two</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="w">
      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
        </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">3</span><span class="w">
      </span><span class="p">}</span><span class="w">
    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">6</span><span class="p">){</span><span class="w">
      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">one</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">0</span><span class="p">){</span><span class="w">
        </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">two</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="w">
      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
        </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">3</span><span class="w">
      </span><span class="p">}</span><span class="w">
    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">7</span><span class="p">){</span><span class="w">
      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">one</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">two</span><span class="p">){</span><span class="w">
        </span><span class="n">vec</span><span class="p">[</span><span class="n">three</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">1</span><span class="w"> 
      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
        </span><span class="n">vec</span><span class="p">[</span><span class="n">three</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0</span><span class="w"> 
      </span><span class="p">}</span><span class="w">
      </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">4</span><span class="w">
    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">op_code</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">8</span><span class="p">){</span><span class="w">
      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">one</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">two</span><span class="p">){</span><span class="w">
        </span><span class="n">vec</span><span class="p">[</span><span class="n">three</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">1</span><span class="w"> 
      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
        </span><span class="n">vec</span><span class="p">[</span><span class="n">three</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0</span><span class="w"> 
      </span><span class="p">}</span><span class="w">
      </span><span class="n">pos</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">4</span><span class="w">
    </span><span class="p">}</span><span class="w">
  </span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">prog</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w">
</span>
## [1] 3
## [1] 0
## [1] 0
## [1] 0
## [1] 0
## [1] 0
## [1] 0
## [1] 0
## [1] 0
## [1] 6731945

Part two

<span class="n">prog</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w">
</span>
## [1] 9571668

JS solution

Part one & Two

<span class="kd">var</span> <span class="nx">vec</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span><span class="p">(</span><span class="dl">"</span><span class="s2">input5.txt</span><span class="dl">"</span><span class="p">,</span> <span class="dl">'</span><span class="s1">utf8</span><span class="dl">'</span><span class="p">).</span><span class="nx">split</span><span class="p">(</span><span class="dl">"</span><span class="s2">,</span><span class="dl">"</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span> <span class="o">=></span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">x</span><span class="p">)).</span><span class="nx">filter</span><span class="p">(</span><span class="nx">x</span> <span class="o">=></span> <span class="nx">x</span><span class="p">.</span><span class="nx">length</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
<span class="kd">function</span> <span class="nx">parse_opcode</span><span class="p">(</span><span class="nx">vec</span><span class="p">,</span> <span class="nx">pos</span><span class="p">){</span>
  <span class="kd">var</span> <span class="nx">code</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span><span class="p">];</span>
  <span class="nx">code</span> <span class="o">=</span> <span class="nx">code</span><span class="p">.</span><span class="nx">toString</span><span class="p">();</span>
  <span class="k">while</span><span class="p">(</span><span class="nx">code</span><span class="p">.</span><span class="nx">length</span> <span class="o"><</span> <span class="mi">5</span><span class="p">){</span>
    <span class="nx">code</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">0</span><span class="dl">'</span> <span class="o">+</span> <span class="nx">code</span>
  <span class="p">}</span>
  <span class="nx">code</span> <span class="o">=</span> <span class="nx">code</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="dl">""</span><span class="p">);</span>
  <span class="kd">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="p">[];</span>
  <span class="nx">l</span><span class="p">.</span><span class="nx">opcode</span> <span class="o">=</span> <span class="nx">code</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="nx">code</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
  <span class="nx">l</span><span class="p">.</span><span class="nx">c</span> <span class="o">=</span> <span class="nx">code</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
  <span class="nx">l</span><span class="p">.</span><span class="nx">b</span> <span class="o">=</span> <span class="nx">code</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
  <span class="nx">l</span><span class="p">.</span><span class="nx">a</span> <span class="o">=</span> <span class="nx">code</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  <span class="kd">var</span> <span class="nx">res</span> <span class="o">=</span> <span class="nx">one_two_three</span><span class="p">(</span><span class="nx">l</span><span class="p">,</span> <span class="nx">vec</span><span class="p">,</span> <span class="nx">pos</span><span class="p">);</span>
  <span class="k">return</span> <span class="nx">res</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">one_two_three</span><span class="p">(</span><span class="nx">l</span><span class="p">,</span> <span class="nx">vec</span><span class="p">,</span> <span class="nx">pos</span><span class="p">){</span>
  <span class="kd">var</span> <span class="nx">out</span> <span class="o">=</span> <span class="p">{};</span>
  <span class="nx">out</span><span class="p">.</span><span class="nx">opcode</span> <span class="o">=</span> <span class="nx">l</span><span class="p">.</span><span class="nx">opcode</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">l</span><span class="p">.</span><span class="nx">c</span> <span class="o">==</span> <span class="dl">"</span><span class="s2">0</span><span class="dl">"</span><span class="p">){</span>
    <span class="nx">out</span><span class="p">.</span><span class="nx">one</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]]</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="nx">out</span><span class="p">.</span><span class="nx">one</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> 
  <span class="p">}</span> 
  <span class="k">if</span> <span class="p">(</span><span class="nx">l</span><span class="p">.</span><span class="nx">b</span> <span class="o">===</span> <span class="dl">"</span><span class="s2">0</span><span class="dl">"</span><span class="p">){</span>
    <span class="nx">out</span><span class="p">.</span><span class="nx">two</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]]</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="nx">out</span><span class="p">.</span><span class="nx">two</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">2</span><span class="p">]</span> 
  <span class="p">}</span> 
  <span class="k">if</span> <span class="p">(</span><span class="nx">l</span><span class="p">.</span><span class="nx">a</span> <span class="o">===</span> <span class="dl">"</span><span class="s2">0</span><span class="dl">"</span><span class="p">){</span>
    <span class="nx">out</span><span class="p">.</span><span class="nx">three</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">3</span><span class="p">]</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="nx">three</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">3</span><span class="p">]</span>
  <span class="p">}</span> 
  <span class="k">return</span> <span class="nx">out</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">prog</span><span class="p">(</span><span class="nx">vec</span><span class="p">,</span> <span class="nx">ipt</span><span class="p">){</span>
  <span class="kd">var</span> <span class="nx">vec</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">slice</span><span class="p">()</span>
  <span class="kd">var</span> <span class="nx">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="nx">res</span> <span class="o">=</span> <span class="p">[];</span>
  <span class="k">while</span> <span class="p">(</span><span class="nx">pos</span> <span class="o"><</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">out</span> <span class="o">=</span> <span class="nx">parse_opcode</span><span class="p">(</span> <span class="nx">vec</span><span class="p">,</span>  <span class="nx">pos</span> <span class="p">);</span>
    <span class="kd">var</span> <span class="nx">op_code</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">out</span><span class="p">.</span><span class="nx">opcode</span><span class="p">);</span>
    <span class="kd">var</span> <span class="nx">one</span> <span class="o">=</span>  <span class="nb">parseInt</span><span class="p">(</span><span class="nx">out</span><span class="p">.</span><span class="nx">one</span><span class="p">);</span>
    <span class="kd">var</span> <span class="nx">two</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">out</span><span class="p">.</span><span class="nx">two</span><span class="p">);</span>
    <span class="kd">var</span> <span class="nx">three</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">out</span><span class="p">.</span><span class="nx">three</span><span class="p">);</span>
    
    <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">==</span> <span class="mi">99</span><span class="p">)</span> <span class="k">break</span>
    
    <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">vec</span><span class="p">[</span><span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="nx">one</span> <span class="o">+</span> <span class="nx">two</span><span class="p">;</span>
      <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">4</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span>  <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">vec</span><span class="p">[</span><span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="nx">one</span> <span class="o">*</span> <span class="nx">two</span><span class="p">;</span>
      <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">4</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">==</span> <span class="mi">3</span><span class="p">){</span>
      <span class="nx">vec</span><span class="p">[</span> <span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="p">]</span> <span class="o">=</span> <span class="nx">ipt</span>
      <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">2</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">==</span> <span class="mi">4</span><span class="p">){</span>
      <span class="nx">res</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">vec</span><span class="p">[</span><span class="nx">vec</span><span class="p">[</span><span class="nx">pos</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]])</span>
      <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">2</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">===</span> <span class="mi">5</span><span class="p">){</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">one</span> <span class="o">!==</span> <span class="mi">0</span><span class="p">){</span>
        <span class="nx">pos</span> <span class="o">=</span> <span class="nx">two</span> 
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">3</span>
      <span class="p">}</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">===</span> <span class="mi">6</span><span class="p">){</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">one</span> <span class="o">===</span> <span class="mi">0</span><span class="p">){</span>
        <span class="nx">pos</span> <span class="o">=</span> <span class="nx">two</span> 
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">3</span>
      <span class="p">}</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">===</span> <span class="mi">7</span><span class="p">){</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">one</span> <span class="o"><</span> <span class="nx">two</span><span class="p">){</span>
        <span class="nx">vec</span><span class="p">[</span><span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> 
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nx">vec</span><span class="p">[</span><span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span> 
      <span class="p">}</span>
      <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">4</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">op_code</span> <span class="o">===</span> <span class="mi">8</span><span class="p">){</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">one</span> <span class="o">==</span> <span class="nx">two</span><span class="p">){</span>
        <span class="nx">vec</span><span class="p">[</span><span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> 
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nx">vec</span><span class="p">[</span><span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span> 
      <span class="p">}</span>
      <span class="nx">pos</span> <span class="o">=</span> <span class="nx">pos</span> <span class="o">+</span> <span class="mi">4</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="k">return</span> <span class="nx">res</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
<span class="p">}</span>
<span class="nx">prog</span><span class="p">(</span><span class="nx">vec</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
## 6731945
<span class="nx">prog</span><span class="p">(</span><span class="nx">vec</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
## 9571668

To leave a comment for the author, please follow the link and comment on their blog: Colin Fay.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)