<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-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r-tastic.co.uk/post/advent-of-code-day-3/"> r-tastic</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Ho, ho, ho, Happy Chris.. New Year? Between eating the sea of fish (as the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Christmas_in_Poland" target="_blank">Polish tradition requires</a>), assembling doll houses and designing a new kitchen, I finally managed to publish the third post on <a rel="nofollow" href="https://r-tastic.co.uk/post/advent-of-code-in-r-day-1/" target="_blank">My R take on Advent of Code</a>. To keep things short and sweet, here’s the original challenge:</p>
<blockquote>
<blockquote>
<p>Each Elf has made a claim about which area of fabric would be ideal for Santa’s suit. All claims have an ID and consist of a single rectangle with edges parallel to the edges of the fabric. Each claim’s rectangle is defined as follows:<br>
The number of inches between the left edge of the fabric and the left edge of the rectangle.<br>
The number of inches between the top edge of the fabric and the top edge of the rectangle.<br>
The width of the rectangle in inches.<br>
The height of the rectangle in inches.<br>
A claim like #123 @ 3,2: 5×4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall. Visually, it claims the square inches of fabric represented by # (and ignores the square inches of fabric represented by .) in the diagram below:</p>
</blockquote>
</blockquote>
<p>………..<br>
………..<br>
…#####…<br>
…#####…<br>
…#####…<br>
…#####…<br>
………..<br>
………..<br>
………..</p>
<blockquote>
<blockquote>
<p>The problem is that many of the claims overlap, causing two or more claims to cover part of the same areas. For example, consider the following claims:</p>
</blockquote>
</blockquote>
<p>#1 @ 1,3: 4×4<br>
#2 @ 3,1: 4×4<br>
#3 @ 5,5: 2×2</p>
<blockquote>
<blockquote>
<p>Visually, these claim the following areas:</p>
</blockquote>
</blockquote>
<p>……..<br>
…2222.<br>
…2222.<br>
.11XX22.<br>
.11XX22.<br>
.111133.<br>
.111133.<br>
……..</p>
<blockquote>
<blockquote>
<p>The four square inches marked with X are claimed by both 1 and 2. (Claim 3, while adjacent to the others, does not overlap either of them.). If the Elves all proceed with their own plans, none of them will have enough fabric. How many square inches of fabric are within two or more claims?</p>
</blockquote>
</blockquote>
<p>This is interesting! let’s load <code>tidyverse</code> and have a quick look at the data:</p>
<pre><code>library(tidyverse)

raw_input &lt;- read.delim('day3-raw-input.txt', header = FALSE)
head(raw_input)</code></pre>
<pre><code>##                    V1
## 1 #1 @ 850,301: 23x12
## 2 #2 @ 898,245: 15x10
## 3   #3 @ 8,408: 12x27
## 4 #4 @ 532,184: 16x13
## 5 #5 @ 550,829: 11x10
## 6 #6 @ 656,906: 13x12</code></pre>
<p>Ha! It looks like we need to first extract each dimension from the original input – easy-peasy with a little bit of regex and <code>parse_number</code>:</p>
<pre><code># separate and clean dimension figures 
clean_input &lt;- raw_input %>%
  rename(input = V1) %>% 
  mutate(ID = str_extract(input, '#[:digit:]+'), # extract ID 
         from_left_edge = str_extract(input, '@..?[:digit:]+\\,'), # extract right squares
         from_top_endge = str_extract(input, '\\,[:digit:]+\\:'), # extract top square
         width = str_extract(input, '[:digit:]+x'), # extract left dimension
         height = str_extract(input, 'x[:digit:]+')# extract right dimension
  ) %>% 
  mutate_if(is.character, readr::parse_number) # extract numbers


head(clean_input, 10)</code></pre>
<pre><code>##                  input ID from_left_edge from_top_endge width height
## 1  #1 @ 850,301: 23x12  1            850            301    23     12
## 2  #2 @ 898,245: 15x10  2            898            245    15     10
## 3    #3 @ 8,408: 12x27  3              8            408    12     27
## 4  #4 @ 532,184: 16x13  4            532            184    16     13
## 5  #5 @ 550,829: 11x10  5            550            829    11     10
## 6  #6 @ 656,906: 13x12  6            656            906    13     12
## 7  #7 @ 489,357: 24x23  7            489            357    24     23
## 8  #8 @ 529,898: 12x19  8            529            898    12     19
## 9  #9 @ 660,201: 19x28  9            660            201    19     28
## 10 #10 @ 524,14: 21x27 10            524             14    21     27</code></pre>
<p>Now that we have the dimensions, how do we go about determining the overlap?<br>
My idea was to, for each claim, create a series of ‘coordinates’ where the digit before a dot indicates the position from the left edge and the second number the position from the top edge. Overlapping squares from different samples would have the same ‘coordinates’.</p>
<p>Right, but how to do it in R? this is where <code>outer</code> function comes in handy. Let’s take the first example:</p>
<pre><code># first example
#1 @ 1,3: 4x4 

from_left_edge &lt;- 1 
width &lt;- 4
from_top_endge &lt;- 3
height &lt;- 4

# create a series of coordinates per set of dimensions
dims &lt;- as.vector(outer(from_left_edge + 1:width,
                          from_top_endge + 1:height,
                          paste, sep = '.'))

sort(dims)</code></pre>
<pre><code>##  [1] &quot;2.4&quot; &quot;2.5&quot; &quot;2.6&quot; &quot;2.7&quot; &quot;3.4&quot; &quot;3.5&quot; &quot;3.6&quot; &quot;3.7&quot; &quot;4.4&quot; &quot;4.5&quot; &quot;4.6&quot;
## [12] &quot;4.7&quot; &quot;5.4&quot; &quot;5.5&quot; &quot;5.6&quot; &quot;5.7&quot;</code></pre>
<p>Neat! Now, let’s wrap it up in a function and apply it to the challenge dataset:</p>
<pre><code>#function that creates coordicates for squares occupied by each claim
get_dimensions &lt;- function(from_left_edge,
                           width,
                           from_top_endge,
                           height
) {
  
  dims &lt;- as.vector(outer(from_left_edge + 1:width,
                          from_top_endge + 1:height,
                          paste, sep = '.')) 
  return(dims)
  
}


## apply the function to the challenge dataset 
final_list &lt;- pmap(list(from_left_edge = clean_input$from_left_edge,
                      width = clean_input$width,
                      from_top_endge = clean_input$from_top_endge,
                      height = clean_input$height),
                 get_dimensions)

# a list of 'coordinates' for the first claim
head(final_list[[1]])</code></pre>
<pre><code>## [1] &quot;851.302&quot; &quot;852.302&quot; &quot;853.302&quot; &quot;854.302&quot; &quot;855.302&quot; &quot;856.302&quot;</code></pre>
<p>Now, let’s calculate the number of those coordiates that appear more than once in our list, which will give us the final solution to the Day 3 Puzzle:</p>
<pre><code>## final solution
final_list%>% 
  unlist() %>% 
  table() %>% # get counts per coordinate
  as_tibble() %>% # put it in usable format
  filter(n > 1) %>%  
  nrow() </code></pre>
<pre><code>## [1] 113576</code></pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://r-tastic.co.uk/post/advent-of-code-day-3/"> r-tastic</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/ev3q-ZuS34Q" height="1"></a></div>