Advent of Code 2019-07 with R

[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-07 with R.

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

Instructions

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

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">"input7.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="c1"># Getting parse_opcode  </span><span class="w">
</span><span class="n">source</span><span class="p">(</span><span class="s2">"intcode_helpers.R"</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">inputs</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="p">){</span><span class="w"> 
  </span><span class="n">ipt_stat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">1</span><span class="w">
  </span><span class="n">stock</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="p">{</span><span class="w">
      </span><span class="nf">return</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">vec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="p">,</span><span class="w"> 
          </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">res</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="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="m">99</span><span class="p">,</span><span class="w"> 
          </span><span class="n">processed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</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="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="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">stock</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">inputs</span><span class="p">)){</span><span class="w">
        </span><span class="nf">return</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">vec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="p">,</span><span class="w"> 
            </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">res</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="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">op_code</span><span class="p">,</span><span class="w"> 
            </span><span class="n">processed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</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="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">ipt_stat</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">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">inputs</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="w">
        </span><span class="n">ipt_stat</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="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">inputs</span><span class="p">[</span><span class="m">2</span><span class="p">]</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">2</span><span class="w">
      </span><span class="n">stock</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stock</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="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">res</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="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="nf">return</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">vec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="p">,</span><span class="w"> 
      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">res</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="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">op_code</span><span class="p">,</span><span class="w"> 
      </span><span class="n">processed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</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">res</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0</span><span class="w">

</span><span class="n">comb</span><span class="w"> </span><span class="o"><-</span><span class="w">  </span><span class="n">expand.grid</span><span class="p">(</span><span class="m">0</span><span class="o">:</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="o">:</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="o">:</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="o">:</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="o">:</span><span class="m">4</span><span class="p">)</span><span class="w"> 

</span><span class="n">to_keep</span><span class="w"> </span><span class="o"><-</span><span class="w">  </span><span class="n">apply</span><span class="p">(</span><span class="n">comb</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">){</span><span class="w">
  </span><span class="nf">length</span><span class="p">(</span><span class="n">unique</span><span class="p">(</span><span class="n">x</span><span class="p">))</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">x</span><span class="p">)</span><span class="w">
</span><span class="p">})</span><span class="w">

</span><span class="n">comb</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">comb</span><span class="p">[</span><span class="o">!</span><span class="n">to_keep</span><span class="p">,</span><span class="w"> </span><span class="p">]</span><span class="w">
</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nf">seq_len</span><span class="p">(</span><span class="n">nrow</span><span class="p">(</span><span class="n">comb</span><span class="p">))){</span><span class="w">
  </span><span class="n">phase</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">comb</span><span class="p">[</span><span class="n">i</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="nf">as.numeric</span><span class="p">()</span><span class="w">
  </span><span class="n">start</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0</span><span class="w">
  </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nf">seq_along</span><span class="p">(</span><span class="n">phase</span><span class="p">)){</span><span class="w">
    </span><span class="n">start</span><span class="w"> </span><span class="o"><-</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="nf">c</span><span class="p">(</span><span class="n">phase</span><span class="p">[</span><span class="n">j</span><span class="p">],</span><span class="w"> </span><span class="n">start</span><span class="p">))</span><span class="o">$</span><span class="n">res</span><span class="w">
  </span><span class="p">}</span><span class="w">
  </span><span class="n">res</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">start</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="nf">max</span><span class="p">(</span><span class="n">res</span><span class="p">)</span><span class="w">
</span>
## [1] 67023

Part two

<span class="n">library</span><span class="p">(</span><span class="n">purrr</span><span class="p">)</span><span class="w">
</span>
## 
## Attaching package: 'purrr'

## The following object is masked from 'package:magrittr':
## 
##     set_names
<span class="n">res</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0</span><span class="w">

</span><span class="n">comb</span><span class="w"> </span><span class="o"><-</span><span class="w">  </span><span class="n">expand.grid</span><span class="p">(</span><span class="m">5</span><span class="o">:</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="o">:</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="o">:</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="o">:</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="o">:</span><span class="m">9</span><span class="p">)</span><span class="w"> 

</span><span class="n">to_keep</span><span class="w"> </span><span class="o"><-</span><span class="w">  </span><span class="n">apply</span><span class="p">(</span><span class="n">comb</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">){</span><span class="w">
  </span><span class="nf">length</span><span class="p">(</span><span class="n">unique</span><span class="p">(</span><span class="n">x</span><span class="p">))</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">x</span><span class="p">)</span><span class="w">
</span><span class="p">})</span><span class="w">
</span><span class="n">comb</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">comb</span><span class="p">[</span><span class="o">!</span><span class="n">to_keep</span><span class="p">,</span><span class="w"> </span><span class="p">]</span><span class="w">

</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nf">seq_len</span><span class="p">(</span><span class="n">nrow</span><span class="p">(</span><span class="n">comb</span><span class="p">))){</span><span class="w">
  </span><span class="n">phase</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">comb</span><span class="p">[</span><span class="n">i</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="nf">as.numeric</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">prog</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="n">phase</span><span class="p">[</span><span class="m">1</span><span class="p">],</span><span class="w"> </span><span class="m">0</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">prog</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="n">phase</span><span class="p">[</span><span class="m">2</span><span class="p">],</span><span class="w"> </span><span class="n">A</span><span class="o">$</span><span class="n">res</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">prog</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="n">phase</span><span class="p">[</span><span class="m">3</span><span class="p">],</span><span class="w"> </span><span class="n">B</span><span class="o">$</span><span class="n">res</span><span class="p">))</span><span class="w">
  </span><span class="n">D</span><span class="w"> </span><span class="o"><-</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="nf">c</span><span class="p">(</span><span class="n">phase</span><span class="p">[</span><span class="m">4</span><span class="p">],</span><span class="w"> </span><span class="n">C</span><span class="o">$</span><span class="n">res</span><span class="p">))</span><span class="w">
  </span><span class="n">E</span><span class="w"> </span><span class="o"><-</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="nf">c</span><span class="p">(</span><span class="n">phase</span><span class="p">[</span><span class="m">5</span><span class="p">],</span><span class="w"> </span><span class="n">D</span><span class="o">$</span><span class="n">res</span><span class="p">))</span><span class="w">
  
  </span><span class="k">while</span><span class="p">(</span><span class="w">
    </span><span class="n">map_lgl</span><span class="p">(</span><span class="w">
      </span><span class="nf">list</span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="p">,</span><span class="w"> </span><span class="n">D</span><span class="p">,</span><span class="w"> </span><span class="n">E</span><span class="p">),</span><span class="w"> 
      </span><span class="s2">"processed"</span><span class="w">
    </span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">all</span><span class="p">()</span><span class="w">
  </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">prog</span><span class="p">(</span><span class="n">A</span><span class="o">$</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">E</span><span class="o">$</span><span class="n">res</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">A</span><span class="o">$</span><span class="n">pos</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">prog</span><span class="p">(</span><span class="n">B</span><span class="o">$</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">$</span><span class="n">res</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">B</span><span class="o">$</span><span class="n">pos</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">prog</span><span class="p">(</span><span class="n">C</span><span class="o">$</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="o">$</span><span class="n">res</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">C</span><span class="o">$</span><span class="n">pos</span><span class="p">)</span><span class="w">
    </span><span class="n">D</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">prog</span><span class="p">(</span><span class="n">D</span><span class="o">$</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="o">$</span><span class="n">res</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">D</span><span class="o">$</span><span class="n">pos</span><span class="p">)</span><span class="w">
    </span><span class="n">E</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">prog</span><span class="p">(</span><span class="n">E</span><span class="o">$</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">D</span><span class="o">$</span><span class="n">res</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">E</span><span class="o">$</span><span class="n">pos</span><span class="p">)</span><span class="w">
  </span><span class="p">}</span><span class="w">
  
  </span><span class="n">res</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">E</span><span class="o">$</span><span class="n">res</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="nf">max</span><span class="p">(</span><span class="n">res</span><span class="p">)</span><span class="w">
</span>
## [1] 7818398

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)