Advent of Code 2019-06 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-06 with R (and no JavaScript this time).

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

Instructions

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

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">igraph</span><span class="p">)</span><span class="w">
</span>
## 
## Attaching package: 'igraph'

## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum

## The following object is masked from 'package:base':
## 
##     union
<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">"input6.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">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="n">stringsAsFactor</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">grph</span><span class="w"> </span><span class="o"><-</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">graph_from_data_frame</span><span class="p">()</span><span class="w"> 

</span><span class="n">grph</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">distance_table</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">extract2</span><span class="p">(</span><span class="s2">"res"</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] 147807

Part two

<span class="n">distances</span><span class="p">(</span><span class="w">
  </span><span class="n">grph</span><span class="p">,</span><span class="w"> 
  </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">V</span><span class="p">(</span><span class="n">grph</span><span class="p">)[[</span><span class="s2">"YOU"</span><span class="p">]],</span><span class="w"> 
  </span><span class="n">to</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">V</span><span class="p">(</span><span class="n">grph</span><span class="p">)[[</span><span class="s2">"SAN"</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="m">2</span><span class="w">
</span>
##     SAN
## YOU 229

JS solution

Nop, no JS solution today… I didn’t feel like reimplementing igraph in
JavaScript, neither digging into the graph modules today 😉

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)