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

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

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

R solution

Part one

<span class="c1"># Read</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="w"> </span><span class="s2">"input1.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="w"> </span><span class="p">)</span><span class="w">
</span><span class="c1"># Get the sum of each element, divided by 3, rounded down, and substracted 2</span><span class="w">
</span><span class="nf">sum</span><span class="p">(</span><span class="w"> </span><span class="nf">floor</span><span class="p">(</span><span class="w"> </span><span class="n">ipt</span><span class="o">$</span><span class="n">V1</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="p">)</span><span class="w">
</span>
## [1] 3361299

Part two

Using a recursive function:
https://en.wikipedia.org/wiki/Recursion_(computer_science)

<span class="n">floorish</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">start</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">loc</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">floor</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="m">3</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">2</span><span class="w">
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">loc</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">start</span><span class="w"> </span><span class="o"><-</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">loc</span><span class="w">
    </span><span class="n">floorish</span><span class="p">(</span><span class="n">loc</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">)</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="nf">return</span><span class="p">(</span><span class="n">start</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="nf">sum</span><span class="p">(</span><span class="w"> </span><span class="n">purrr</span><span class="o">::</span><span class="n">map_dbl</span><span class="p">(</span><span class="n">ipt</span><span class="o">$</span><span class="n">V1</span><span class="p">,</span><span class="w"> </span><span class="n">floorish</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w">
</span>
## [1] 5039071

JS solution

Part one & Two

<span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">fs</span><span class="dl">'</span><span class="p">);</span>

<span class="c1">// Reading the file</span>
<span class="kd">var</span> <span class="nx">res</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">input1.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="se">\n</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="p">);</span>

<span class="c1">// Turning to integer</span>
<span class="nx">res</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="nb">parseInt</span><span class="p">(</span><span class="nx">x</span><span class="p">));</span>

<span class="c1">// Doing the floor of division less 2</span>
<span class="kd">var</span> <span class="nx">val</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="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nx">x</span> <span class="o">/</span> <span class="mi">3</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="p">);</span>

<span class="c1">// Suming</span>
<span class="kd">var</span> <span class="nx">add</span> <span class="o">=</span> <span class="p">(</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">)</span> <span class="o">=></span> <span class="nx">x</span> <span class="o">+</span> <span class="nx">y</span><span class="p">;</span>

<span class="c1">// Solution</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">val</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">add</span><span class="p">)</span> <span class="p">);</span>

<span class="c1">// Creating the recursive function</span>
<span class="kd">function</span> <span class="nx">floorish</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">start</span> <span class="o">=</span> <span class="mi">0</span><span class="p">){</span>
  <span class="nx">loc</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nx">val</span> <span class="o">/</span> <span class="mi">3</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">loc</span> <span class="o">></span> <span class="mi">0</span><span class="p">){</span>
    <span class="nx">start</span> <span class="o">+=</span> <span class="nx">loc</span><span class="p">;</span>
    <span class="k">return</span> <span class="nx">floorish</span><span class="p">(</span><span class="nx">loc</span><span class="p">,</span> <span class="nx">start</span><span class="p">);</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">start</span><span class="p">;</span>
  <span class="p">}</span> 
<span class="p">}</span>

<span class="c1">// Doing the computation</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</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">floorish</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span> <span class="p">).</span><span class="nx">reduce</span><span class="p">(</span> <span class="nx">add</span> <span class="p">)</span> <span class="p">);</span>
## 3361299
## 5039071

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)