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

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

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/8

R solution

Part one

<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">purrr</span><span class="p">)</span><span class="w">

</span><span class="n">ipt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read.delim</span><span class="p">(</span><span class="s2">"input8.txt"</span><span class="p">,</span><span class="w"> </span><span class="n">header</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">,</span><span class="w"> </span><span class="n">colClasses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"character"</span><span class="p">)</span><span class="o">$</span><span class="n">V1</span><span class="w">
</span><span class="n">ipt</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">ipt</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">layers_size</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">6</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">25</span><span class="w">
</span><span class="n">l</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">list</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="m">1</span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">ipt</span><span class="p">)</span><span class="o">/</span><span class="n">layers_size</span><span class="p">)){</span><span class="w">
  </span><span class="n">l</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">ipt</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">150</span><span class="p">]</span><span class="w">
  </span><span class="n">ipt</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ipt</span><span class="p">[</span><span class="m">151</span><span class="o">:</span><span class="nf">length</span><span class="p">(</span><span class="n">ipt</span><span class="p">)]</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">mn</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">lapply</span><span class="p">(</span><span class="n">table</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">map_dbl</span><span class="p">(</span><span class="s2">"0"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">which.min</span><span class="p">()</span><span class="w">

</span><span class="n">l</span><span class="p">[[</span><span class="n">mn</span><span class="p">]]</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">table</span><span class="p">()</span><span class="w">
</span>
## .
##   0   1   2 
##   7  14 129
<span class="m">14</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">129</span><span class="w">
</span>
## [1] 1806

Part two

<span class="n">v</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</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">layers_size</span><span class="p">)){</span><span class="w">
  </span><span class="n">idx</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map_dbl</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> 
  </span><span class="n">v</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">idx</span><span class="p">[</span><span class="n">idx</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">1</span><span class="p">)][</span><span class="m">1</span><span class="p">]</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tibble</span><span class="p">)</span><span class="w">
</span><span class="n">matrix</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n">ncol</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="o">%>%</span><span class="w"> 
  </span><span class="n">as.data.frame</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">rowid_to_column</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">gather</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">V1</span><span class="o">:</span><span class="n">V6</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">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gsub</span><span class="p">(</span><span class="s2">"V(.)"</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\1"</span><span class="p">,</span><span class="w"> </span><span class="n">key</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="o">%>%</span><span class="w">
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">rowid</span><span class="p">,</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.factor</span><span class="p">(</span><span class="n">value</span><span class="p">)))</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">geom_tile</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">coord_fixed</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">scale_fill_viridis_d</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">scale_y_reverse</span><span class="p">()</span><span class="w">
</span>

JS solution

<span class="kd">var</span> <span class="nx">ipt</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">input8.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="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="o">&</span> <span class="nx">x</span> <span class="o">!=</span> <span class="dl">'</span><span class="se">\n</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">layers_size</span> <span class="o">=</span> <span class="mi">6</span> <span class="o">*</span> <span class="mi">25</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">layer_n</span> <span class="o">=</span> <span class="nx">ipt</span><span class="p">.</span><span class="nx">length</span> <span class="o">/</span> <span class="nx">layers_size</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="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="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">layer_n</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">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ipt</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">layers_size</span><span class="p">);</span>
<span class="p">}</span>

<span class="kd">var</span> <span class="nx">res_b</span> <span class="o">=</span> <span class="nx">res</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="nx">table</span><span class="p">(</span><span class="nx">x</span><span class="p">));</span>
<span class="kd">var</span> <span class="nx">minim</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span> <span class="nx">res_b</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="nx">x</span><span class="p">[</span><span class="dl">'</span><span class="s1">0</span><span class="dl">'</span><span class="p">]));</span>
<span class="kd">var</span> <span class="nx">smallest</span> <span class="o">=</span> <span class="nx">res_b</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="dl">'</span><span class="s1">0</span><span class="dl">'</span><span class="p">]</span> <span class="o">==</span> <span class="nx">minim</span><span class="p">);</span>
<span class="nx">smallest</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="dl">"</span><span class="s2">1</span><span class="dl">"</span><span class="p">]</span> <span class="o">*</span> <span class="nx">smallest</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="dl">"</span><span class="s2">2</span><span class="dl">"</span><span class="p">];</span>
## 1806
<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</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">layers_size</span><span class="p">;</span> <span class="nx">i</span> <span class="o">++</span><span class="p">){</span>
  <span class="kd">var</span> <span class="nx">idx</span> <span class="o">=</span> <span class="nx">res</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="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
  <span class="nx">v</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">idx</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">z</span> <span class="o">=></span> <span class="nx">z</span><span class="o">==</span> <span class="mi">0</span> <span class="o">|</span> <span class="nx">z</span> <span class="o">==</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>

<span class="kd">var</span> <span class="nx">nn</span> <span class="o">=</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="mi">6</span><span class="p">;</span> <span class="nx">i</span> <span class="o">++</span><span class="p">){</span>
  <span class="nx">nn</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">25</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="dl">"</span><span class="s2"> </span><span class="dl">"</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/0/g</span><span class="p">,</span> <span class="dl">"</span><span class="s2"> </span><span class="dl">"</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">nn</span>
## [ '    1 1     1 1     1 1 1 1   1 1 1       1 1    ',
##   '      1   1     1   1         1     1   1     1  ',
##   '      1   1     1   1 1 1     1     1   1     1  ',
##   '      1   1 1 1 1   1         1 1 1     1 1 1 1  ',
##   '1     1   1     1   1         1   1     1     1  ',
##   '  1 1     1     1   1         1     1   1     1  ' ]

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)