Solving #AdventOfCode day 5 and 6 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 the puzzles of Advent of Code with
R.

[Disclaimer] Obviously, this post contains a big spoiler about Advent
of Code, as it gives solutions for solving days quoted in the title.

Advent of Code

Advent of Code is an Advent calendar of small programming puzzles for
a variety of skill sets and skill levels that can be solved in any
programming language you like. About Advent of
Code

Day five, part one

The first part of the challenge for day 3 starts well: we have to read
in R a file with A LOT of letters 🙂

<span class="n">vec</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">readLines</span><span class="p">(</span><span class="s2">"input5.txt"</span><span class="p">)</span><span class="w">
</span><span class="n">substr</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><span class="m">30</span><span class="p">)</span><span class="w">
</span>
## [1] "MmMmMxcRrCmMAaEeiIMmopZzPHxEeH"

This character vector is a polymer, and it describes chemical reactions.

The polymer is formed by smaller units which, when triggered, react
with each other such that two adjacent units of the same type and
opposite polarity are destroyed. Units’ types are represented by
letters; units’ polarity is represented by capitalization. For
instance, r and R are units with the same type but opposite polarity,
whereas r and s are entirely different types and do not react.

So the first question was:

How many units remain after fully reacting the polymer you scanned?

Let’s use a regex to do that 🙂 First, we’ll create a regex that
describes any possible combination of upper/lower
letters:

<span class="n">regex</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="nb">letters</span><span class="p">,</span><span class="w"> </span><span class="nb">LETTERS</span><span class="p">),</span><span class="w"> </span><span class="s2">"|"</span><span class="p">,</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="nb">LETTERS</span><span class="p">,</span><span class="w"> </span><span class="nb">letters</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="p">)</span><span class="w">
</span><span class="n">regex</span><span class="w">
</span>
## [1] "aA|Aa|bB|Bb|cC|Cc|dD|Dd|eE|Ee|fF|Ff|gG|Gg|hH|Hh|iI|Ii|jJ|Jj|kK|Kk|lL|Ll|mM|Mm|nN|Nn|oO|Oo|pP|Pp|qQ|Qq|rR|Rr|sS|Ss|tT|Tt|uU|Uu|vV|Vv|wW|Ww|xX|Xx|yY|Yy|zZ|Zz"
<span class="c1"># Check that the regex is there</span><span class="w">
</span><span class="n">grepl</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span><span class="w"> </span><span class="n">vec</span><span class="p">)</span><span class="w">
</span>
## [1] TRUE

So, we’ll need to run a loop that removes this regex until there is
nothing to remove anymore. The idea is to get the number of characters
before the gsub, perform the sub, and when the removal is done,
count if the number of characters from before the subtraction is
different from the number of characters after (i.e, if it is the same,
there is nothing to gsub anymore).

<span class="n">continue</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">TRUE</span><span class="w">
</span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">continue</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">old_size</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nchar</span><span class="p">(</span><span class="n">vec</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">gsub</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span><span class="w"> </span><span class="s2">""</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">continue</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nchar</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">old_size</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1"># Check that the regex is not there anymore</span><span class="w">
</span><span class="n">grepl</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span><span class="w"> </span><span class="n">vec</span><span class="p">)</span><span class="w">
</span>
## [1] FALSE
<span class="n">nchar</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span><span class="w">
</span>
## [1] 9288

Day five, part two

With part 2, we need to try to first remove, one by one, the couples of
units (for example “aA”), and react the polymer without each couple.
Then, we need to find:

What is the length of the shortest polymer you can produce by removing
all units of exactly one type and fully reacting the result?

Let’s start by putting our last code in a function:

<span class="n">react</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">regex</span><span class="w"> </span><span class="o">=</span><span class="w">  </span><span class="n">paste0</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="nb">letters</span><span class="p">,</span><span class="w"> </span><span class="nb">LETTERS</span><span class="p">),</span><span class="w"> </span><span class="s2">"|"</span><span class="p">,</span><span class="w"> 
                                  </span><span class="n">paste0</span><span class="p">(</span><span class="nb">LETTERS</span><span class="p">,</span><span class="w"> </span><span class="nb">letters</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="p">)){</span><span class="w">
  </span><span class="n">continue</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="kc">TRUE</span><span class="w">
  </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">continue</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">old_size</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nchar</span><span class="p">(</span><span class="n">vec</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">gsub</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span><span class="w"> </span><span class="s2">""</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">continue</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nchar</span><span class="p">(</span><span class="n">vec</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">old_size</span><span class="w">
  </span><span class="p">}</span><span class="w">
  </span><span class="n">vec</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

Let’s try with the examples from the website:

<span class="n">nchar</span><span class="p">(</span><span class="n">react</span><span class="p">(</span><span class="s2">"dbcCCBcCcD"</span><span class="p">))</span><span class="w">
</span>
## [1] 6
<span class="n">nchar</span><span class="p">(</span><span class="n">react</span><span class="p">(</span><span class="s2">"daAcCaCAcCcaDA"</span><span class="p">))</span><span class="w">
</span>
## [1] 8
<span class="n">nchar</span><span class="p">(</span><span class="n">react</span><span class="p">(</span><span class="s2">"dabAaBAaDA"</span><span class="p">))</span><span class="w">
</span>
## [1] 4
<span class="n">nchar</span><span class="p">(</span><span class="n">react</span><span class="p">(</span><span class="s2">"abAcCaCBAcCcaA"</span><span class="p">))</span><span class="w">
</span>
## [1] 6

Now we’ll combine pattern removal and reaction:

<span class="n">clean_and_react</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">pattern</span><span class="p">){</span><span class="w">
  </span><span class="n">react</span><span class="p">(</span><span class="w"> </span><span class="n">gsub</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w"> </span><span class="n">vec</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">clean_and_react</span><span class="p">(</span><span class="s2">"dabAcCaCBAcCcaDA"</span><span class="p">,</span><span class="w"> </span><span class="s2">"a|A"</span><span class="p">)</span><span class="w"> 
</span>
## [1] "dbCBcD"

Then, a function to get a tibble with: extracted pattern, and number of
characters:

<span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">clean_and_react_and_count</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">pattern</span><span class="p">){</span><span class="w">
  </span><span class="n">tibble</span><span class="p">(</span><span class="w">
    </span><span class="n">pattern</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pattern</span><span class="p">,</span><span class="w"> 
    </span><span class="n">nchars</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nchar</span><span class="p">(</span><span class="n">clean_and_react</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">pattern</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">clean_and_react_and_count</span><span class="p">(</span><span class="s2">"dabAcCaCBAcCcaDA"</span><span class="p">,</span><span class="w"> </span><span class="s2">"a|A"</span><span class="p">)</span><span class="w"> 
</span>
## # A tibble: 1 x 2
##   pattern nchars
##   <chr>    <int>
## 1 a|A          6

As it should take some time, let’s use {furrr} to do our calculation:

<span class="n">library</span><span class="p">(</span><span class="n">furrr</span><span class="p">)</span><span class="w">
</span>
## Loading required package: future
<span class="n">plan</span><span class="p">(</span><span class="n">multiprocess</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">future_map_dfr</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="nb">LETTERS</span><span class="p">,</span><span class="s2">"|"</span><span class="p">,</span><span class="w"> </span><span class="nb">letters</span><span class="p">),</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">clean_and_react_and_count</span><span class="p">(</span><span class="n">vec</span><span class="p">,</span><span class="w"> </span><span class="n">.x</span><span class="p">))</span><span class="w">
</span>

What’s the best solution?

<span class="n">top_n</span><span class="p">(</span><span class="n">res</span><span class="p">,</span><span class="w"> </span><span class="m">-1</span><span class="p">)</span><span class="w">
</span>
## Selecting by nchars

## # A tibble: 1 x 2
##   pattern nchars
##   <chr>    <int>
## 1 F|f       5844

Day six, part one

Now we’re working with distance calculation. We’ve been provided a bunch
of coordinates. Once we have put these on a grid, we need to fill the
“empty cells” with the reference to the closest coordinate from our
input, by calculating the shortest manhattan
distance
.

<span class="n">day6</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"input6.txt"</span><span class="p">,</span><span class="w"> </span><span class="n">col_names</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"V1"</span><span class="p">,</span><span class="w"> </span><span class="s2">"V2"</span><span class="p">))</span><span class="w">
</span>
## Parsed with column specification:
## cols(
##   V1 = col_integer(),
##   V2 = col_integer()
## )
<span class="n">day6</span><span class="o">$</span><span class="n">id</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">50</span><span class="p">)</span><span class="w">
</span><span class="n">day6</span><span class="w">
</span>
## # A tibble: 50 x 3
##       V1    V2 id   
##    <int> <int> <chr>
##  1   315   342 1    
##  2    59   106 2    
##  3    44   207 3    
##  4    52    81 4    
##  5   139   207 5    
##  6    93   135 6    
##  7   152   187 7    
##  8   271    47 8    
##  9   223   342 9    
## 10    50   255 10   
## # ... with 40 more rows

First of all, let’s get a list of all the “empty cells” we mentioned
before:

<span class="n">all_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="w">
  </span><span class="nf">min</span><span class="p">(</span><span class="n">day6</span><span class="o">$</span><span class="n">V</span><span class="m">1</span><span class="p">)</span><span class="o">:</span><span class="nf">max</span><span class="p">(</span><span class="n">day6</span><span class="o">$</span><span class="n">V</span><span class="m">1</span><span class="p">),</span><span class="w"> 
  </span><span class="nf">min</span><span class="p">(</span><span class="n">day6</span><span class="o">$</span><span class="n">V</span><span class="m">2</span><span class="p">)</span><span class="o">:</span><span class="nf">max</span><span class="p">(</span><span class="n">day6</span><span class="o">$</span><span class="n">V</span><span class="m">2</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">as_tibble</span><span class="p">()</span><span class="w">
</span>

The Manhattan distance function:

<span class="n">manat_dist</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">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">){</span><span class="w">
  </span><span class="nf">abs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

A function to find the closer ID, given an x and a y:

<span class="n">closest_id</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">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">df</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">day6</span><span class="p">){</span><span class="w">
  </span><span class="n">df</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">dist</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">manat_dist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">V</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">manat_dist</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="m">2</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">top_n</span><span class="p">(</span><span class="m">-1</span><span class="p">,</span><span class="w"> </span><span class="n">dist</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">pull</span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1"># Apply it on all our combination</span><span class="w">
</span><span class="n">cl</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">future_pmap_chr</span><span class="p">(</span><span class="n">all_comb</span><span class="p">,</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">...</span><span class="p">){</span><span class="w">
  </span><span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">closest_id</span><span class="p">(</span><span class="n">.</span><span class="m">.1</span><span class="p">,</span><span class="w"> </span><span class="n">.</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="nf">length</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="m">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="kc">NA</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">x</span><span class="w">
  </span><span class="p">}</span><span class="w">
</span><span class="p">})</span><span class="w"> 
</span>

And now, time to answer the puzzle:

What is the size of the largest area that isn’t infinite?

An infinite area is defined by the fact that at least one of its element
is on the edge of the grid (hence equal to the min or max of V1 or V2).

<span class="n">all_comb</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">all_comb</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="w">
    </span><span class="n">closest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cl</span><span class="p">,</span><span class="w"> 
    </span><span class="n">max1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">max</span><span class="p">(</span><span class="n">Var1</span><span class="p">),</span><span class="w">
    </span><span class="n">max2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">max</span><span class="p">(</span><span class="n">Var2</span><span class="p">),</span><span class="w">
    </span><span class="n">min1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">min</span><span class="p">(</span><span class="n">Var1</span><span class="p">),</span><span class="w">
    </span><span class="n">min2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">min</span><span class="p">(</span><span class="n">Var2</span><span class="p">),</span><span class="w">
  </span><span class="p">)</span><span class="w">

</span><span class="c1"># Get if each row are on the border</span><span class="w">
</span><span class="n">is_border</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pmap_lgl</span><span class="p">(</span><span class="n">all_comb</span><span class="p">,</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">...</span><span class="p">){</span><span class="w">
  </span><span class="n">.</span><span class="m">.1</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">.</span><span class="m">.4</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">.</span><span class="m">.1</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">.</span><span class="m">.6</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">.</span><span class="m">.2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">.</span><span class="m">.5</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">.</span><span class="m">.2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">.</span><span class="m">.7</span><span class="w">
</span><span class="p">})</span><span class="w">

</span><span class="n">all_comb</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">is_border</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">is_border</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">closest</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">is_bord</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">any</span><span class="p">(</span><span class="n">is_border</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="n">is_bord</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">count</span><span class="p">(</span><span class="n">closest</span><span class="p">,</span><span class="w"> </span><span class="n">sort</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ungroup</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">top_n</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w">
</span>
## Selecting by n

## # A tibble: 1 x 2
##   closest     n
##   <chr>   <int>
## 1 13       4290

Day six, part two

Now, we need to compute the distance of each point on the grid to each
coordinates, and to answer this question:

What is the size of the region containing all locations which have a
total distance to all given coordinates of less than 10000?

In other word, we get the distance to each coordinate on each cell, and
keep only the one with a total Manhattan distance which is less than
1000:

<span class="c1"># Create a function to compute all distances</span><span class="w">
</span><span class="n">all_dist</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">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">df</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">day6</span><span class="p">){</span><span class="w">
  </span><span class="n">df</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">mutate</span><span class="p">(</span><span class="n">dist</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">manat_dist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">V</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">manat_dist</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="m">2</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="c1"># And use it on the all_comb table</span><span class="w">
</span><span class="n">future_pmap</span><span class="p">(</span><span class="n">all_comb</span><span class="p">,</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">...</span><span class="p">){</span><span class="w">
  </span><span class="n">all_dist</span><span class="p">(</span><span class="n">.</span><span class="m">.1</span><span class="p">,</span><span class="w"> </span><span class="n">.</span><span class="m">.2</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">map_int</span><span class="p">(</span><span class="o">~</span><span class="nf">sum</span><span class="p">(</span><span class="n">.x</span><span class="o">$</span><span class="n">dist</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">keep</span><span class="p">(</span><span class="o">~</span><span class="w"> </span><span class="n">.x</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m">10000</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="w">
</span>
## [1] 37318

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)