# Day 1 Inverse Captcha

## 1.1 Part I

Let’s get started with the challenge from Day 1. Here’s the key part:

```
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.
```

In short, for any series of numbers, the “captcha” is the sum of all digits that match the next digit in the series. If the last digit matches the first digit, then it also gets added into the sum.

I ended up taking the following approach (which you can review in the code chunk below):

- Split the numeric string into a vector of digits using
`str_split`

- For each digit, if the digit was equal to the next digit in the series, then I would add that value to my
`sum`

.

- Since my first and last digit was 8, I added 8 to the final sum.

## 1.2 Part II

Part II adds a few differences, forcing us to think more generally about this problem (that + 8 at the end of that previous method works, but is not neat in the least!).

```
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 2s match each other, but no other digit has
a match.
- 123123 produces 12.
- 12131415 produces 4.
```

We start with the same input (`x`

) as before, but we need a few modifications:

- To make circling around the list easy, we can double
`x_vector`

- Now rather than compare
`i`

and`i + 1`

, we’re interested in`i`

and`i + l/2`

. (English: any digit and the digit halfway around the circular list). - Save as a sum like before! No need to add 8 since we’re checking on all our digits (from 1 to the total length of
`x_vector`

) in our for-loop.

```
sum <- 0
x_vector2 <- c(x_vector, x_vector) #doubled
len <- length(x_vector)
for(i in 1:len){
if(x_vector2[i] == x_vector2[i + len/2]){
sum <- sum + as.numeric(x_vector[i])
}
}
sum
```

Not so bad for Day 1, eh?