Advent of Code 2019-04 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-04 with R and JavaScript.

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

[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/4

R solution

Part one

<span class="n">test</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">156218</span><span class="o">:</span><span class="m">652527</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">cond</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">splt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">strsplit</span><span class="p">(</span><span class="w">
    </span><span class="nf">as.character</span><span class="p">(</span><span class="n">vec</span><span class="p">),</span><span class="w"> 
    </span><span class="s2">""</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="nf">as.numeric</span><span class="p">()</span><span class="w">
  </span><span class="n">adj_eq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="p">(</span><span class="n">splt</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">lag</span><span class="p">(</span><span class="n">splt</span><span class="p">))[</span><span class="m">-1</span><span class="p">]</span><span class="w">
  </span><span class="n">incre</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="p">(</span><span class="n">splt</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">lag</span><span class="p">(</span><span class="n">splt</span><span class="p">))[</span><span class="m">-1</span><span class="p">]</span><span class="w">
  </span><span class="nf">any</span><span class="p">(</span><span class="n">adj_eq</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="n">incre</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">vapply</span><span class="p">(</span><span class="n">test</span><span class="p">,</span><span class="w"> </span><span class="n">cond</span><span class="p">,</span><span class="w"> </span><span class="n">logical</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="nf">sum</span><span class="p">()</span><span class="w">
</span>
## [1] 1694

Part two

<span class="n">cond2</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">splt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">strsplit</span><span class="p">(</span><span class="w">
    </span><span class="nf">as.character</span><span class="p">(</span><span class="n">vec</span><span class="p">),</span><span class="w"> 
    </span><span class="s2">""</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="nf">as.numeric</span><span class="p">()</span><span class="w">
  </span><span class="n">adj_eq</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="p">(</span><span class="n">splt</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">lag</span><span class="p">(</span><span class="n">splt</span><span class="p">))[</span><span class="m">-1</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">any</span><span class="p">(</span><span class="n">adj_eq</span><span class="p">)){</span><span class="w">
    </span><span class="n">adj_eq</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">table</span><span class="p">(</span><span class="n">splt</span><span class="p">)</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="n">incre</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="p">(</span><span class="n">splt</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">lag</span><span class="p">(</span><span class="n">splt</span><span class="p">))[</span><span class="m">-1</span><span class="p">]</span><span class="w">
  </span><span class="nf">any</span><span class="p">(</span><span class="n">adj_eq</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="n">incre</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">vapply</span><span class="p">(</span><span class="n">test</span><span class="p">,</span><span class="w"> </span><span class="n">cond2</span><span class="p">,</span><span class="w"> </span><span class="n">logical</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="nf">sum</span><span class="p">()</span><span class="w">
</span>
## [1] 1148

JS solution

Part one & Two

// Generating the array (from
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)

<span class="kd">const</span> <span class="nx">range</span> <span class="o">=</span> <span class="p">(</span><span class="nx">start</span><span class="p">,</span> <span class="nx">stop</span><span class="p">,</span> <span class="nx">step</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">=></span> <span class="nb">Array</span><span class="p">.</span><span class="k">from</span><span class="p">({</span> <span class="na">length</span><span class="p">:</span> <span class="p">(</span><span class="nx">stop</span> <span class="o">-</span> <span class="nx">start</span><span class="p">)</span> <span class="o">/</span> <span class="nx">step</span> <span class="o">+</span> <span class="mi">1</span><span class="p">},</span> <span class="p">(</span><span class="nx">_</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span> <span class="nx">start</span> <span class="o">+</span> <span class="p">(</span><span class="nx">i</span> <span class="o">*</span> <span class="nx">step</span><span class="p">));</span>
<span class="kd">var</span> <span class="nx">test</span> <span class="o">=</span> <span class="nx">range</span><span class="p">(</span><span class="mi">156218</span><span class="p">,</span> <span class="mi">652527</span><span class="p">)</span>
<span class="kd">function</span> <span class="nx">lag</span><span class="p">(</span><span class="nx">vec</span><span class="p">){</span>
  <span class="kd">var</span> <span class="nx">res</span> <span class="o">=</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="kc">null</span><span class="p">);</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">++</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">i</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="kd">function</span> <span class="nx">cond</span><span class="p">(</span><span class="nx">vec</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">toString</span><span class="p">().</span><span class="nx">split</span><span class="p">(</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="kd">var</span> <span class="nx">laged</span> <span class="o">=</span> <span class="nx">lag</span><span class="p">(</span><span class="nx">vec</span><span class="p">);</span>
  <span class="kd">var</span> <span class="nx">adj_eq</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span> <span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span> <span class="nx">x</span> <span class="o">==</span> <span class="nx">laged</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">)</span>
  <span class="nx">adj_eq</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span>
  <span class="kd">var</span> <span class="nx">adj_eq</span> <span class="o">=</span> <span class="nx">adj_eq</span><span class="p">.</span><span class="nx">some</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="p">);</span>
  <span class="kd">var</span> <span class="nx">incre</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span> <span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span> <span class="nx">x</span> <span class="o">>=</span> <span class="nx">laged</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">)</span>
  <span class="nx">incre</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span>
  <span class="kd">var</span> <span class="nx">incre</span> <span class="o">=</span> <span class="nx">incre</span><span class="p">.</span><span class="nx">every</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="p">);</span>
  <span class="kd">var</span> <span class="nx">all</span> <span class="o">=</span> <span class="p">[</span><span class="nx">incre</span><span class="p">,</span> <span class="nx">adj_eq</span><span class="p">].</span><span class="nx">every</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="p">);</span>
  <span class="k">return</span> <span class="nx">all</span>
<span class="p">}</span>
<span class="nx">test</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">cond</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span><span class="nx">v</span> <span class="o">=></span> <span class="nx">v</span><span class="p">).</span><span class="nx">length</span>
## 1694
<span class="kd">function</span> <span class="nx">table</span><span class="p">(</span><span class="nx">vec</span><span class="p">){</span>
  <span class="kd">var</span> <span class="nx">tbl</span> <span class="o">=</span> <span class="p">{}</span>
  <span class="nx">vec</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">){</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">tbl</span><span class="p">[</span><span class="nx">x</span><span class="p">]){</span>
      <span class="nx">tbl</span><span class="p">[</span><span class="nx">x</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tbl</span><span class="p">[</span><span class="nx">x</span><span class="p">]</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">tbl</span><span class="p">[</span><span class="nx">x</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">})</span>
  <span class="k">return</span> <span class="nx">tbl</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">cond2</span><span class="p">(</span><span class="nx">vec</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">toString</span><span class="p">().</span><span class="nx">split</span><span class="p">(</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="kd">var</span> <span class="nx">laged</span> <span class="o">=</span> <span class="nx">lag</span><span class="p">(</span><span class="nx">vec</span><span class="p">);</span>
  <span class="kd">var</span> <span class="nx">adj_eq</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span> <span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span> <span class="nx">x</span> <span class="o">==</span> <span class="nx">laged</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">)</span>
  <span class="nx">adj_eq</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span>
  <span class="kd">var</span> <span class="nx">adj_eq</span> <span class="o">=</span> <span class="nx">adj_eq</span><span class="p">.</span><span class="nx">some</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="p">);</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">adj_eq</span><span class="p">){</span>
    <span class="kd">var</span> <span class="nx">tb</span> <span class="o">=</span> <span class="nx">table</span><span class="p">(</span><span class="nx">vec</span><span class="p">);</span>
    <span class="kd">var</span> <span class="nx">res</span> <span class="o">=</span> <span class="p">[];</span>
    <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="k">in</span> <span class="nx">tb</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">tb</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="kd">var</span> <span class="nx">adj_eq</span> <span class="o">=</span> <span class="nx">res</span><span class="p">.</span><span class="nx">some</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="p">}</span>
  
  <span class="kd">var</span> <span class="nx">incre</span> <span class="o">=</span> <span class="nx">vec</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span> <span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=></span> <span class="nx">x</span> <span class="o">>=</span> <span class="nx">laged</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">);</span>
  <span class="nx">incre</span><span class="p">.</span><span class="nx">shift</span><span class="p">();</span>
  <span class="kd">var</span> <span class="nx">incre</span> <span class="o">=</span> <span class="nx">incre</span><span class="p">.</span><span class="nx">every</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="p">);</span>
  <span class="kd">var</span> <span class="nx">all</span> <span class="o">=</span> <span class="p">[</span><span class="nx">incre</span><span class="p">,</span> <span class="nx">adj_eq</span><span class="p">].</span><span class="nx">every</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="p">);</span>
  <span class="k">return</span> <span class="nx">all</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">test</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">cond2</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span><span class="nx">v</span> <span class="o">=></span> <span class="nx">v</span><span class="p">).</span><span class="nx">length</span><span class="p">;</span>
## 1148

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)