<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/my-r-take-on-advent-of-code-day-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r-tastic.co.uk/post/my-r-take-on-advent-of-code-day-2/"> r-tastic</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is my second blog post from the series of <code>My R take on Advent of Code</code>. If you’d like to know more about Advent of Code, check out the <a rel="nofollow" href="https://r-tastic.co.uk/post/advent-of-code-in-r-day-1/" target="_blank">first post from the series</a> or simply go to <a rel="nofollow" href="https://adventofcode.com/2018" target="_blank">their website</a>. Below you’ll find the challnge from Day 2 and the solution that worked for me. As always, feel free to leave comments if you have different ideas on how this could have been solved!</p>
<div>
<h3>Day 2 Puzzle</h3>
<blockquote>
<blockquote>
<p>(…) you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.<br>
For example, if you see the following box IDs:</p>
</blockquote>
</blockquote>
<blockquote>
<blockquote>
<p><code>abcdef</code> contains no letters that appear exactly two or three times.<br>
<code>bababc</code> contains two <code>a</code> and three <code>b</code>, so it counts for both.<br>
<code>abbcde</code> contains two <code>b</code>, but no letter appears exactly three times.<br>
<code>abcccd</code> contains three <code>c</code>, but no letter appears exactly two times.<br>
<code>aabcdd</code> contains two <code>a</code> and two <code>d</code>, but it only counts once.<br>
<code>abcdee</code> contains two <code>e</code>.<br>
<code>ababab</code> contains three <code>a</code> and three <code>b</code>, but it only counts once.</p>
</blockquote>
</blockquote>
<blockquote>
<blockquote>
<p>Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.<br>
What is the checksum for your list of box IDs?</p>
</blockquote>
</blockquote>
<p>So what is it all about? As complicated as it may sound, essentially we need to:</p>
<ul>
<li>understand which string contains letters that appear exactly 2 times</li>
<li>understand which string contains letters that appear exactly 3 times</li>
<li>count the number of each type of string</li>
<li>multiply them together</li>
</ul>
<p>Doesn’t sound so bad anymore, ey? This is how we can go about it:</p>
<p>First load your key packages…</p>
<pre><code>library(dplyr)
library(stringr)
library(tibble)
library(purrr)</code></pre>
<p>… and have a look at what the raw input looks like.</p>
<pre><code># check raw input
glimpse(input)</code></pre>
<pre><code>##  chr &quot;xrecqmdonskvzupalfkwhjctdb\nxrlgqmavnskvzupalfiwhjctdb\nxregqmyonskvzupalfiwhjpmdj\nareyqmyonskvzupalfiwhjcidb\&quot;| __truncated__</code></pre>
<p>Right, Advent of Code will never give you nice and clean data to work with, that’s for sure. But it doesn’t look like things are too bad this time – let’s just split it by the new line and keep it as a vector for now. Does it look reaosnably good?</p>
<pre><code># clean it
clean_input =  strsplit(input, '\n') %>% unlist()   # splt by NewLine
glimpse(clean_input)</code></pre>
<pre><code>##  chr [1:250] &quot;xrecqmdonskvzupalfkwhjctdb&quot; &quot;xrlgqmavnskvzupalfiwhjctdb&quot; ...</code></pre>
<p>Much better! Now, let’s put it all in a data frame for now, we’ll need it very soon.</p>
<pre><code># put it in the data.frame
df2 &lt;- tibble(input = str_trim(clean_input))
head(df2)</code></pre>
<pre><code>## # A tibble: 6 x 1
##   input                     
##   </code></pre></div>