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 tibble
s 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.
For example:
1122
produces a sum of 3 (1 + 2)
because the first digit (1
) matches the second digit and the third digit (2)
matches the fourth digit.1111
produces 4
because each digit (all 1
) matches the next.1234
produces 0
because no digit matches the next.91212129
produces 9
because the only digit that matches the next one is the last digit, 9
.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
Load Packages
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
puzzle <- tibble(digits = digits[[1]]) %>%
mutate(
index = row_number(),
digits = parse_integer(digits)) %>%
select(index, digits)
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 Match
or No Match
.
Find out where the matches are
puzzle <- puzzle %>%
mutate(match = case_when(
digits == digits[index + 1] ~ "Match",
TRUE ~ "No Match"))
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
## [38;5;246m# A tibble: 1 x 1[39m
## sum_of_matches
## [3m[38;5;246m<int>[39m[23m
## [38;5;250m1[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.
For example:
1212
produces 6
: the list contains 4
items, and all four digits match the digit 2
items ahead.1221
produces 0
, because every comparison is between a 1
and a 2
.123425
produces 4
, because both 2
s match each other, but no other digit has a match.123123
produces 12
.12131415
produces 4
.My initial thought is to just break the dataframe in half and then check if the digits match, which is acomplished by slice
ing it in half and preparing to bind the columns by renaming some variables.
first_half <- puzzle %>%
slice(1:(nrow(puzzle) / 2)) %>%
select(-match) %>%
rename(
first_index = index,
first_digits = digits)
second_half <- puzzle %>%
slice(((nrow(puzzle) / 2) + 1):nrow(puzzle)) %>%
select(-match) %>%
rename(
second_index = index,
second_digits = digits)
Now it is a simple bind_cols
and then checking for matches, adding matches together and summing that column to get our answer.
first_half %>% bind_cols(second_half) %>%
mutate(match = if_else(first_digits == second_digits, "Match", "No Match")) %>%
filter(match == "Match") %>%
mutate(total = first_digits + second_digits) %>%
summarise(sum = sum(total))
## [38;5;246m# A tibble: 1 x 1[39m
## sum
## [3m[38;5;246m<int>[39m[23m
## [38;5;250m1[39m [4m1[24m220
YES! Correct again! 🎉