Over on the
rOpenSci Slack, Sam asked if anyone was doing the Advent of Code challenges in R. A few others said they were interested and I decided to go for it as well! My solutions are likely not as savvy as the other more experienced programmers, but it was a fun way to see how other people approach problems and if there is anything about their approach that you can incorporate into your programming style.
I tend to work often with
tibbles and rely often on
dplyr so my solution orients itself around a dataframe and using common
dplyr functions to solve the problem.
There are two parts to this problem:
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
1122produces a sum of
3 (1 + 2)because the first digit (
1) matches the second digit and the third digit
(2)matches the fourth digit.
4because each digit (all
1) matches the next.
0because no digit matches the next.
9because the only digit that matches the next one is the last digit,
You’re given data in the form of a long string of integers and task to solve the problem by providing the sum of the digits provided based on the rules introduced above.
Here are the packages I used
My puzzle input
It may seem weird, but I thought it would be easier to read this in as a string and then use
stringr::str_split() to get each value separated in order to aid in processing.
Split the data into a single digit vector
My next idea was to create an index so I don’t have to use a
for() loop and rely on the index generated from that process. I also converted the digits back to the integer data type.
Convert vector to a tibble and add an index
I find setting up even really simple
if_else logic so much easier when using
case_when() since I don’t have to worry about the dataframe and can use the variable name. We’re just checking to see if the digit ahead is similar to the digit before and designating the flag in another column with either
Find out where the matches are
I was convinced for a second I could do something like
puzzle$digits[-1] to get the last value but then remembered I was thinking about Python and not R–whoops! Here I’m just checking to see if the first and last digits match since the list is conceptually circular. This reminds me of the first and last lines of Finnegan’s Wake being circular. In any case, this is just a quick check.
Check if last and first digits match
Now we can get the sum and check our work to see if our solution returned a correct response.
Get the sum
## [90m# A tibble: 1 x 1[39m ## sum_of_matches ## [3m[90m<int>[39m[23m ## [90m1[39m [4m1[24m029
YAY Correct! 🥂
Here are the rules for part two:
Now, instead of considering the next digit, it wants you to consider the digit halfway around the circular list. That is, if your list contains 10 items, only include a digit in your sum if the digit 10/2 = 5 steps forward matches it. Fortunately, your list has an even number of elements.
6: the list contains
4items, and all four digits match the digit
0, because every comparison is between a
4, because both
2s match each other, but no other digit has a match.
My initial thought is to just break the dataframe in half and then check if the digits match, which is acomplished by
sliceing it in half and preparing to bind the columns by renaming some variables.
Now it is a simple
bind_cols and then checking for matches, adding matches together and summing that column to get our answer.
## [90m# A tibble: 1 x 1[39m ## sum ## [3m[90m<int>[39m[23m ## [90m1[39m [4m1[24m220
YES! Correct again! 🎉